Skip to content

Test: add tests for empty enum validation#870

Open
Vaibhav701161 wants to merge 1 commit intojson-schema-org:mainfrom
Vaibhav701161:test/empty-enum
Open

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

Conversation

@Vaibhav701161
Copy link

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

Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
@Vaibhav701161 Vaibhav701161 requested a review from a team as a code owner March 9, 2026 15:00
@jviotti
Copy link
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
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
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
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
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
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
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?

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.

3 participants