Loading...
Loading...
Authors JSON Schema definitions for use with z-schema validation. Use when the user needs to write a JSON Schema, define a schema for an API payload, create schemas for form validation, structure schemas with $ref and $defs, choose between oneOf/anyOf/if-then-else, design object schemas with required and additionalProperties, validate arrays with items or prefixItems, add format constraints, organize schemas for reuse, or write draft-2020-12 schemas.
npx skill4agent add zaggino/z-schema writing-json-schemas$schematype{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {},
"required": [],
"additionalProperties": false
}additionalProperties: false{
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 }
},
"required": ["name", "email"],
"additionalProperties": false
}{
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zip": { "type": "string", "pattern": "^\\d{5}(-\\d{4})?$" }
},
"required": ["street", "city"]
}
}
}patternProperties{
"type": "object",
"patternProperties": {
"^x-": { "type": "string" }
},
"additionalProperties": false
}propertyNames{
"type": "object",
"propertyNames": { "pattern": "^[a-z_]+$" }
}{
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
}prefixItemsitems{
"type": "array",
"prefixItems": [{ "type": "string" }, { "type": "integer" }],
"items": false
}items: false{
"type": "array",
"contains": { "type": "string", "const": "admin" }
}{
"type": "array",
"contains": { "type": "integer", "minimum": 10 },
"minContains": 2,
"maxContains": 5
}{
"type": "string",
"minLength": 1,
"maxLength": 255,
"pattern": "^[A-Za-z0-9_]+$"
}datedate-timetimeemailidn-emailhostnameidn-hostnameipv4ipv6uriuri-referenceuri-templateiriiri-referencejson-pointerrelative-json-pointerregexdurationuuid{ "type": "string", "format": "date-time" }formatAssertions: nullformatAssertions: true{
"type": "number",
"minimum": 0,
"maximum": 100,
"multipleOf": 0.01
}exclusiveMinimumexclusiveMaximum{ "type": "integer", "exclusiveMinimum": 0, "exclusiveMaximum": 100 }anyOf{
"anyOf": [{ "type": "string" }, { "type": "number" }]
}oneOf{
"oneOf": [
{ "type": "string", "maxLength": 5 },
{ "type": "string", "minLength": 10 }
]
}allOf{
"allOf": [{ "$ref": "#/$defs/base" }, { "properties": { "extra": { "type": "string" } } }]
}not{ "not": { "type": "null" } }ifthenelseoneOf{
"type": "object",
"properties": {
"type": { "type": "string", "enum": ["personal", "business"] },
"company": { "type": "string" }
},
"if": { "properties": { "type": { "const": "business" } } },
"then": { "required": ["company"] },
"else": {}
}| Scenario | Use |
|---|---|
| Value can be multiple types | |
| Exactly one variant must match | |
| Compose inherited schemas | |
| "if condition then require fields" | |
| Exclude a specific shape | |
ifthenelseoneOf$ref$defs{
"$defs": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
},
"required": ["street", "city"]
}
},
"type": "object",
"properties": {
"home": { "$ref": "#/$defs/address" },
"work": { "$ref": "#/$defs/address" }
}
}import ZSchema from 'z-schema';
const schemas = [
{
$id: 'address',
type: 'object',
properties: { city: { type: 'string' } },
required: ['city'],
},
{
$id: 'person',
type: 'object',
properties: {
name: { type: 'string' },
home: { $ref: 'address' },
},
required: ['name'],
},
];
const validator = ZSchema.create();
validator.validateSchema(schemas);
validator.validate({ name: 'Alice', home: { city: 'Paris' } }, 'person');unevaluatedPropertiesallOfadditionalProperties: falseunevaluatedProperties{
"allOf": [
{
"type": "object",
"properties": { "name": { "type": "string" } },
"required": ["name"]
},
{
"type": "object",
"properties": { "age": { "type": "integer" } }
}
],
"unevaluatedProperties": false
}{ "name": "Alice", "age": 30 }{ "name": "Alice", "age": 30, "extra": true }const validator = ZSchema.create();
try {
validator.validateSchema(schema);
} catch (err) {
console.log('Schema errors:', err.details);
}additionalPropertiesadditionalProperties: falseunevaluatedProperties: falseadditionalProperties: falseallOfunevaluatedProperties: falseitemsprefixItemsitems$schema$schemadefinitions$defs$defs