Beta 5 fixes partial
and deepPartial
making it work correctly with lazy
schema. Specifically the optionality is added after lazy is evaluated but before any other when
conditions are added. This makes it consistent with other conditional schema, where runtime conditions always supersede previous schema configuration. This allows for optional overrides if necessary.
const person = object({
name: string().required(),
age: number().required(),
legalGuardian: string().when('age', {
is: (age) => age != null && age < 18,
then: (schema) => schema.required(),
}),
});
const optionalPerson = person.partial()
person.cast({name: 'James', age: 6 }) // => TypeError legalGuardian required
// age is still required b/c it's applied after the `partial`
optionalPerson.cast({name: 'James', age: 6 }) // => TypeError legalGuardian required
This works slightly differently for lazy
which have no schema to "start" with:
const config = object({
nameOrIdNumber: lazy((value) => {
if (typeof value === 'number') return number().required()
return string().required()
}),
});
const opti = config.partial()
config.cast({}) // => TypeError nameOrIdNumber is required
config.partial().cast({}) // => {}
Cast optionality migration path
A larger breaking change in v1 is the assertion of optionality during cast
, making previous patterns like string().nullable().required()
no longer possible. Generally this pattern is used when deserialized data is not valid to start, but will become valid through user input such as with an HTML form. v1 no longer allows this, but in order to make migration easier we've added an option to cast
that mimics the previous behavior (not exactly but closely).
const name = string().required()
name.cast(null, { assert: 'ignore-optionality'}) // => null
We recommend updating your schema to new patterns where possible but this allows for incremental upgrades
What's Changed
- chore(deps): update all non-major dependencies by @renovate in #1630
- chore(deps): update all non-major dependencies by @renovate in #1652
- fix(docs): correct typo "coarce" to "coerce" by @eunicode in #1654
- chore(deps): update all non-major dependencies by @renovate in #1656
- chore(deps): update all non-major dependencies by @renovate in #1665
- Capitalize two words in README by @Glitchy-Tozier in #1676
- Fix typo by @karlhorky in #1672
- Show example of function message by @karlhorky in #1674
- chore(deps): update all non-major dependencies by @renovate in #1678
- Fix typo: coarce -> coerce by @karlhorky in #1677
- chore(deps): update all non-major dependencies by @renovate in #1685
- chore(deps): update all non-major dependencies by @renovate in #1692
- chore(deps): update all non-major dependencies by @renovate in #1697
- chore(deps): update all non-major dependencies by @renovate in #1699
- chore(deps): update all non-major dependencies by @renovate in #1709
- chore(deps): update all non-major dependencies by @renovate in #1714
- Fix Typo : delete duplicate word "passed" by @ANTARES-KOR in #1696
- chore(deps): update all non-major dependencies by @renovate in #1722
- chore(deps): update all non-major dependencies by @renovate in #1727
- chore(deps): update all non-major dependencies by @renovate in #1731
- chore(deps): update all non-major dependencies by @renovate in #1737
- chore(deps): update all non-major dependencies by @renovate in #1744
- small typo fix by @somodis in #1745
- feat: better Lazy types and deepPartial fixes by @jquense in #1748
- feat: add cast nullability migration path. by @jquense in #1749
New Contributors
- @eunicode made their first contribution in #1654
- @Glitchy-Tozier made their first contribution in #1676
- @karlhorky made their first contribution in #1672
- @ANTARES-KOR made their first contribution in #1696
- @somodis made their first contribution in #1745
Full Changelog: v1.0.0-beta.4...v1.0.0-beta.5