7

I wanted to prevent a json filed from allowing null as a valid value for it. Tried using the keyword not, but no luck.

Want the below json to be validated as false, as the field stats as value as null.

{
  "stats": "null"
}

please find my schema 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"
  ]
}

Though i gave "not": {"type": "null"}, it still validated successfully.

Demitri
  • 13,134
  • 4
  • 40
  • 41
Manu
  • 1,379
  • 6
  • 24
  • 53
  • I tried validating with `{ "stats" : "null" }` and it failed. How are you validating your JSON? – Tim Biegeleisen Jun 25 '15 at 05:16
  • Are you sure it's "null" not 'null'? For this is a String, not a 'null' value – Danielson Jun 25 '15 at 05:32
  • I am using java code to validate the json, with jsonSchema.But I am checking the validation using http://jsonschemalint.com/draft4/#, to test in amuch easier way. – Manu Jun 25 '15 at 05:32

3 Answers3

9

Wow. So much confusion here.

The problem is simple:

{
  "stats": "null"
}

"null" is a string, thus it’s valid (because you allow strings). This would not be allowed by your schema, which works as you expect:

{
    stats: null
}

The answer from Ashish Patil is wrong: in your schema (not your data), when you are specifying the type, the type name is a string. Specifying "not": {"type": null} is not valid. You could specify "not": {"type": "null"}, but that would be redundant as the earlier "type": "string" already implies that.

The accepted answer from jruizaranguren works because it doesn’t allow the string "null". It doesn’t address the core confusion that null is not the same as "null".

Nate
  • 18,752
  • 8
  • 48
  • 54
3

You can use "enum" keyword instead of "type". "null" is not a valid json and json-schema type.

Also additionalProperties and maxProperties are useless within stats 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"
    ]
}
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
3

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

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

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

{ "stats": "null" }

So, if you really wanted null to be not allowed in your file, then your example file should look like { "stats": null } Along schema i have provided.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36