json-schema: Incorrect behavior of strict vs. default modes

The behavior of strict mode is a bit misleading (and incorrect IMHO). JSON schema provides required already but in the default execution of the json-schema validator it ignores this property. The expected behavior is that default validation checks that any keys present in the document being validated match the definition in the schema and that all keys listed as required in the schema are present. Strict mode would then make a bit more sense to change all keys to required though I’d argue that’s a bit misleading as well and strict mode should really just imply that there are no keys present in the validation target that are not defined in the schema as well. Specifying that all keys present in the schema are required really should be a separate option since this is a different validation case (perhaps require_all: true?).

In summary I propose:

Default validation:

  • Invalid unless all keys in the input document match the definition in the schema.
  • Invalid unless all keys listed as required in the schema are present.
  • Keys present in the input document but not present in the schema are ignored (i.e don’t affect validation).

Strict validation:

  • Default rules + invalid if any keys are present in the input document but not defined in the schema.

Require all:

  • Strict validation + invalid unless all keys defined in the schema are present in the input document.

I’m happy to take a stab at a patch if I can get some consensus that this is the right thing to do.

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

I was thinking of a slightly different idea ; When strict: true offer the possibility to the users to “override” the default behavior enforced by the strict mode

Example : with strict: true all property are by default "required": true ; but if the user set "optional": true or "required": false if override the behavior. The same if the user set "additionalProperties": true

I could try and dig in to the code to see if I can make a PR out of this idea

WDYT ?