ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).

Home Page:https://docs.superstructjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No longer possible to coerce values to `undefined`

benj-dobs opened this issue · comments

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?

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 :)