Skip to content

Test: add tests for empty enum validation#870

Merged
jdesrosiers merged 1 commit intojson-schema-org:mainfrom
Vaibhav701161:test/empty-enum
Mar 15, 2026
Merged

Test: add tests for empty enum validation#870
jdesrosiers merged 1 commit intojson-schema-org:mainfrom
Vaibhav701161:test/empty-enum

Conversation

@Vaibhav701161
Copy link
Copy Markdown
Contributor

Summary

This PR adds test cases covering the behaviour of:

{ "enum": [] }

The tests are added for the following drafts:

  • draft6
  • draft7
  • draft2019-09
  • draft2020-12
  • v1

Background

Starting from Draft 6, the JSON Schema validation specification relaxed the requirement that enum must contain at least one element.
Earlier drafts required this with a MUST requirement, but from Draft 6 onward this was changed to SHOULD (as defined in RFC 2119).

The draft 2020-12 validation meta-schema defines enum as:

"enum": { "type": "array", "items": true }

Since there is no minItems constraint, the meta-schema allows an empty array as a valid schema value.

However, the current test suite does not contain any tests that exercise the case where enum is an empty array.

Interpretation

The tests added in this PR assume that an empty enum matches no instances.

The validation rule states:

An instance validates successfully if its value is equal to one of the elements of the array.

If the array is empty, there is no element that can match the instance value.
Therefore, the tests expect all instances to be invalid.

If the intended behaviour is different, the expected results in the tests can be adjusted accordingly.

Files Changed

The following files are updated:

  • tests/draft6/enum.json
  • tests/draft7/enum.json
  • tests/draft2019-09/enum.json
  • tests/draft2020-12/enum.json
  • tests/v1/enum.json

Draft 3 is not included because the specification explicitly required enum to contain at least one element.

Motivation

This case came up while implementing a lint rule in the sourcemeta/core project that detects empty enum values.
For reference : sourcemeta/core#2287
Since this behaviour was not currently covered in the test suite, adding these tests helps clarify the expected validator behaviour.

cc @jviotti

@Vaibhav701161 Vaibhav701161 requested a review from a team as a code owner March 9, 2026 15:00
@jviotti
Copy link
Copy Markdown
Member

jviotti commented Mar 9, 2026

@jdesrosiers @gregsdennis and overall @json-schema-org/tsc is this the right reading of an empty enum? Came up while working on my linter, wanted to make sure we are all in the same page, and encouraged @Vaibhav701161 to kickstart the discussion. Thoughts?

@gregsdennis
Copy link
Copy Markdown
Member

There are several tests that validate a schema against (a $ref to) the meta-schema to ensure that certain things are allowed. I think this falls into that category as well.

@jviotti
Copy link
Copy Markdown
Member

jviotti commented Mar 9, 2026

@gregsdennis The point here is to assert what happens if you validate any value against a schema where enum is empty. I don't think any existing tests covers that. Or at least not any that I could find?

@gregsdennis
Copy link
Copy Markdown
Member

Yes, it does that. However we should also test that empty enum is valid. Maybe these do that implicitly, but other places have a more direct test.

@jviotti
Copy link
Copy Markdown
Member

jviotti commented Mar 9, 2026

@gregsdennis Can you point me to it? By doing searches on either 2020-12 metaschemas or tests, I don't see any empty enumeration being exercised.

@gregsdennis
Copy link
Copy Markdown
Member

I'm not saying that enum is being tested anywhere else. I'm saying that the metaschema is used elsewhere to test keyword values.

@jviotti
Copy link
Copy Markdown
Member

jviotti commented Mar 9, 2026

Ah, I see. @Vaibhav701161 maybe worth searching for what @gregsdennis is referring to and adding a test confirming enum: [] validates against the meta-schemas?

@Vaibhav701161
Copy link
Copy Markdown
Contributor Author

Thanks for the clarification.

I’ll look for the existing tests that validate keyword values against the meta-schemas and add a similar test confirming that { "enum": [] } is accepted by the meta-schema as a valid schema and update the PR shortly once I locate the appropriate place in the test suite for that.

@Vaibhav701161
Copy link
Copy Markdown
Contributor Author

@jviotti I've added additional tests confirming that { "enum": [] } is accepted by the corresponding meta-schemas.

Copy link
Copy Markdown
Member

@jviotti jviotti left a comment

Choose a reason for hiding this comment

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

Awesome!

Comment thread tests/draft2019-09/defs.json Outdated
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
@jdesrosiers jdesrosiers merged commit c7fc509 into json-schema-org:main Mar 15, 2026
3 checks passed
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.

4 participants