JSON schema check for null when "type" = "string"

I wanted json filed not to allow null as a valid value for it. Tried to use the keyword no , but no luck.

You want the bottom json to be checked as false, as the stats field as null.

{ "stats": "null" } 

please find my circuit below: -

 { "$schema": "http://json-schema.org/draft-04/schema#", "id": "http://jsonschema.net#", "type": "object", "additionalProperties": false, "maxProperties": 1, "properties": { "stats": { "id": "http://jsonschema.net/stats#", "type": "string", "maxLength": 5, "minLength": 2, "additionalProperties": false, "maxProperties": 1, "not": {"type": "null"} } }, "required": [ "stats" ] } 

Although I gave "not": {"type": "null"} , it still passed the test successfully.

+5
source share
3 answers

Wow. There is so much confusion.

The problem is simple:

 { "stats": "null" } 

"null" is a string, so it is valid (because you allow strings). This scheme will not be resolved by your scheme, which works as you expect:

 { stats: null } 

The answer from Ashish Patil is incorrect: in your schema (and not in your data), when you specify a type, the type name is a string. The indication "not": {"type": null} invalid. You can specify "not": {"type": "null"} , but that would be superfluous, since the previous "type": "string" already implies this.

The accepted answer from jruizaranguren works because it does not allow the string "null" . This does not concern the main confusion that null does not match "null" .

+3
source

First of all, null is not a string. So try using below in your circuit -

  "stats": { "id": "http://jsonschema.net/stats#", "type": "string", "maxLength": 5, "minLength": 2, "additionalProperties": false, "maxProperties": 1, "not": {"type": null} } 

But in the example snippet below you mentioned something like below -

{ "stats": "null" }

So, if you really want your file not to be allowed to be null, then your example file should look like { "stats": null } Along the schema that I provided.

+3
source

Instead of type, you can use the keyword enum. "null" is not a valid json and json-schema type.

Also, additional properties and maxProperties are useless in the stat description.

 { "$schema" : "http://json-schema.org/draft-04/schema#", "id" : "http://jsonschema.net#", "type" : "object", "additionalProperties" : false, "maxProperties" : 1, "properties" : { "stats" : { "id" : "http://jsonschema.net/stats#", "type" : "string", "maxLength" : 5, "minLength" : 2 "not" : { "enum" : ["null"] } } }, "required" : [ "stats" ] } 
+2
source

All Articles