foundation_jsonschema
A self-contained, no_std-compatible JSON Schema validation library for Rust.
Supports Draft 4, 6, 7, 2019-09, and 2020-12 with a fluent scheme builder API.
Features
- Five draft versions — Draft 4, 6, 7, 2019-09, 2020-12 with automatic detection
- Fluent builder API — type-safe
scheme::*builders that produce valid schemas - Format validation — email, uri, uuid, date-time, hostname, ipv4, ipv6, base64
- Compound schemas —
anyOf,oneOf,allOf,not,if/then/else - Custom keywords — register your own validation logic
- Custom formats — override or extend the built-in format checkers
- No external dependencies — all meta-schemas resolved in-memory
no_stdcompatible — works withalloc, regex viafancy-regexfeature
Quick Start
use ;
use json;
// Validate a schema directly
let schema = json!;
let validator = validator_for.unwrap;
assert!;
assert!; // missing required
The Scheme Builder API
The scheme module provides type-safe, fluent builders for constructing JSON
Schema documents. Each builder only exposes methods that make sense for its
type, catching configuration errors at compile time rather than runtime.
Primitives
Strings with length, pattern, and format constraints:
use scheme;
use json;
let validator = string
.min_len
.max_len
.pattern
.build
.compile
.unwrap;
assert!;
assert!;
Format shorthands for common string patterns:
let validator = string
.email
.build
.assert_format // enforce format validation
.compile
.unwrap;
assert!;
assert!;
Available format shorthands: .email(), .uri(), .uuid(), .date_time(),
.hostname(), .ipv4(), .ipv6(), .base64().
Integers with range and divisibility:
let validator = integer
.min
.max
.description
.build
.compile
.unwrap;
assert!;
assert!;
assert!;
Convenience shorthands: .positive(), .nonnegative(), .negative(),
.nonpositive(), .multiple_of(5), .exclusive_min(0), .exclusive_max(100).
Numbers (floats):
let validator = number
.min
.max
.build
.compile
.unwrap;
assert!;
assert!;
assert!;
Booleans and null:
let validator = boolean
.default
.build
.compile
.unwrap;
// Null schema — matches only null
let null_validator = null
.description
.build
.compile
.unwrap;
Objects
Required and optional properties:
let validator = object
.required
.required
.required
.optional
.build
.assert_format
.compile
.unwrap;
assert!;
assert!;
Strict mode — reject unknown properties:
let validator = object
.required
.strict // additionalProperties: false
.build
.compile
.unwrap;
assert!;
assert!;
Loose mode — allow any extra properties (default, but explicit):
let schema = object
.required
.loose // additionalProperties: true
.build_schema;
Catch-all schema — extra properties must match a schema:
let schema = object
.required
.catchall // all extra fields must be strings
.build_schema;
Pattern properties — validate properties matching a regex:
let schema = object
.pattern_property
.build_schema;
// Properties like "x-custom", "x-header" must be strings
Nested objects:
let validator = object
.required
.required
.build
.compile
.unwrap;
Arrays
Items with constraints:
let validator = array
.items
.min_items
.max_items
.unique
.build
.compile
.unwrap;
assert!;
assert!; // min_items: 1
assert!; // unique
assert!; // max_items: 5
Tuple validation (Draft 2020-12 prefixItems):
let validator = array
.prefix_items
.len
.build
.compile
.unwrap;
assert!;
assert!;
assert!;
Additional items for tuple + rest patterns:
let schema = array
.prefix_items
.additional_items
.build_schema;
// First item: string, second: integer, rest: boolean
Contains, min_contains, max_contains (Draft 6+):
let schema = array
.contains
.min_contains
.max_contains
.build_schema;
Nullable and Optional
Nullable — accepts the type or null:
let validator = object
.required
.build
.compile
.unwrap;
assert!;
assert!;
assert!;
Optional — wraps in anyOf with null:
let validator = object
.required
.required
.build
.compile
.unwrap;
assert!;
assert!;
Note: .optional() on a field schema (used with required()) makes the field
value nullable. Use .optional(name, schema) on the object builder to add a
property that is not in the required array.
Enum and Literal Values
Enum constraint on strings:
let validator = object
.required
.required
.build
.compile
.unwrap;
assert!;
assert!;
Standalone enum:
use ;
use json;
let schema = r#enum;
let v = validator_for.unwrap;
assert!;
assert!;
Literal / const:
use validator_for;
use json;
let schema = literal;
let v = validator_for.unwrap;
assert!;
assert!;
Compound Schemas
anyOf — must match at least one:
use validator_for;
use json;
let schema = any_of;
let v = validator_for.unwrap;
assert!;
assert!;
assert!;
oneOf — must match exactly one:
let schema = one_of;
allOf — must match all (intersection):
use validator_for;
use json;
let schema = all_of;
let v = validator_for.unwrap;
assert!;
assert!;
not — inverted validation:
use validator_for;
use json;
let schema = not;
let v = validator_for.unwrap;
assert!;
assert!;
if/then — conditional validation:
use validator_for;
use json;
let schema = if_then;
let v = validator_for.unwrap;
// Non-admin: no permissions required
assert!;
// Admin with permissions: valid
assert!;
if/then/else — discriminated union:
use validator_for;
use json;
let schema = if_then_else;
let v = validator_for.unwrap;
assert!;
assert!;
References
// Internal reference
let schema = ref_;
// Dynamic reference (Draft 2020-12)
let schema = dynamic_ref;
Schema Inspection
Build a schema without compiling it, for inspection or serialization:
use json;
let schema = object
.required
.required
.optional
.strict
.build_schema;
// schema is a serde_json::Value — inspect, serialize, or pass to other tools
println!;
build_schema vs build
build_schema(&self)— returns aserde_json::Value. Non-consuming, can be called multiple times. Useful for inspection.build(self)— returns aValidationOptions. Consuming, chains into.compile()for the full build-compile-validate pipeline.
use json;
let builder = string.min_len;
// Inspect multiple times
let s1 = builder.build_schema;
let s2 = builder.build_schema;
// Compile and validate (consumes builder)
let validator = builder.build.compile.unwrap;
ValidationOptions
Fine-grained control over schema compilation:
use ;
let validator = object
.required
.build
.with_draft
.assert_format
.compile
.unwrap;
Schema Accessors
When using build(), the schema is embedded in ValidationOptions:
use json;
let opts = string.min_len.max_len.build;
// Reference without consuming
let schema_ref = opts.schema;
println!;
// Clone the schema
let cloned = opts.clone_schema;
// Take ownership
let owned = opts.into_schema;
Custom Draft Selection
use ;
let validator = object
.required
.build
.with_draft
.compile
.unwrap;
Custom Resolvers
Provide your own resource resolver for external references:
use InMemoryFetcher;
use scheme;
let resolver = builtin;
let validator = object
.build
.with_resolver
.compile
.unwrap;
Direct Validation
For cases where you already have a serde_json::Value schema:
use ;
use json;
let schema = json!;
// Quick boolean check
assert!;
// Full validation with error details
let result = validate;
assert!;
// Reusable validator for repeated checks
let validator = validator_for.unwrap;
assert!;
no_std Support
Enable the fancy-regex feature to use fancy-regex instead of the regex
crate, allowing pattern validation without the standard library:
[]
= { = "0.0.1", = false, = ["fancy-regex"] }