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.
So, the moral of the story is that there is no default type and you should not leave it out of your schemas.
Jason