| Old syntax | New syntax |
|---|---|
stringType |
string()1 |
numberType |
number()1 |
booleanType |
boolean()1 |
dateType |
date()1 |
oneOfType('foo', 'bar') |
union(['foo', 'bar'])1 |
stringType('') |
string().default('')12 |
stringType(throwable) |
string().defined()12 |
arrayOfType(stringType) |
string().array()/string().defined().array()13 |
arrayOfType(stringType('')) |
string().default('').array()124 |
arrayOfType(stringType(throwable)) |
string().defined().array()1245 |
arrayOfType(/* */)([]) |
N/A6 |
arrayOfType(/* */)(throwable) |
N/A6 |
For additional info, check out Advanced examples in the README.
Footnotes
-
With new syntax, for state fields, the value (or its parts) is not stringified or parsed (i.e. only validated). ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10
-
With old syntax, custom types could technically allow
undefined, in which casethrowableand a fallback value wouldn't be used for absent values. With new syntax,undefinedcan't be returned if.default()or.defined()are used. ↩ ↩2 ↩3 ↩4 -
With old syntax, any invalid item fails the whole array, and that error is replaced with undefined. There is no equivalent new syntax, but
string().array()should be more useful. It always returns an array, and invalid items are replaced withundefined(as ofv2.0.0,undefinedvalues are omitted). If you need to fail the whole array, the closest new syntax isstring().defined().array(), which throws an error if any item is absent or invalid (though it's not very useful). ↩ -
This old syntax is unintuitive and unintentional, and therefore shouldn't be used at all. ↩
-
With new syntax, there are no tweaks on the array level because it's always defined and the only way to throw an error is to do it on the item level. ↩ ↩2