Meta-Schema Basics
The meta-schema system in @jetio/validator validates that your JSON Schemas are correctly structured according to JSON Schema specifications. This ensures your schemas are valid before you use them for data validation.
What Are Meta-Schemas?
A meta-schema is a JSON Schema that validates other JSON Schemas. It defines:
- What keywords are allowed
- The structure of keyword values
- Relationships between keywords
- Draft-specific rules and constraints
Think of it as “schema validation for schemas.”
// Your data schema
const userSchema = {
type: "object",
properties: {
name: { type: "string" },
},
};
// A meta-schema validates that userSchema is correct:
// - Is 'type' a valid keyword?
// - Is 'object' a valid type value?
// - Is 'properties' structured correctly?Why Validate Schemas?
1. Catch typos and errors early
const badSchema = {
type: "objct", // Typo: should be 'object'
properties: {
name: { type: "string" },
},
};
jetValidator.validateSchema(badSchema); // false2. Prevent invalid configurations
const invalidSchema = {
type: "number",
minimum: "10", // String instead of number
};
jetValidator.validateSchema(invalidSchema); // false3. Enforce draft compatibility
// Using a Draft 2020-12 keyword with Draft-07
const incompatibleSchema = {
$schema: "https://json-schema.org/draft-07/schema",
type: "object",
unevaluatedProperties: false, // Only in 2019-09+
};
jetValidator.validateSchema(incompatibleSchema); // falseSupported JSON Schema Drafts
| Draft | URI | Year | Key Features |
|---|---|---|---|
| Draft-06 | https://json-schema.org/draft-06/schema | 2017 | const, contains, propertyNames |
| Draft-07 | https://json-schema.org/draft-07/schema | 2018 | if/then/else, readOnly, writeOnly |
| Draft 2019-09 | https://json-schema.org/draft/2019-09/schema | 2019 | unevaluatedProperties, unevaluatedItems, vocabulary system |
| Draft 2020-12 | https://json-schema.org/draft/2020-12/schema | 2020 | prefixItems, $dynamicRef, improved $ref |
Recommendation: Use Draft-07 for maximum compatibility, or Draft 2020-12 for the newest features.
Last updated on