defining schema for a schema #736
Replies: 2 comments 4 replies
-
A schema that validates a schema is known as a "meta-schema". Meta-schemas are just schemas. They can be a little mind bending to write, but you can write them just like you would write any other schema. For example, here's a constraint that requires that there's {
"properties": {
"properties": {
"required": ["deploy-region"]
}
}
}
So, using my example, see if you can expand it to express the rest of your constraints. If you're still having trouble, post what you've tried and we'll try to nudge you in the right direction. Also, sorry it took so long for someone to respond. Our whole team was traveling last week and we've gotten a bit behind. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the reply! I'm trying to wrap my head around vocabularies and connecting it with dialect :) Here is what I have: Example JSON Instance Data{
"deployapi": "1.0",
"title": "class-of-2024-blogs",
"blogs": {
"blog-schemas": {
"ScienceBlogs": {
"type": "object",
"properties": {
"topic": {
"type": "string", //REUSE all fields available in JSON schema 'properties'
"minLength": 10,
"maxLength": 128,
"pattern": "^[a-zA-Z0-9]*$",
"example": "Dark Matter",
"primary-key": true //NEW field, that should be available within properties
},
"writers": {
"type": "array",
"items": {
"type": "string"
},
"example": ["Jack", "Jill"]
},
"content": {
"type": "string",
},
"publish-date": {
"type": "string",
"format": "date-time",
}
},
"required": ["topic", "writers", "content", "publish-date"],
"additionalProperties": false
"deploy-region": "us-east", //NEW field, that should be available as sibling to properties
"owner-contact": { //NEW field, that should be available as sibling to properties
"email": "test@gmail.com"
}
},
"TheatreBlogs": {
"type": "object",
"properties": {
"title": {
"type": "string",
"primary-key": true
},
"writers": {
"type": "array",
"items": {
"type": "string"
}
},
"actors": {
"type": "array",
"items": {
"type": "string"
}
},
"genre": {
"type": "string",
"enum": ["comedy", "historical"]
}
"synopsis": {
"type": "string",
},
"publish-date": {
"type": "string",
"format": "date-time",
}
},
"required": ["topic", "writers", "actors", "synopsis", "publish-date"],
"additionalProperties": false
"deploy-region": "us-west",
"owner-contact": {
"email": "drama@gmail.com"
}
},
}
}
} My schema{
"$schema": "https://myorganization/meta-schema#",
"$id": "https://myorganization/blog-schema##",
"type": "object",
"properties": {
"deployapi": {
"type": "string",
"pattern": "^1\\.\\d+(-.+)?$"
},
"title": {
"type": "string"
},
"blogs": {
"$ref": "#/$defs/blogModel"
},
"required": [ "deployapi", "title", "blogs"]
"additionalProperties": false
}
"$defs": {
"blogModel": {
"type": "object",
"properties": {
"blog-schemas": {
"type": "object",
"properties": {
"deploy-region": {
"$ref": "#/$defs/deployRegion"
},
"owning-team": {
"$ref": "#/$defs/owningTeam"
}
}
"required": ["deploy-region", "owning-team"],
"additionalProperties": {
"$dynamicRef": "#meta"
}
}
}
}
"deployRegion": {
"type": "string"
"enum": ["us-east", "us-west"]
},
"owningTeam": {
"type": "object",
"properties": {
"contact-name": {
"type": "string"
},
"contact-email": {
"type": "string"
}
},
"required": ["contact-email"],
"additionalProperties": false
},
}
} My meta-schema{
"$schema": "https://myorganization/meta-schema#",
"$id": "https://myorganization/meta-schema#",
"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
"https://json-schema.org/draft/2020-12/vocab/validation": true,
"https://json-schema.org/draft/2020-12/vocab/meta-data": true,
"https://json-schema.org/draft/2020-12/vocab/format-annotation": true,
"https://json-schema.org/draft/2020-12/vocab/content": true,
"https://myorganization/myorganization-vocab": true
},
"$dynamicAnchor": "meta",
"type": ["object", "boolean"],
"properties": {
"example": true,
"primary-key": {
"type": "boolean"
}
}
} Questions:
[2] Thanks for your time and guidance! |
Beta Was this translation helpful? Give feedback.
-
Hi,
I have previously used simple features of JSON schema to validate my JSON data.
Now, I'm exploring the option of defining a JSON schema to validate another JSON schema.
In my service, I want to allow the user to create any kind of business object. Say the example of blog post from here.
But before any data is created, I'd like my user to give me the JSON schema for their business object. So the JSON schema for the above data would look like this:
Now when they give me the schema, how can I allow the user to include the below in the JSON schema?
Basically, I want to create a schema for a schema.
With my limited search, I could not find good answers. If there is any documentation or previous references please point me to them.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions