Skip to main content

Crate jsonschema_schema

Crate jsonschema_schema 

Source
Expand description

§jsonschema-schema

Crates.io docs.rs GitHub License

Typed Rust structs for JSON Schema (draft 2020-12) documents. Part of the Lintel project.

Unlike raw serde_json::Value, this crate gives you named fields for every standard keyword — properties, items, allOf, $ref, format, and so on — so you can pattern-match and navigate schemas without string lookups.

§Features

  • Strongly typedSchema, SchemaValue (object or boolean), TypeValue (single or union), and SimpleType (the seven primitive type names) with full serde round-tripping
  • All standard keywords — core identifiers, metadata, validation, applicators, composition, conditionals, content, and dependencies
  • Editor extensions — first-class x-taplo, x-tombi-*, x-intellij-*, and x-lintel extension structs
  • Catch-all — unknown properties are preserved in extra: BTreeMap<String, Value>
  • Pointer navigationnavigate_pointer walks a JSON Pointer path through nested schemas, resolving $ref along the way
  • Helper utilitiesref_name, resolve_ref, Schema::type_str, Schema::description, and more

§Usage

§Parsing a schema from JSON

use jsonschema_schema::{Schema, SchemaValue, SimpleType, TypeValue};

let json = serde_json::json!({
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "title": "User",
    "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer", "minimum": 0 }
    },
    "required": ["name"]
});

let schema: Schema = serde_json::from_value(json).unwrap();

assert_eq!(schema.title.as_deref(), Some("User"));
assert_eq!(schema.required_set(), &["name"]);

// type_str() produces a human-readable summary
assert_eq!(schema.type_str().as_deref(), Some("object"));

// Access a nested property schema
let name_sv = schema.properties.get("name").unwrap();
let name = name_sv.as_schema().unwrap();
assert!(matches!(name.type_, Some(TypeValue::Single(ref t)) if *t == SimpleType::String));

§Building a schema programmatically

use jsonschema_schema::{Schema, SchemaValue, SimpleType, TypeValue};
use indexmap::IndexMap;

let mut props = IndexMap::new();
props.insert("email".to_string(), SchemaValue::Schema(Box::new(Schema {
    type_: Some(TypeValue::Single(SimpleType::String)),
    format: Some("email".into()),
    ..Default::default()
})));

let schema = Schema {
    type_: Some(TypeValue::Single(SimpleType::Object)),
    properties: props,
    required: Some(vec!["email".into()]),
    ..Default::default()
};

let json = serde_json::to_value(&schema).unwrap();
assert_eq!(json["required"], serde_json::json!(["email"]));
assert_eq!(json["properties"]["email"]["format"], "email");

navigate_pointer resolves a RFC 6901 JSON Pointer through the typed schema tree, automatically following $ref references within the same document.

use jsonschema_schema::{Schema, SchemaValue, SimpleType, TypeValue, navigate_pointer};
use std::collections::BTreeMap;

// Build a schema with $defs and a $ref
let item = SchemaValue::Schema(Box::new(Schema {
    type_: Some(TypeValue::Single(SimpleType::String)),
    ..Default::default()
}));
let mut defs = BTreeMap::new();
defs.insert("Tag".to_string(), item);

let root = SchemaValue::Schema(Box::new(Schema {
    defs: Some(defs),
    ..Default::default()
}));

let result = navigate_pointer(&root, &root, "/$defs/Tag").unwrap();
let tag = result.as_schema().unwrap();
assert_eq!(tag.type_str().as_deref(), Some("string"));

§Extracting a ref name

use jsonschema_schema::ref_name;

assert_eq!(ref_name("#/$defs/Address"), "Address");
assert_eq!(ref_name("./other.json"), "other.json");

§License

Apache-2.0

Re-exports§

pub use extensions::EnumValueMeta;
pub use extensions::ExtDocs;
pub use extensions::IntellijSchemaExt;
pub use extensions::LintelSchemaExt;
pub use extensions::TaploInfoSchemaExt;
pub use extensions::TaploSchemaExt;
pub use extensions::TombiSchemaExt;

Modules§

extensions

Structs§

ApplicatorVocabulary
Applicator vocabulary — composition, conditionals, and object/array subschemas.
ContentVocabulary
Content vocabulary — contentMediaType, contentEncoding, contentSchema.
CoreVocabulary
Core vocabulary — identifiers, references, and definitions.
FormatAnnotationVocabulary
Format-annotation vocabulary.
MetaDataVocabulary
Meta-data vocabulary — annotations (title, description, etc.).
Schema
A JSON Schema object (draft 2020-12).
SchemaError
A structural validation error found in a schema.
UnevaluatedVocabulary
Unevaluated vocabulary — unevaluatedItems and unevaluatedProperties.
ValidationVocabulary
Validation vocabulary — type checks and numeric, string, array, and object constraints.

Enums§

SchemaValue
A JSON Schema value — either a boolean schema or an object schema.
SimpleType
Primitive type names defined by JSON Schema (simpleTypes).
TypeValue
The value of the JSON Schema type keyword.

Functions§

navigate_pointer
Walk a JSON Pointer path through a schema, resolving $ref at each step.
ref_name
Extract the trailing name from a $ref path (e.g. "#/$defs/Foo" -> "Foo").
resolve_ref
Resolve a $ref within the same schema document.
schema
Generate the JSON Schema for SchemaValue (a JSON Schema 2020-12 document).