fix: primitive() and enum validation now correctly handle null#32
Merged
Conversation
The primitive() schema guard relied on typeof checks via Generics.Primitives.includes(typeof arg), but typeof null === 'object' not 'null', so null was never matched despite being in PrimitiveType. Same bug existed in validateDefault for both the primitive and enum cases where the typeof check was used as an early rejection filter. Fixes #31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #31
The
primitive()schema guard andvalidateDefaultboth usedGenerics.Primitives.includes(typeof arg)to check if a value is a primitive. Sincetypeof null === "object"(not"null"),nullwas never matched despite being part of thePrimitiveTypeunion.Changes
Root cause fix —
src/validators/schema/primitive.tsAdded explicit
arg === nullcheck before thetypeofcheck in the guard:This mirrors the pattern used in
asNull.tswhich already checksarg === nulldirectly.Same structural bug in validator —
src/validators/schema/helpers/validate/validateDefault.tsThe
validateDefaultInnerfunction had the sametypeof-only check in two places:primitivecase (line 68) — Addedctx.arg !== null &&guard before thetypeofcheckenumcase (line 189) — Same fix applied. This was an additional bug with the same root cause:asEnumschemas containingnullvalues would fail validation because the enum case invalidateDefaultrejects non-typeof-matchable values before checking enum members.Note: The
asEnum.tsguard itself delegates toprimitive(), so it is automatically fixed by theprimitive.tschange. The separate fix was only needed invalidateDefault.ts.Tests —
src/validators/__tests__/primitive.spec.tsReplaced the skipped stub with a full test suite covering:
null(the core bug)optional()variantvalidator()method existenceReproduction (before fix)