Skip to content

Releases: chanzuckerberg/sorbet-coerce

v0.2.2

27 Feb 04:50
fd20fa4
Compare
Choose a tag to compare
  • Support T::Hash
  • Bundle signatures for sorbet-coerce error classes

v0.2.1

23 Feb 04:26
a2b5c6e
Compare
Choose a tag to compare
  • Fix an outdated signature in rbi/sorbet-coerce.rbi

v0.2.0

21 Feb 23:53
5697cf5
Compare
Choose a tag to compare

This release adds T::Coerce::Configuration for configuring CoercionError. We have CoercionError, ShapeError, or TypeError when the coercion doesn't work successfully.

T::Coerce::CoercionError (configurable)

It raises a coercion error when it fails to convert a value into the specified type (i.e. 'bad string args' to Integer). This can be configured globally or at each call-site. When configured to true, it will fill the result with nil instead of raising the errors.

T::Coerce::Configuration.raise_coercion_error = false # default to true

We can use an inline flag to overwrite the global configuration:

T::Coerce[T.nilable(Integer)].new.from('abc', raise_coercion_error: false)
# => nil

T::Coerce::ShapeError (NOT configurable)

It raises a shape error when the shape of the input does not match the shape of input type (i.e. '1' to T::Array[Integer] or to T::Struct). This cannot be configured and always raise an error.

TypeError (configurable)

It raises a type error when the coerced input does not match the input type. This error is raised by Sorbet and can be configured through T::Configuration.

v0.1.6

07 Nov 00:17
7afaf1a
Compare
Choose a tag to compare
  • T::CoercionError becomes configurable through T::Configuration

v0.1.5

25 Oct 03:51
a56c0c6
Compare
Choose a tag to compare
  • Include the bundled rbi file and tests

v0.1.4

25 Oct 02:06
3afb2f6
Compare
Choose a tag to compare
  • Relocate the bundled rbi file so sorbet can pick it up

v0.1.3

24 Oct 16:57
3f37984
Compare
Choose a tag to compare
  • Fix the build for new sorbet version 0.4.4901
    • Handle type aliases (e.g. T::Boolean)
  • Update the encrypted API token

v0.1.2

21 Oct 18:55
Compare
Choose a tag to compare
  • Add polyfill as a runtime dependency

v0.1.1

21 Oct 17:54
89c4aff
Compare
Choose a tag to compare
  • Bug fixes (when coercing nested T::Array and union types)
  • Support T::Boolean

Initial release v0.1.0

16 Oct 17:25
fb766b7
Compare
Choose a tag to compare

This gem works with Sorbet's static type checker and type definitions. It'll return a statically-typed object or throws T::CoercionError if the coercion fails.

It provides a simple and generic way of coercing types in a sorbet-typed project. It is particularly useful when we're dealing with external API responses and controller parameters.

converted = T::Coerce[<Type>].new.from(<value>)

T.reveal_type(converted) # <Type>

The supported types include

  • Simple Types (Integer, String, etc.)
  • Custom Types: If the values can be coerced by .new
  • T.nilable(<supported type>)
  • T::Array[<supported type>]
  • Subclasses of T::Struct

We don't support

  • T::Hash (currently)
  • T.any(<supported type>, ...): A union type other than T.nilable