Skip to content

feat: add fieldName property to validation error objects#57

Merged
herzog31 merged 4 commits intoadobe:mainfrom
gsriram24:feature/fieldname
Feb 2, 2026
Merged

feat: add fieldName property to validation error objects#57
herzog31 merged 4 commits intoadobe:mainfrom
gsriram24:feature/fieldname

Conversation

@gsriram24
Copy link
Copy Markdown
Contributor

@gsriram24 gsriram24 commented Jan 7, 2026

Description

This PR adds a fieldNames property to validation error objects, enabling consumers to programmatically identify which field caused a validation error without parsing the issueMessage string.

Changes:

Core (src/types/base.js):

  • required() — returns fieldNames: [name] for missing/invalid type errors
  • recommended() — returns fieldNames: [name] for missing/invalid type warnings
  • or() — returns fieldNames (all fields) for compound conditions

Custom validators:

  • BreadcrumbList.jsfieldNames for item count and URL validation errors
  • DefinedRegion.jsfieldNames for addressRegion/postalCode conflict
  • MerchantReturnPolicy.jsfieldNames for country/link validation
  • Product.jsfieldNames for rating/review/offers and notes count errors
  • ProductMerchant.jsfieldNames for GTIN validation
  • Rating.jsfieldNames for out-of-range rating errors
  • schemaOrg.jsfieldNames for unsupported property errors

Tests:

  • Added base.test.js with 10 new tests covering fieldNames behavior

Related Issue

Fixes #55

Motivation and Context

Currently, the field name is embedded in the issueMessage string:

{ issueMessage: 'Required attribute "price" is missing', ... }

Consumers must use regex to extract the field name, which is fragile and breaks if message wording changes. This PR adds a dedicated fieldName property for direct programmatic access, enabling use cases like:

  • Highlighting specific form fields in structured data editors
  • Building field-level error mappings without string parsing
  • Future-proofing against message wording or i18n changes

How Has This Been Tested?

  • All 211 existing tests pass
  • Added 10 new tests in base.test.js covering:
    • required() returning fieldNames for missing attributes
    • required() returning fieldNames for invalid type
    • recommended() returning fieldNames for missing/invalid attributes
    • or() returning fieldNames for compound conditions
    • Nested path fieldNames (e.g., offers.price)
npm run unit-test
# 211 passing

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

- Add fieldName to required(), recommended(), or() in base.js
- Add fieldName to custom validators (BreadcrumbList, DefinedRegion,
  MerchantReturnPolicy, Product, ProductMerchant, Rating, schemaOrg)
- Add tests for fieldName in base.test.js

Fixes adobe#55
@herzog31 herzog31 requested review from herzog31 and noruiz January 28, 2026 10:06
@herzog31 herzog31 added the enhancement New feature or request label Jan 28, 2026
Comment thread src/types/DefinedRegion.js Outdated
Copy link
Copy Markdown
Member

@herzog31 herzog31 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution 💯

Could you please also ensure that the changes related to fieldName made to

  • BreadcrumbList.js
  • DefinedRegion.js
  • MerchantReturnPolicy.js
  • Product.js
  • ProductMerchant.js
  • Rating.js
  • schemaOrg.js
    are also covered by unit tests? Extending existing tests is fine.

gsriram24 and others added 2 commits February 2, 2026 08:40
Replace fieldName (singular) with fieldNames (array) for consistency.
All validation errors now return fieldNames as an array, eliminating
redundancy and providing a cleaner API.

- Single field errors: fieldNames: ['price']
- Multi-field errors: fieldNames: ['aggregateRating', 'offers', 'review']

Access primary field with error.fieldNames[0] or iterate over all fields.

Co-authored-by: Cursor <cursoragent@cursor.com>
Add fieldNames coverage to unit tests for:
- BreadcrumbList.test.js
- DefinedRegion.test.js
- MerchantReturnPolicy.test.js
- Product.test.js
- ProductMerchant.test.js
- Rating.test.js
- schemaOrg.test.js

This ensures the fieldNames property is verified in each validator's
test file, not just in base.test.js.

Co-authored-by: Cursor <cursoragent@cursor.com>
@gsriram24
Copy link
Copy Markdown
Contributor Author

@herzog31, updated the test files to assert for fieldNames too!

@herzog31 herzog31 merged commit fd63c8b into adobe:main Feb 2, 2026
2 checks passed
@herzog31
Copy link
Copy Markdown
Member

herzog31 commented Feb 2, 2026

Thanks a lot @gsriram24 for the contribution.

github-actions Bot pushed a commit that referenced this pull request Feb 2, 2026
# [1.6.0](v1.5.0...v1.6.0) (2026-02-02)

### Features

* add fieldName property to validation error objects ([#57](#57)) ([fd63c8b](fd63c8b)), closes [#55](#55)
* Adjust formatting and README for fieldNames ([bd5098e](bd5098e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add fieldName property to validation error objects

3 participants