Skip to content

feat: display expected constant for const validation errors (#162) - #340

Open
utkarshpatwa393-sys wants to merge 1 commit into
atlassian:mainfrom
utkarshpatwa393-sys:feat/const-error-display-162
Open

utkarshpatwa393-sys wants to merge 1 commit into
atlassian:mainfrom
utkarshpatwa393-sys:feat/const-error-display-162

Conversation

@utkarshpatwa393-sys

Copy link
Copy Markdown

Summary

Resolves #162.

When an Ajv schema specifies const: "foo" and validation fails on a mismatch, Ajv provides params: { allowedValue: "foo" }. Previously, better-ajv-errors did not handle the const keyword specifically, falling back to DefaultValidationError which output generic text: const must be equal to constant without showing what the expected constant value was.

Changes

  1. Types (src/types.ts):
    • Added ConstParams type ({ allowedValue: unknown }).
  2. Utils & Helpers (src/utils.ts, src/helpers.ts):
    • Added isConstError type guard.
    • Grouped const errors into enum bucket and routed them through EnumValidationError with allowedValues: [error.params.allowedValue].
  3. Enum Validation Error (src/validation-errors/enum.ts):
    • Distinguishes const keyword to render CONST header (instead of ENUM).
    • Displays CONST must be equal to constant (<allowedValue>) and provides fallback pointer message Unexpected value, should be equal to constant <allowedValue> or Levenshtein suggestion if closest match found.
  4. Testing & Changeset:
    • Added unit tests in src/__tests__/index.ts verifying CLI and JS formatted error outputs.
    • Added changeset in .changeset/const-error-display.md.

…n#162)

Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
@changeset-bot

changeset-bot Bot commented Nov 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7411b46

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@utkarshpatwa393-sys

Copy link
Copy Markdown
Author

@torifat Hi! Resolves #162 by displaying the expected constant value for const validation errors along with suggestions and code frames. All 82 tests, build, lint, and changeset passing. Ready for your review whenever convenient!

This branch has not been deployed

No deployments
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.

Show actual incorrect constant rather than "const must be equal to constant"

1 participant