JSON Schema
Check values against a JSON Schema (drafts 4 to 2020-12), with every error and the path of the value at fault.
Version 1.0.0 · 3 functions · 1 crate
Functions
What use json_schema.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use schema_errors, schema_valid from json_schema (Quillo 0.6).
schema_errors(value: any, schema: any) → listor an errorEvery way value breaks schema: { path, message, schema_path } each, path a JSON Pointer to the value at fault (/items/2/price, "" for the whole value), schema_path the rule it broke. An empty list when it is valid. The draft is the schema's $schema, 2020-12 when it names none; $refs to other files or URLs are not fetched. At most 200 errors.
schema_valid(value: any, schema: any) → boolor an errorWhether value is valid against schema.
schema_problems(schema: any) → listThe problems of a schema itself (a misspelt type, a bad pattern): an empty list for a schema that can be used.
Get it
$ quillo install json_schema # json_schema.qwer into this folder$ quillo install json_schema --example # and its example, json_schema.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/json_schema.qwer$ curl -O https://quillolang.org/plugins/files/json_schema.qweThen, in your program, all of it or only what you call:
use json_schema.qweruse schema_errors, schema_valid from json_schema // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
jsonschema = { version = "0.26", default-features = false }Example
// An orders API that checks each body against a JSON Schema and says what is wrong, field by field.//// quillo run json_schema.qwe then open http://localhost:8080/samplesuse "json_schema.qwer"let ORDER = { "$schema": "https://json-schema.org/draft/2020-12/schema", type: "object", required: ["customer", "items"], properties: { customer: { type: "string", format: "email" }, items: { type: "array", minItems: 1, items: { type: "object", required: ["sku", "qty"], properties: { sku: { type: "string", pattern: "^[A-Z][0-9]+$" }, qty: { type: "integer", minimum: 1 } } }, }, note: { type: "string", maxLength: 200 }, }, additionalProperties: false,}route POST /orders { let errors = schema_errors(request.json, ORDER)? if len(errors) > 0 { reply 422 { errors } } { accepted: request.json.customer }}route GET /samples = { good: schema_valid({ customer: "ada@example.com", items: [{ sku: "B7", qty: 2 }] }, ORDER)?, bad: schema_errors({ customer: "ada@example.com", items: [{ sku: "b7", qty: 0 }, { qty: 1 }], colour: "red" }, ORDER)? | map((e) => `${e.path}: ${e.message}`), broken_schema: schema_problems({ type: "strnig" }),}$ quillo run json_schema.qwe$ curl 'localhost:8080/samples'Source
//! Check values against a JSON Schema (drafts 4 to 2020-12): every error with the path of the value at fault, for API bodies, imports and config files.//! version: 1.0.0//!//! [dependencies]//! jsonschema = { version = "0.26", default-features = false }use serde_json::{json, Value};fn validator(schema: &Value) -> Result<jsonschema::Validator, String> { jsonschema::validator_for(schema).map_err(|e| format!("the schema is not valid: {} (at {})", e, e.instance_path))}/// Every way `value` breaks `schema`: `{ path, message, schema_path }` each, `path` a JSON Pointer to the value at fault (`/items/2/price`,/// `""` for the whole value), `schema_path` the rule it broke. An empty list when it is valid. The draft is the schema's/// `$schema`, 2020-12 when it names none; `$ref`s to other files or URLs are not fetched. At most 200 errors.pub fn schema_errors(value: serde_json::Value, schema: serde_json::Value) -> Result<Vec<Value>, String> { let v = validator(&schema)?; Ok(v.iter_errors(&value).take(200).map(|e| json!({ "path": e.instance_path.to_string(), "message": e.to_string(), "schema_path": e.schema_path.to_string(), })).collect())}/// Whether `value` is valid against `schema`.pub fn schema_valid(value: serde_json::Value, schema: serde_json::Value) -> Result<bool, String> { Ok(validator(&schema)?.is_valid(&value))}/// The problems of a schema itself (a misspelt type, a bad pattern): an empty list for a schema that can be used.pub fn schema_problems(schema: serde_json::Value) -> Vec<String> { match jsonschema::validator_for(&schema) { Ok(_) => vec![], Err(e) => vec![format!("{} (at {})", e, e.instance_path)], }}Related
fhir.qwer · 1.0.0HL7 FHIR R4 JSON: resources checked for required fields, bound codes and date formats, Bundles read and built, references resolved across a Bundle, FHIRPath-lite field access, and search parameters with paging, sorting and includes for a FHIR server.3 views · 7 downloadsJSON Patch json_patch.qwer · 1.0.0JSON Patch (RFC 6902) and Merge Patch (RFC 7396): the difference between two values as operations, and patches applied.10 views · 6 downloadsJSONPath jsonpath.qwer · 1.0.0Query values with JSONPath (RFC 9535): filters, slices, recursive descent and functions, with the paths of what matched.8 views · 7 downloadsJSON types json_types.qwer · 1.0.0Types inferred from JSON samples: TypeScript interfaces, a JSON Schema and Rust structs for an API's answers or a webhook's body, with optional and nullable fields found across samples.7 views · 7 downloadsCanonical JSON json_canonical.qwer · 1.0.0Canonical JSON (RFC 8785): one way to write a value, keys sorted and numbers as JavaScript writes them, so hashes and signatures of JSON hold however it was spaced or ordered.7 views · 5 downloadsEncoding encoding.qwer · 1.0.0Base64, base64url, hex and percent-encoding read back, files as base64 and back, and query strings as maps.7 views · 7 downloadsThread 0 posts
On JSON Schema: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.