Skip to content

v9.3.0

Choose a tag to compare

@DZakh DZakh released this 18 Feb 08:03
· 284 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