Skip to content

fix: handle null values in enum allowedValues - #30

Merged
Gdewilde merged 2 commits into
mainfrom
fix/null-enum-tostring
Mar 28, 2026
Merged

fix: handle null values in enum allowedValues#30
Gdewilde merged 2 commits into
mainfrom
fix/null-enum-tostring

Conversation

@Gdewilde

Copy link
Copy Markdown
Contributor

Summary

  • Fix TypeError: Cannot read properties of null (reading 'toString') when allowedValues contains null
  • Replace value.toString() with String(value ?? '') to safely handle nullable enums

Problem

When a JSON Schema defines a nullable enum (e.g., enum: ['draft', 'posted', null]), AJV includes null in error.params.allowedValues. The error formatter calls .toString() on each value, which crashes on null.

This is common in OpenAPI 3.0 specs where fields are both nullable: true and have an enum constraint.

Test plan

  • Added test case: should not crash when allowedValues contains null
  • All 17 tests pass

🤖 Generated with Claude Code

Gdewilde and others added 2 commits March 28, 2026 12:25
When a schema defines a nullable enum (e.g., `enum: ['one', 'two', null]`),
the `allowedValues` array contains `null`. Calling `null.toString()` throws
a TypeError, crashing the error formatter.

Replace `.toString()` with `String(value ?? '')` to safely handle null
values in enum suggestions.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

size-limit report 📦

Path Size
dist/better-ajv-errors.cjs.production.min.js 1.92 KB (+0.16% 🔺)
dist/better-ajv-errors.esm.js 2.08 KB (+0.05% 🔺)

@Gdewilde
Gdewilde merged commit 0508712 into main Mar 28, 2026
3 checks passed
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