gems dry-types 1.0.0

latest releases: 1.8.2, 1.8.1, 1.8.0...
5 years ago

1.0.0 2019-04-23

Changed

  • [BREAKING] Behavior of built-in constructor types was changed to be more strict. They will always raise an error on failed coercion (flash-gordon)
    Compare:

    # 0.15.0
    Types::Params::Integer.('foo')
    # => "foo"
    
    # 1.0.0
    Types::Params::Integer.('foo')
    # => Dry::Types::CoercionError: invalid value for Integer(): "foo"

    To handle coercion errors Type#call now yields a block:

    Types::Params::Integer.('foo') { :invalid } # => :invalid

    This makes work with coercions more straightforward and way faster.

  • [BREAKING] Safe types were renamed to Lax, this name better serves their purpose. The previous name is available but prints a warning (flash-gordon)

  • [BREAKING] Metadata is now pushed down to the decorated type. It is not likely you will notice a difference but this a breaking change that enables some use cases in rom related to the usage of default types in relations (flash-gordon)

  • Nominal types are now completely unconstrained. This fixes some inconsistencies when using them with constraints. Nominal#try will always return a successful result, for the previous behavior use Nominal#try_coerce or switch to strict types with passing a block to #call (flash-gordon)

Performance improvements

  • During the work on this release, a lot of performance improvements were made. dry-types 1.0 combined with dry-logic 1.0 are multiple times faster than dry-types 0.15 and dry-logic 0.5 for common cases including constraints checking and coercion (flash-gordon)

Added

  • API for custom constructor types was enhanced. If you pass your own callable to .constructor it can have a block in its signature. If a block is passed, you must call it on failed coercion, otherwise raise a type coercion error (flash-gordon)
    Example:
    proc do |input, &block|
      if input.is_a? String
        Integer(input, 10)
      else
        Integer(input)
      end
    rescue ArgumentError, TypeError => error
      if block
        block.call
      else
        raise Dry::Types::CoercionError.new(
          error.message,
          backtrace: error.backtrace
        )
      end
    end
    This makes the exception handling your job so that dry-types won't have to catch and re-wrap all possible errors (this is not safe, generally speaking).
  • Types now can be converted to procs thus you can pass them as blocks (flash-gordon)
    %w(1 2 3).map(&Types::Coercible::Integer)
    # => [1, 2, 3]

Compare v0.15.0...v1.0.0

Don't miss a new dry-types release

NewReleases is sending notifications on new releases.