Why JSON Schema rejects null but accepts a missing field
Your API fixture passes with no nickname. Then a client sends "nickname": null and validation fails. The difference is whether the key is present and which values the schema allows.
Validate three payloads against the same schema
Imagine an API that accepts updates to a user profile. In CodeSwissKnife, open JSON Schema Generator & Validator, switch to validation mode and paste this schema:
{
"type": "object",
"properties": {
"nickname": { "type": "string" }
}
}
Paste each payload into the JSON input and validate it on its own:
{}passes because the property is optional.{"nickname": null}fails atnicknamebecause the property is present and the schema expects a string.{"nickname": "Ada"}passes because the value is a string.
JSON Schema uses required to check for a key and type to check the value of a property. See the object reference and null reference.
Allow null when the API uses it to clear a value
Suppose this endpoint leaves the profile unchanged when nickname is omitted and clears the nickname when it receives null. Change the schema to accept both strings and null:
{
"type": "object",
"properties": {
"nickname": { "type": ["string", "null"] }
}
}
All three payloads now pass. Try {"nickname": 42} as a check that numbers still fail.
For an API response that always includes the key, add "required": ["nickname"] at the top level of the schema, alongside properties. The empty object now fails and both null and a string still pass.
Test the API behavior as well as the schema
Start with a profile whose nickname is "Ada". Send an empty update and verify that the nickname stays "Ada". Then send {"nickname": null} and verify that the nickname clears. If both requests have the same effect, check whether the client omits null values during serialization or the server treats a missing key and an explicit null the same way.
Keep fixtures for the missing key, explicit null, valid string and wrong type. Run them against the request schema whenever the API contract changes.
Validate API payloads locally on macOS
See how to check a payload against a schema and inspect errors at the affected field in the JSON Schema tool guide.