Expand description
§shape
Schema by example: a spec is written as an example of the value it accepts, and validating a value against it completes it. The Rust port of shape; the TypeScript implementation is canonical and this one is held to it by a shared corpus.
use shape::{obj, shape, Spec, Token, Value};
let s = shape(obj([("name", Spec::from(Token::String)), ("port", Spec::from(8080))]));
let out = s.validate(Value::from(serde_json::json!({ "name": "x" }))).unwrap();
assert_eq!(serde_json::Value::from(out), serde_json::json!({ "name": "x", "port": 8080 }));Re-exports§
pub use algebra::extend;pub use algebra::omit;pub use algebra::partial;pub use algebra::pick;pub use algebra::Names;pub use argu::Argu;pub use argu::ArguError;pub use argu::Signature;pub use context::Context;pub use context::PathPart;pub use context::State;pub use context::Update;pub use context::UpdateErr;pub use discriminated::discriminated;pub use discriminated::Disc;pub use error::FieldError;pub use error::ValidationError;pub use expr::build;pub use expr::expr;pub use expr::expr_apply;pub use expr::ExprError;pub use isolate::Inner;pub use isolate::TransformFn;pub use json::node_json;pub use json::JsonError;pub use jsonschema::json_schema;pub use jsonschema_import::from_json_schema;pub use jsonschema_import::JsonSchemaError;pub use node::Kind;pub use node::ListMode;pub use node::Node;pub use node::Token;pub use node::Validator;pub use node::ValidatorFn;pub use normalize::Options;pub use spec::arr;pub use spec::from_map;pub use spec::null;pub use spec::obj;pub use spec::Spec;pub use standard::StandardIssue;pub use standard::StandardResult;pub use standard::StandardSchema;pub use stringify::stringify_node;pub use value::Map;pub use value::Value;pub use builders::*;
Modules§
- algebra
- Object algebra:
pick,omit,partialandextendbuild a new object shape out of an existing one. The result is a fresh node, so the source is left as it was and one base can be reshaped many times. Key expressions in a source ({"a: Min(2)": 0}) are compiled on the way in, since the algebra has to know the real property names. - argu
- Argu: positional arguments matched against an ordered spec, as a call
signature is:
argu.validate(args, "foo", spec)gives the arguments by name. Askipslot is optional and shifts the ones after it; arestslot captures whatever remains. - builders
- The builders: functions that take a spec and return a node with the builder’s rule applied, and the same as methods on a node so they chain.
- coerce
- Coerce: the value converted to the node’s kind where the conversion is unambiguous, before the type check; and the strict ISO 8601 date-time both implementations parse identically.
- context
- What flows through a validation: the caller’s context, the state a validator sees at its node, and the update it fills in.
- discriminated
- A discriminated union: the branch is chosen by the value of a tag property and the value validated against that branch alone, so the errors are its own rather than a list of every alternative.
- error
- Errors: one per failing value, with the text TypeScript renders for it.
- expr
- The string form of a spec:
"String.Min(2)","Min(2, Number)","One(String, Number)". Builder names, type tokens, JSON literals and/regexp/tokens, chained with dots and given arguments in parentheses. - format
- String formats: Email, Url, Uuid, DateTime, Ip, Ipv4, Ipv6. Every pattern
is written so that the
regexcrate and the JavaScript engine agree on it: ASCII classes only, no lookaround, explicit whitespace. - isolate
- Isolated validation: Catch and Transform (Ignore probes the same way from its call sites). These builders take the checks a node carries, its befores and its afters, inside, and validate the node as a whole (those checks, the structural check, every descendant) in a sub-run before the node itself proceeds. Only then is the outcome of the entire subtree known at once; the canonical implementation does the same, since it runs a node’s afters before visiting its children.
- json
- Declarative JSON
- jsonschema
- JSON Schema export (draft 2020-12): the values a node accepts. Every kind, bound, format, literal set, composition, reference and default has a rendering; a check that is a function, and the builders that only change what comes out (Coerce, Catch, Transform, Rename, Key), have none. The canonical implementation renders the same schema for the same shape, and the differential harness compares the two.
- jsonschema_
import - JSON Schema import (draft 2020-12, and the common keywords of earlier drafts), the inverse of the export: a type becomes a token, bounds become size builders, formats and patterns their builders, enum and const become Exact, properties and items become objects and arrays, the compositions become One, All and Discriminated, and a definition is inlined where it is referenced, with Define and Refer only where a definition refers to itself. A property that is not required and has no default is Skip. Unknown keywords are ignored; an unknown type or reference is an error.
- node
- The compiled form of a spec: a tree of nodes, each a kind with its flags, its children and its validators.
- normalize
- Spec by example to node: a literal is optional and its own default, a type token is required, an object or array carries its children.
- spec
- A spec by example: the value a spec is written as, before it is compiled
into a node. Rust has no untyped literal, so a spec is built from these
conversions, from the builders, or from the
shape!macro. - standard
- Standard Schema V1 interoperability (https://standardschema.dev/): a version, a vendor, and a validate that never fails, reporting either the produced value or a list of issues.
- stringify
- A node rendered as the spec text it stands for: type names for required nodes, values for optional ones, the builder chain as a suffix. This is what a composite message names its branches with.
- validate
- The walk: a node against a value, producing in place.
- value
- The value shape validates: what JSON can carry, plus the four things the
canonical behaviour needs and JSON cannot say (an absent value,
NaN, a date, a bigint), with object keys in insertion order.
Macros§
- shape
- A spec written as the example it is; see the module documentation.
Structs§
- Schema
- A compiled spec.
Enums§
- Into
Error - What
validate_intocan fail with: the validation, or the deserialization of the produced value into the target type.
Constants§
- VERSION
- The crate version.
Functions§
- shape
- Compile a spec.