Skip to content

How do I migrate to v1 #1906

@jquense

Description

@jquense

Please read through the releases: https://github.com/jquense/yup/releases which document the breaking changes in more detail but the tl;dr; here is the following:

Big additions

The tuple type, object omit, pick, partial and deepPartial better TS support, most consistent behavior and

Breaking Changes

Flat bundle

There is no distributed lib folder anymore everything is bundled into one file with rollup

Stricter when API

#1542

tl;dr; use functions for then/otherwise conditions. convert .when({ to: true, then: yup.string().required() }) to .when({ to: true, then: schema => schema.required() })

Email validation is looser

Email validation via a regexp is impossible to do 100% correctly rather than try to match every case yup now matches the WHATWG definition of a valid email address to align with how browsers validate (or are supposed to validate) email inputs since that is a common use case.

We are not accepting changes to this at this time. If the built in validation is not sufficient for your use-case (totally reasonable!) please implement a regex or logic that you prefer, either with a custom method, or override the string email method via addMethod

Nullability and optionality are stricter and enforced during cast

This is the largest, and likely most disruptive change. Prior yup allowed for patterns like:

const nullableRequiredString = string().nullable().required()

nullableRequiredString.cast(null) // -> null

nullableRequiredString.validate(null) // ValidationError("this is required and cannot be null")

This may seem unintuitive behavior (and it is) but allowed for a common client side validation case, where we want to use a single schema to parse server data, as well as validate user input. In other words, a server might return invalid "default" values that should still fail when trying to submit.

Now, nullable(), defined and required are all mutually dependent methods. Meaning string().nullable().defined().required() produces a schema where the value must be a string, and not null or undefined. The effect of this is that the type of a cast() is now accurate and the same as the type returned from validate.

There is a migration path for folks who use this pattern: name.cast(null, { assert: 'ignore-optionality'}) // => null read more here

There are some knock-on changes caused by this as well, due to internals changing. Specifically describe() now returns optional and nullable properties instead of required to distinguish between the two possible states clearly. There is also no longer a test name required for most schema:

-const isRequired = schema.describe().tests.some(test => test.name === 'required')
+const description = schema.describe()
+const isRequired = !description.optional && !description.nullable

TS generics are faster, more accurate and very different from v032.

SchemaOf<Person> has been removed, and you can now provide the type you want the schema to match: ObjectSchema<Person>. read more: here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions