Skip to content

"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 stringinvalidinvalidvalid
note required, type string or nullinvalidvalidvalid
note not required, type stringvalidinvalidvalid

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

SituationFix
A server-generated field (id, createdAt) is required and the same schema is used for the requestMark 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 POSTPartial updates need a schema without required

Sources

Related pages

Try it with your own API

Create a free workspace, add an OpenAPI file, and publish its documentation and a mock server from the same repository.

Free for individuals. No credit card.

$ npm install -g @apigit/cli$ apigit auth login$ apigit doc publish