10BC0 feat: add support for optional parent with mandatory nested propertie… by Devesh36 · Pull Request #1341 · express-validator/express-validator · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@Devesh36
Copy link

Description

Fixed optional parent field validation in schema validation

When using checkSchema(), nested properties were being validated even when their optional parent field was missing from the request. This fix ensures that nested properties are only validated when their parent field is actually present.

Changes Made

  • Modified src/chain/context-runner-impl.ts to track which fields are marked as optional
  • Added filtering logic to skip validation of nested fields when their parent is optional and missing from the request
  • Stores optional field information on the request object during validation

Problem Solved

Before: { a: {} } would fail validation even though a was marked as optional
After: { a: {} } correctly fails only when a exists, ensuring nested mandatory properties are validated appropriately

Test Results

  • Added new test case covering all scenarios
  • All 312 tests passing
  • 100% code coverage maintained

@Devesh36
Copy link
Author
Devesh36 commented Nov 7, 2025

please review this @gustavohenke :)

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.

1 participant

0