Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only resolve "strip()" for schema when used as an object field #1977

Merged
merged 1 commit into from
Apr 12, 2023

Conversation

jquense
Copy link
Owner

@jquense jquense commented Apr 12, 2023

Schema with strip() resolve to never types so that they are removed from the object type, but this also meant they resolved to never even outside of an object which isn't accurate since strip only has an effect for object properties. This also meant that Schema types were harder to use b/c never is like the anti any and always resulted in schema that were incompatible with each other even if they were.

string().strip().cast() // string | undefined

object({
   unused: string().strip()
}).cast()  // {}

Now the strip flag is only affects the output type when used in an object shape, which means that you can do things like the following without issue:

export function yup<T>(schema: AnySchema<T>) {
  return (value: unknown) => schema.validateSync(value);
}

yup(string()); 
yup(string().default('')); 

fixes #1974

```
string().strip().cast() // string | undefined

object({
   unused: string().strip()
}).cast()  // {}
```
@jquense jquense merged commit 2ba1104 into master Apr 12, 2023
@jquense jquense deleted the better-strip-types branch April 12, 2023 14:36
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.

Result of Schema.default is not assignable to Schema
1 participant