Default type

48 views
Skip to first unread message

Mitar

unread,
Jun 11, 2015, 1:03:51 AM6/11/15
to json-...@googlegroups.com
Hi!

It seems that default type is "object"? At least
http://jsonschemalint.com works like that. But I have not found that
anywhere in the v4 draft? Where is that specified?


Mitar

--
http://mitar.tnode.com/
https://twitter.com/mitar_m

Jason Desrosiers

unread,
Jun 11, 2015, 11:32:26 PM6/11/15
to json-...@googlegroups.com
A schema has no default type.  If you put an empty schema `{}` into the validator, any valid JSON of any type will validate successfully.  It may look like "object" is the default type because your object schemas validates exactly the same whether you leave the type declaration out or not.  Take the following schema for example.

{
 
"type": "object",
 
"properties": {
   
"foo": { "type": "string" }
 
},
 
"required": ["foo"]
}

This schema defines an object that requires a property named "foo" that must be a string.  So, what should we expect to happen if we remove the type declaration?

{
 
"properties": {
   
"foo": { "type": "string" }
 
},
 
"required": ["foo"]
}

If the JSON is an object, it will validate exactly the same as the first schema.  The difference is that if the JSON is of any type other than object, it will also be considered valid.  This includes numbers, strings, etc.  This happens because the object validation keywords only apply when the JSON is an object.  If the JSON is not an object, none of the validation keywords apply and it is equivalent to validating against an empty schema.  This next example should help to illustrate the concept further.

{
 
"properties": {
   
"foo": { "type": "string" }
 
},
 
"required": ["foo"],
 
"maxLength": 2
}

This example has keywords used to validate objects and keywords used to validate strings in the same schema.  The validator should only apply the object validation keywords when the JSON is an object and the string validation keywords when the JSON is a string.  In this case, if the JSON is a string, then it must be 2 characters or less in order to be valid.  If it is an object, it should have a string property called "foo".  JSON of any other type will also be valid.

It looks like the validator at http://jsonschemalint.com/ does not handle this last schema correctly.  Try the validator at http://json-schema-validator.herokuapp.com/ to see the correct validation behavior.

So, the moral of the story is that there is no default type and you should not leave it out of your schemas.

Jason

Mitar

unread,
Jun 19, 2015, 2:17:20 AM6/19/15
to json-...@googlegroups.com
Hi!

Interesting. Thanks for the explanation. I thought that use of
"properties" validation is governed by existence of "type: object" and
not that JSON is an object. This is an interesting design decision.
:-)


Mitar
> --
> 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.
Reply all
Reply to author
Forward
0 new messages