There have been a lot of posts asking about the support for custom types, so forgive me if this is a duplicate but I couldn't find the answer I was looking for.
One of the things we are using JSON Schemas for is to generate valid seed data to mock API responses. One problem we've encountered is that it's hard to know what kind of fake data is allowed. E.g. a "firstName" property would expect one kind of string whereas "mailingAddress" would expect another (etc. etc. etc.). They can't all use "Lorem Ipsum" text.
From what I understand, one of the ways that this could be solved is by providing a custom $schema and then referencing all of them using the "allOf" keyword.
For example, if we had the normal JSON Schema definition like this (named "a.json"):
and then a custom schema type (named b.json):
{
"$schema": "http://my-custom-schema/",
"type": "object",
"properties": {
"firstName": {"type": "name"},
"mailingAddress": {"type": "address"}
}
}
Our schema could combine them doing something like:
{
"allOf": ["a.json", "b.json"]
}
Since the b.json specifies a custom $schema, then schema validators SHOULD ignore it, right? And we can code our own validator to work with schemas defined as $schema "
http://my-custom-schema/". Is that correct?
Since this approach is a bit laborious (it effectively requires us to create 2 extra schemas to adequately describe a schema), I'm wondering if it's acceptable to add in custom keywords to accommodate the extra data? A schema validator SHOULD ignore extra/superfluous keywords, right?
In the below schema, I've added in custom values for "x-type" to specify to our random generators what kind of fake data should be generated for each field.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"firstName": {"type": "string", "x-type": "name"},
"mailingAddress": {"type": "string", "x-type": "address"}
}
}
Is that a valid solution to add custom keywords to a schema definition? The online validator @
https://json-schema-validator.herokuapp.com/ shows a warning for a schema like that, but not an error (it says that the keywords are unknown and will be ignored).
There are lots of other use cases for this type of thing, but hopefully the above demonstrates a couple possible solutions. Can someone tell me if they are valid solutions?