Specify a value can be a string or null with JSON Schema Specify a value can be a string or null with JSON Schema json json

Specify a value can be a string or null with JSON Schema


From https://json-schema.org/understanding-json-schema/reference/type.html

The type keyword may either be a string or an array:

  • If it’s a string, it is the name of one of the basic types above.
  • If it is an array, it must be an array of strings, where each string is the name of one of the basic types, and each element is unique. In this case, the JSON snippet is valid if it matches any of the given types

The same page lists also the defined data type names, including string and null.

Try:

"member_region": { "type": ["string", "null"] }


Extending on Explosion Pills answer if you go for the array syntax:

"member_region": { "type": [ "string", "null" ] } // this works

because you are stating a type, not an example/value. You shouldn't go for:

 "member_region": { "type": [ "string", null ] } // this throws an exception


["string", "null"] will work if you're not using a RegEx pattern.

I'm using a date field validator:

    (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d 

to allow a MM/dd/yyyy pattern, but it will throw a non-match error at an empty string ("")