Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hopefully this isn't obvious to others because I find the docs at https://json-schema.org/ to be lacking in finer details. I'm getting a block of JSON with some properties that can be null or a string. How do you specify, in a JSON Schema (to be parsed by Json.NET's JsonSchema.Parse method), that a value can be of type null or type string?

Is there something simple I'm missing like supplying an array for the type? For example;

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

Also, does anyone have a better source for JSON Schema details than json-schema.org? Where can I find a larger selection of examples? I don't want to read a big document/specification to find something that can easily be demonstrated in a 10 line example.

like image 634
evanmcdonnal Avatar asked Apr 26 '13 16:04

evanmcdonnal


People also ask

How do I allow null values in JSON Schema?

JSON Schema does not allow null as a value unless it is specifically allowed. In your first example schema, a null value would not be valid. If you wish to allow null values for those data types, then you need to explicitly allow null as a type.

Can a JSON string be null?

JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

Should JSON include null values?

You should definitely include it if there is any need to distinguish between null and undefined since those have two different meanings in Javascript. You can think of null as meaning the property is unknown or meaningless, and undefined as meaning the property doesn't exist.

Is null a type of string?

The "Type 'string | null' is not assignable to type string" error occurs when a possibly null value is assigned to something that expects a string . To solve the error, use a non-null assertion or a type guard to verify the value is a string before the assignment.


2 Answers

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"] }
like image 162
Explosion Pills Avatar answered Sep 28 '22 02:09

Explosion Pills


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
like image 21
zardilior Avatar answered Sep 27 '22 02:09

zardilior