ajv icon indicating copy to clipboard operation
ajv copied to clipboard

Fail object validation on additional properties

Open slavafomin opened this issue 2 years ago • 1 comments

Hello!

Thank you for this great library!

However, I can't really find a way to make Ajv to fail validation when encountering additional properties in objects. The only related option I can find is removeAdditional, but it just silently ignores the extra properties.

The existence of the unexpected properties in a JSON configuration file or an API request is often a sign of a mistake of the document author. The safer behavior is to detect such cases and fail the document validation, before it could lead to problems. This ensures better DX.

Here's the config option in other validation libraries:

  • zod — strict https://github.com/colinhacks/zod#strict

  • joi — allowUnknown https://joi.dev/api/?v=17.9.1#anyvalidatevalue-options

  • yup — noUnknown https://github.com/jquense/yup#objectnounknownonlyknownkeys-boolean--true-message-string--function-schema

Am I missing something obvious here?

Thanks!

slavafomin avatar Aug 26 '23 22:08 slavafomin

It took a lot of Googling to find this one comment about this. It definitely seems like a major flaw in the validator.

jpjpjp avatar May 26 '24 13:05 jpjpjp

I think you want additionalProperties: false https://ajv.js.org/json-schema.html#additionalproperties

jasoniangreen avatar Jul 12 '24 22:07 jasoniangreen