ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
7.02k stars 224 forks source link

No longer possible to coerce values to `undefined` #1125

Closed benj-dobs closed 2 years ago

benj-dobs commented 2 years ago

If I return undefined from a coercion, the coercion is ignored completely, and the original value is kept (even if the original value does not satisfy the struct we're coercing the value into).

The most common use case in my project is:

const coercableOptional = <T>(struct: Struct<T>) =>
  coerce(optional(struct), literal(null), () => undefined);

This no longer works: null values are retained, even if struct should not allow null values.

I think this was caused by #1106

Is there an alternative syntax for coercing to undefined, or is this a bug that needs fixing?

benj-dobs commented 2 years ago

I'm more and more convinced this is a bug, and I've raised a pull request to fix it. Let me know if I've got that wrong :)