Skip to content

Add minContains and maxContains from draft-2019 - #248

Open
kayodeo1 wants to merge 1 commit into
horejsek:masterfrom
kayodeo1:draft2019-min-max-contains
Open

kayodeo1 wants to merge 1 commit into
horejsek:masterfrom
kayodeo1:draft2019-min-max-contains

Conversation

@kayodeo1

Copy link
Copy Markdown

Part of #81 (draft 2019-09 support): adds the minContains and maxContains keywords.

In draft 2019-09, minContains (default 1) and maxContains (default unlimited) bound how many array items must be valid against contains (validation spec, section 6.4.4–6.4.5). Until now both keywords were ignored:

fastjsonschema.validate({'$schema': 'https://json-schema.org/draft/2019-09/schema',
                         'contains': {'type': 'number'}, 'maxContains': 1}, [1, 2])
# before: passes; after: JsonSchemaValueException: data must contain at most 1 items valid against contains definition

fastjsonschema.validate({'$schema': 'https://json-schema.org/draft/2019-09/schema',
                         'contains': {'type': 'string'}, 'minContains': 0}, [])
# before: JsonSchemaValueException (contains requires one match); after: passes

Changes:

  • CodeGeneratorDraft2019.generate_contains: when minContains or maxContains is set, the generated code counts the items valid against contains and checks both bounds (rule='minContains' / rule='maxContains'). contains: true counts every item, and contains: false counts none. Plain contains (no bounds, or minContains: 1 only) still uses the existing draft-06 code, which stops at the first match.
  • Both keywords must be non-negative integers (2.0 is accepted, as in the suite); otherwise JsonSchemaDefinitionException. Booleans are rejected.
  • tests/json_schema/test_draft2019.py: minContains.json and maxContains.json are removed from the ignored suite files. Without the change, 16 of their 40 cases fail.
  • tests/test_array.py: tests for both bounds, contains: true/false with bounds, minContains/maxContains of 0, and invalid definitions.
  • CHANGELOG.txt: one line under UNRELEASED.

Test run (pytest -m "not benchmark", as in tox -e py, with the JSON-Schema-Test-Suite submodule at the pinned commit 9fc880bf), Python 3.13:

  • before: 5615 passed, 12 deselected, 351 xfailed, 829 xpassed
  • after: 5670 passed, 12 deselected, 335 xfailed, 805 xpassed (0 failed)

pylint fastjsonschema reports the same messages as on master; none are in the new code.

The change and the tests were prepared with an AI assistant (Claude Code with Claude Opus 5.5).

I used an AI assistant to help prepare this change.

In draft 2019-09, minContains (default 1) and maxContains (default
unlimited) bound how many array items must be valid against contains.
They were ignored, so for example {"contains": {"type": "number"},
"maxContains": 1} accepted [1, 2], and minContains: 0 did not make an
array without matching items valid.

The draft-2019 generator now counts the matching items when either
keyword is set and checks both bounds; plain contains keeps the existing
short-circuit code. Both keywords must be non-negative integers. The
suite files minContains.json and maxContains.json are no longer ignored.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 14:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants