blank schema and JSON

60 views
Skip to first unread message

Mashrur Mia

unread,
Oct 1, 2018, 4:38:33 AM10/1/18
to JSON Schema
Hi,

So a newbie question. Does JSON Schema validates the JSON structure itself, given that schema is blank?

For example, 

if I have blank schema such as

{}

and try to validate the following


{
    "hello": "world",
}


would the result be invalid? (note the trailing comma)

The blank schema say it accepts anything but I assume that it accepts any properties. But if the JSON itself is structurally invalid, it should not validate, right?

thanks

Sa'ad

Loren Cahlander

unread,
Oct 12, 2018, 4:18:23 AM10/12/18
to json-...@googlegroups.com, Loren Cahlander
Hello Sa’ad,

Unfortunately there are a majority of JSON parsers that are very forgiving and the trailing comma does not show as an error.  I think that they should.

This loosy-goosy approach shows up even in the polymer custom element generation.  The following is in the automatically generated javascript definition for an element:

  static get properties() {
    return {
      prop1: {
        type: String,
        value: ‘my-element',
      },
    };
  }

You will notice that there is a trailing comma after prop1 definition.

A JSON Schema validation should also validate the structural soundness of the JSON document as well, but they don’t.

Regards,
Loren


--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Anton Parkhomenko

unread,
Oct 12, 2018, 4:18:36 AM10/12/18
to json-...@googlegroups.com
Hey Sa'ad,

Basically, JSON Schema supposed to validate JSON. The string you provided is not a JSON instance, so obviously schema cannot validate nor invalidate it. I think it is up to a library implementation to decide how to handle cases like this (e.g. runtime exception), but for most statically typed languages like Scala, this operation ("validate non-JSON against JSON schema") would not make any sense.

Hope that helps.

Anton

mashrur mia [sa'ad]

unread,
Oct 12, 2018, 4:43:41 AM10/12/18
to json-...@googlegroups.com
thanks Loren,

I have also noticed odd cases like

{
"name": "bob"
},
"age": 32

got validated by the schema. I can find an example, if you wish to see.

I resorted to Jackson's DeserializationFeature.FAIL_ON_TRAILING_TOKENS
setting in Java to sort this out (and reading off the whole tree). I
would have expected this is fundamental check via JSON Schema :(

anyways,

thanks

Sa'ad

mashrur mia [sa'ad]

unread,
Oct 12, 2018, 4:44:51 AM10/12/18
to json-...@googlegroups.com
ok, well if that's the case then be it. I assume we would need an
addition pre-processing to validate that the input is a valid JSON.

Henry Andrews

unread,
Oct 18, 2018, 9:29:41 AM10/18/18
to JSON Schema
JSON Schema is defined on a data model, which is derived from JSON.  It is not defined over the JSON file/character sequence itself.  Many JSON Schema implementations will use some JSON parser automatically for you (in which case you get whatever error checking that parser provides).  Many require you to parse the JSON yourself.  In many applications, JSON parsing is something that happens separately from validation anyway, so this is intentional.

-henry
Reply all
Reply to author
Forward
0 new messages