"must have required property": which property, where, and why
Last checked and updated
The schema at the place the error names lists a property under required, and the value does not have it. The message (the Ajv validator's wording) names the property in quotes; the pointer says which object lacks it.
Reading it
Item:
type: object
required: [name, status, quantity]
$ curl -X POST http://127.0.0.1:4632/items \
-H 'content-type: application/json' -d '{"name":"a"}'
HTTP/1.1 422 Unprocessable Entity
{"errors":[
{"in":"body","pointer":"","message":"must have required property 'status'"},
{"in":"body","pointer":"","message":"must have required property 'quantity'"}],
"type":"https://apigit.com/mock/problems/invalid-request",
"title":"The request does not match the API description","status":422, ...}
One line per missing property. pointer is the object that should contain it: the empty pointer is the body itself, /address would mean address.status is missing.
Present is not the same as non-null
required only asks for the key. Three different schemas:
| Schema | {} | {"note": null} | {"note": "x"} |
|---|---|---|---|
note required, type string | invalid | invalid | valid |
note required, type string or null | invalid | valid | valid |
note not required, type string | valid | invalid | valid |
A client that drops null fields before sending (some serialisers do, or are configured to) turns a valid body into one with a missing required property.
A wall of these under oneOf or anyOf
When the schema is a oneOf or anyOf, the validator reports why each alternative failed:
{"errors":[
{"in":"body","pointer":"","message":"must have required property 'number'"},
{"in":"body","pointer":"","message":"must have required property 'iban'"},
{"in":"body","pointer":"/type","message":"must be equal to constant"},
{"in":"body","pointer":"","message":"must match exactly one schema in oneOf"}], ...}
Nobody is asking for both number and iban. Pick the alternative you meant and fix that one's line; the last line explains the rest: "must match exactly one schema in oneOf".
When the schema is at fault
| Situation | Fix |
|---|---|
A server-generated field (id, createdAt) is required and the same schema is used for the request | Mark it readOnly, or use a separate request schema |
required names a property that properties does not define (a typo: required: [stauts]) | Every object fails; correct the name |
required is written inside a property instead of on the object (name: { type: string, required: true }) | That is the syntax of a parameter (required: true); in a schema, required is a list on the parent object |
| A PATCH operation reuses the schema of POST | Partial updates need a schema without required |