New Feature
S.coerce
(S.t<'from>, S.t<'to>) => S.t<'to>
This very powerful API allows you to coerce another data type in a declarative way. Let's say you receive a number that is passed to your system as a string. For this S.coerce
is the best fit:
let schema = S.string->S.coerce(S.float)
"123"->S.parseOrThrow(schema) //? 123.
"abc"->S.parseOrThrow(schema) //? throws: Failed parsing at root. Reason: Expected number, received "abc"
// Reverse works correctly as well 🔥
123.->S.reverseConvertOrThrow(schema) //? "123"
Currently, ReScript Schema supports the following coercions (🔄 means reverse support):
- from
string
tostring
🔄 - from
string
to literalstring
,boolean
,number
,bigint
null
,undefined
,NaN
🔄 - from
string
toboolean
🔄 - from
string
toint32
🔄 - from
string
tonumber
🔄 - from
string
tobigint
🔄 - from
int32
tonumber
There are plans to add more support in future versions and make it extensible.
Other Changes
- Renamed
S.to
->S.shape
.S.to
was deprecated and will be removed in v10. - Exposed
S.shape
to JS/TS API - Removed "reverse" from the error message text
Full Changelog: v9.2.3...v9.3.0