shape
Rust port of the shape schema-by-example
validator. Your schema looks (almost) exactly like your data.
use ;
let s = new;
let out = s.validate?;
// out == { "debug": true, "port": 8080, "host": "localhost" }
The TypeScript implementation in ../ts is canonical: this
port matches it for validation outcomes, produced values and exact error
text, and a shared conformance corpus plus a
differential harness keep it that way. The
full documentation is in ../docs; the Rust surface is
in the Rust API reference and the
how-to.
Install
The package is shape-schema, since crates.io already had an unrelated
shape; the library is shape, so the code reads use shape::…. Requires
Rust 1.75 or later. The serde feature (on by default) adds the conversions
with serde_json::Value and validate_into.
Concepts
A schema is built from an example value. Literal values become optional
with a default; type tokens (Token::String, Token::Number, ...) become
required. Objects are closed unless opened (open, child); a
one-element array is an element shape, a longer one a tuple.
Every builder of the canonical implementation is here as a function taking the spec it applies to last, and as a chain method:
use ;
let port = optional;
let name = buildize.min.max;
validate produces the value with defaults injected; valid gives a
verdict without producing; error lists every issue; validate_into::<T>
deserializes the produced value with serde; json writes the shape as
declarative JSON and build reads it back; json_schema exports a JSON
Schema and from_json_schema imports one; Schema::parse reads the string
form ("String.Min(2)").
Development
License
MIT