Skip to content

v9.3.0

Latest
Compare
Choose a tag to compare
@DZakh DZakh released this 18 Feb 08:03
· 2 commits to main since this release

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 to string 🔄
  • from string to literal string, boolean, number, bigint null, undefined, NaN 🔄
  • from string to boolean 🔄
  • from string to int32 🔄
  • from string to number 🔄
  • from string to bigint 🔄
  • from int32 to number

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