Repository navigation
Conversation
Values set to undefined previously crashed the generated serializer
with a TypeError (e.g. Cannot read properties of undefined (reading
'toString')) for string and object types, or threw a conversion error
for integer, number and date-time formats.
Every position that already coerces null to the falsy value of the
declared type now treats undefined identically: "" for strings and
the date/date-time/time formats, 0 for integer and number, {} for
object, [] for array, false for boolean (unchanged), and null for
nullable and null-typed positions. Object properties set to undefined
are still skipped.
This branch has not been deployed
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.
Fixes #680
Summary
Serialize
undefinedthe same way asnull: coerce it to the falsy value of the declared type instead of throwing. The reported case is['test1', 'test2', 'test3', undefined]withitems: { type: 'string' }, which threwTypeError: Cannot read properties of undefined (reading 'toString'). Objects,integer,numberand the date formats threw in the same positions.Each
=== nullcoercion guard in the generated code inindex.jsis now== null. Inlib/serializer.js,asIntegerandasNumberget a nullish early return, and the date serializers use== null. This follows the review of #681: "all functions need to conform to this behavior (nullish values get coerced to the falsy value of the corresponding type)".Unchanged:
undefinedare still skipped, as inJSON.stringify({ a: undefined }). A required property set toundefinedstill throws.anyOf/oneOfstill throw the schema mismatch forundefined, because no branch can be chosen.format: 'unsafe'.One output change for input that did not throw before: a
nullable: trueobject schema with no properties now givesnullforundefinedinstead of{}, the same as fornull.The alternative is to emit
nullforundefinedarray items, asJSON.stringify([undefined])does. I did not take it because this library already coercesnullto the type's falsy value, and #681 settled on one rule for both. The README "Nullable" section only describesnull. I can addundefinedthere if you want it documented.The new tests in
test/missing-values.test.jsfail without the source change and pass with it, except the cases that pin existing behaviour.Checklist
npm run testandnpm run benchmarkand the Code of conduct