23

Is it valid to extend a JSON schema with custom properties?

The reason I am asking is, because I am using a schema to also render a form for the JSON the schema describes (each property described in the schema is used as a form element with label and some sort of input).

It would be useful to be able to extend the schema with some properties that I mainly use for the form rendering, but that would be ignored when using the schema to validate the JSON object itself.

I could have two different representations for the JSON object (one being the schema and one being the schema like object with custom properties that I just for creating the form, but it would be easier for maintenance if I can combine both in one).

Unfortunately Google wasn't very helpful and I don't have a huge amount of experience using JSON schemas, so apologies if I am missing something obvious.

Edit 1:
Example Schema Snippet:

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string",
            "CUSTOM_PROPERTY": "CUSTOM_VALUE"
        }
    }
}

Note the above is just a snippet and hence doesn't have title, $schema etc.

DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
bbop99
  • 1,625
  • 1
  • 11
  • 25
  • You can extend a JSON object by adding new properties if after all the JSON is still valid. Could you please show an example ? – PMerlet Dec 07 '16 at 14:11
  • 2
    @Cubi It's more about whether the JSON Schema can be correctly parsed by a JSON schema parser if it contains custom properties. Added an example above. – bbop99 Dec 07 '16 at 14:29

1 Answers1

17

(if it's valid JSON) the validator most probably will ignore your custom properties. But what validator are you going to use ? Check it against that particular validator.

Here you have some online validators to test:

Also, you can extend JSON schema, see https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.6.5

Pedro
  • 1,875
  • 12
  • 15
  • 1
    up to date link: https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.6.5 – Jeroen Dec 28 '19 at 08:24