Skip to content

fix(schema): allow empty array for required field with minItems#258

Open
spiderocious wants to merge 2 commits into
remoteoss:mainfrom
spiderocious:fix/247-empty-array-required
Open

fix(schema): allow empty array for required field with minItems#258
spiderocious wants to merge 2 commits into
remoteoss:mainfrom
spiderocious:fix/247-empty-array-required

Conversation

@spiderocious

@spiderocious spiderocious commented May 26, 2026

Copy link
Copy Markdown

Fixes a bug where a required array property fails validation when given an
empty array, even though the schema explicitly allows it via minItems: 0.
A required group-array with min: 0 could never be submitted empty.

Changes Made

  • src/validation/schema.ts

    • In the required check, an empty array is no longer automatically treated
      as a missing value when the field schema declares minItems. In that case
      the array counts as present, and its length is validated by minItems
      (which runs separately) rather than by required.
    • Behaviour is unchanged for required arrays that do not declare
      minItems: an empty array is still treated as missing, preserving existing
      behaviour.
  • test/validation/required_field.test.ts

    • Added a test that a required array with minItems: 0 accepts an empty array.
    • Added a test that a required array with minItems: 1 reports a minItems
      error (not a required error) for an empty array, confirming length is
      still enforced by the right mechanism.
    • Added a test that a required array without minItems still treats an empty
      array as missing.

Related Issues/PRs

Fixes #247

Checklist


Note

Medium Risk
Changes core required-field semantics for all required arrays (not only minItems: 0), which may alter validation for forms that relied on empty [] triggering "Required field".

Overview
Fixes validation so required on array properties no longer fails when the value is []. In schema.ts, the required-key filter used to treat empty arrays like missing fields; arrays (including empty) are now always considered present, with comments pointing authors to minItems for non-empty requirements.

Tests were updated: a required list: [] case in array field tests now expects no error, and required_field.test.ts adds coverage for empty vs populated arrays, minItems: 0 / 1, and required arrays without minItems (empty array valid; missing key still Required field).

Reviewed by Cursor Bugbot for commit 05b9fd4. Bugbot is set up for automated code reviews on this repo. Configure here.

@spiderocious spiderocious marked this pull request as ready for review May 26, 2026 12:32
@spiderocious

Copy link
Copy Markdown
Author

@sandrina-p @danielcardoso5 please review

Comment thread src/validation/schema.ts Outdated
// - it's null AND treatNullAsUndefined option is true
// - it's an array/object and it's empty
if (Array.isArray(fieldValue)) {
// By default an empty array is treated as a missing required value.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

bug: This is wrong (an existing bug). By default an empty array should NOT be treated as a missing required value. I confirmed in the jsonschemalint.com

Simple JSON Schema:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "name": { "type": "string" },
    "browser": {
      "title": "Browsers",
      "type": "array"
    }
  },
  "required": [
    "browser"
  ]
}
Image

So we should adapt the code, tests and comments for both scenarios.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review feedback, @sandrina-p , I'll address this by simplifying the check so an empty array always counts as present, and I'll update the comments and tests (including the pre-existing one in array.test.ts) to match. Pushing the fix shortly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've made the changes, please check again. @sandrina-p

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.

group-array with required parent fails for empty array, despite minItems: 0

2 participants