Dry::Types::Hash#try
returnsFailure
instead of throwing an exception on missing keys (GustavoCaso)
Defaults#valid?
now works fine when passingDry::Core::Constans::Undefined
as value (GustavoCaso)valid?
for constructor types wrappingSum
s (GustavoCaso)
- Defaults now works fine with meta (GustavoCaso)
- Defaults are now re-decorated properly (flash-gordon)
params.int
was added to make the upgrade process in dry-validation smoother (available after yourequire 'dry/types/compat/int'
) (flash-gordon)
- [BREAKING] Renamed
Types::Form
toTypes::Params
. You can opt-in the former name withrequire 'dry/types/compat/form_types'
. It will be dropped in the next release (ndrluis) - [BREAKING] The
Int
types was renamed toInteger
, this was the only type named differently from the standard Ruby classes so it has been made consistent. The former name is available withrequire 'dry/types/compat/int'
(GustavoCaso + flash-gordon) - [BREAKING] Default types are not evaluated on
nil
. Default values are evaluated only if no value were given.This change allowed to greatly simplify hash schemas, make them a lot more flexible yet predictable (see below).type = Types::Strict::String.default("hello") type[nil] # => constraint error type[] # => "hello"
- [BREAKING]
Dry::Types.register_class
was removed,Dry::Types.register
was made private API, do not register your types in the globaldry-types
container, use a module instead, e.g.Types
(flash-gordon) - [BREAKING] Enum types don't accept value index anymore. Instead, explicit mapping is supported, see below (flash-gordon)
-
Hash schemas were rewritten. The old API is still around but is going to be deprecated and removed before 1.0. The new API is simpler and more flexible. Instead of having a bunch of predefined schemas you can build your own by combining the following methods:
Schema#with_key_transform
—transforms keys of input hashes, for things like symbolizing etc.Schema#strict
—makes a schema intolerant to unknown keys.Hash#with_type_transform
—transforms member types with an arbitrary block. For instance,
optional_keys = Types::Hash.with_type_transform { |t, _key| t.optional } schema = optional_keys.schema(name: 'strict.string', age: 'strict.int') schema.(name: "Jane", age: nil) # => {name: "Jane", age: nil}
Note that by default all keys are required, if a key is expected to be absent, add to the corresponding type's meta
omittable: true
:intolerant = Types::Hash.schema(name: Types::Strict::String) intolerant[{}] # => Dry::Types::MissingKeyError tolerant = Types::Hash.schema(name: Types::Strict::String.meta(omittable: true)) tolerant[{}] # => {} tolerant_with_default = Types::Hash.schema(name: Types::Strict::String.meta(omittable: true).default("John")) tolerant[{}] # => {name: "John"}
The new API is composable in a natural way:
TOLERANT = Types::Hash.with_type_transform { |t| t.meta(omittable: true) }.freeze user = TOLERANT.schema(name: 'strict.string', age: 'strict.int') user.(name: "Jane") # => {name: "Jane"} TOLERANT_SYMBOLIZED = TOLERANT.with_key_transform(&:to_sym) user_sym = TOLERANT_SYMBOLIZED.schema(name: 'strict.string', age: 'strict.int') user_sym.("name" => "Jane") # => {name: "Jane"}
(flash-gordon)
-
Types.Strict
is an alias forTypes.Instance
(flash-gordon)strict_range = Types.Strict(Range) strict_range == Types.Instance(Range) # => true
-
Enum#include?
is an alias toEnum#valid?
(d-Pixie + flash-gordon) -
Range
was added (GustavoCaso) -
Array
types filter outUndefined
values, if you have an array type with a constructor type as its member, the constructor now can returnDry::Types::Undefined
to indicate empty value:filter_empty_strings = Types::Strict::Array.of( Types::Strict::String.constructor { |input| input.to_s.yield_self { |s| s.empty? ? Dry::Types::Undefined : s } } ) filter_empty_strings.(["John", nil, "", "Jane"]) # => ["John", "Jane"]
-
Types::Map
was added for homogeneous hashes, when only types of keys and values are known in advance, not specific key names (fledman + flash-gordon)int_to_string = Types::Hash.map('strict.integer', 'strict.string') int_to_string[0 => 'foo'] # => { 0 => "foo" } int_to_string[0 => 1] # Dry::Types::MapError: input value 1 for key 0 is invalid: type?(String, 1)
-
Enum supports mappings (bolshakov + flash-gordon)
dict = Types::Strict::String.enum('draft' => 0, 'published' => 10, 'archived' => 20) dict['published'] # => 'published' dict[10] # => 'published'
- Fixed applying constraints to optional type, i.e.
.optional.constrained
works correctly (flash-gordon) - Fixed enum working with optionals (flash-gordon)
- Dropped the
dry-configurable
dependency (GustavoCaso) - The gem now uses
dry-inflector
for inflections instead ofinflecto
(GustavoCaso)
- The type compiler was fixed for simple rules such as used for strict type checks (flash-gordon)
- Fixed an error on
Dry::Types['json.decimal'].try(nil)
(nesaulov) - Fixed an error on calling
try
on an array type built of constrained types (flash-gordon) - Implemented
===
for enum types (GustavoCaso)
Constructor#try
rescuesArgumentError
(raised in cases likeInteger('foo')
) (flash-gordon)#constructor
works correctly for default and enum types (solnic)- Optional sum types work correctly in
safe
mode (GustavoCaso) - The equalizer of constrained types respects meta (flash-gordon)
- A bunch of shortcut methods for constructing types to the autogenerated module, e.g.
Types.Constructor(String, &:to_s)
(flash-gordon)
Types::Array#member
was deprecated in favor ofTypes::Array#of
(flash-gordon)
- Constructors are now equalized using
fn
andmeta
too (flash-gordon)
- Fixed
Constructor#name
withSum
-types (flash-gordon)
#to_ast
available for all type objects (GustavoCaso)Types::Array#of
as an alias for#member
(maliqq)- Detailed failure objects are passed to results which improves constraint violation messages (GustavoCaso)
- Callable defaults accept the underlying type (v-kolesnikov)
- Fixed
Type#optional?
for sum types (flash-gordon)
Type#optional?
returns true if type is Sum and left is nil (GustavoCaso)Type#pristine
returns a type withoutmeta
(flash-gordon)
meta
is used in type equality again (solnic)Any
works correctly with meta again (flash-gordon)
- Types can be used in
case
statements now (GustavoCaso)
- Return original value when Date.parse raises a RangeError (jviney)
- Meta data are now stored separately from options (flash-gordon)
Types::Object
was renamed toTypes::Any
(flash-gordon)
- Added
Types::Object
which passes an object of any type (flash-gordon)
- Updated to dry-core >= 0.2.1 (ruby warnings are gone) (flash-gordon)
- Support for
"Y"
and"N"
astrue
andfalse
values, respectively (scare21410)
- Optimized object allocation in hash schemas, resulting in up to 25% speed boost (davydovanton)
Hash#strict_with_defaults
properly evaluates callable defaults (bolshakov)
Hash#weak
accepts Hash-descendants again (solnic)
Hash#strict_with_defaults
which validates presence of all required keys and respects default types for missing values (backus)Type#constrained?
method (flash-gordon)
- Summing two constrained types works correctly (flash-gordon)
Types::Array::Member#valid?
in cases where member type is a constraint (solnic)Hash::Schema#try
handles exceptions properly and returns a failure object (solnic)
- [BREAKING] Renamed
Hash##{schema=>permissive}
(backus) - [BREAKING]
dry-monads
dependency was made optional, Maybe types are available afterDry::Types.load_extensions(:maybe)
(flash-gordon) - [BREAKING]
Dry::Types::Struct
andDry::Types::Value
have been extracted todry-struct
(backus) Types::Form::Bool
supports upcased true/false values (kirs)Types::Form::{Date,DateTime,Time}
fail gracefully for invalid input (padde)- ice_nine dependency has been dropped as it was required by Struct only (flash-gordon)
- Compiler no longer chokes on type nodes without args (solnic)
- Removed
bin/console
from gem package (solnic)
Struct
now implementsType
interface so ieSomeStruct | String
works now (flash-gordon):weak
Hash constructor which can partially coerce a hash even when it includes invalid values (solnic)- Types include
Dry::Equalizer
now (flash-gordon)
Struct#to_hash
descends into arrays too (nepalez)Default#with
works now (flash-gordon)
:symbolized
hash schema is now based on:weak
schema (solnic)Struct::Value
instances are now deeply frozen via ice_nine (backus)
Bool#default
gladly acceptsfalse
as its value (solnic)- Creating an empty schema with input processor no longer fails (lasseebert)
- Allow multiple calls to meta (solnic)
- Allow capitalised versions of true and false values for boolean coercions (nil0bject)
- Replace kleisli with dry-monads (flash-gordon)
- Use coercions from Kernel (flash-gordon)
- Decimal coercions now work with Float (flash-gordon)
- Coerce empty strings in form posts to blank arrays and hashes (timriley)
- update to use dry-logic v0.2.3 (fran-worley)
JSON::*
types with JSON-specific coercions (coop)
- Schema is properly inherited in Struct (backus)
constructor_type
is properly inherited in Struct (fbernier)
Major focus of this release is to make complex type composition possible and improving constraint errors to be more meaningful.
Type#try
interface that tries to process the input and return a result object which can be either a success or failure (solnic)#meta
interface for setting arbitrary meta data on types (solnic)ConstraintError
has a message which includes information about the predicate which failed ienil violates constraints (type?(String) failed)
(solnic)Struct
usesDry::Equalizer
too, just likeValue
(AMHOL)Sum::Constrained
which has a disjunction rule built from its types (solnic)- Compiler supports
[:constructor, [primitive, fn_proc]]
nodes (solnic) - Compiler supports building schema-less
form.hash
types (solnic)
Sum
now supports complex types likeArray
orHash
with member types and/or constraints (solnic)Default#constrained
will properly wrap a new constrained type (solnic)
- [BREAKING] Renamed
Type#{optional=>maybe}
(AMHOL) - [BREAKING]
Type#optional(other)
builds a sum:Strict::Nil | other
(AMHOL) - [BREAKING] Type objects are now frozen (solnic)
- [BREAKING]
Value
instances are frozen (AMHOL) Array
is no longer a constructor and has aArray::Member
subclass (solnic)Hash
is no longer a constructor and is split intoHash::Safe
,Hash::Strict
andHash::Symbolized
(solnic)Constrained
has now aConstrained::Coercible
subclass which will try to apply its type prior applying its rule (solnic)#maybe
usesStrict::Nil
now (solnic)Type#default
will raise ifnil
was passed forMaybe
type (solnic)Hash
with a schema will set maybe values for missing keys or nils (flash-gordon)
Renamed from dry-data
to dry-types
and:
Dry::Types.module
which returns a namespace for inclusion which has all built-in types defined as constants (solnic)Hash#schema
supports default values now (solnic)Hash#symbolized
passes through keys that are already symbols (solnic)Struct.new
uses an empty hash by default as input (solnic)Struct.constructor_type
macro can be used to change attributes constructor (solnic)default
accepts a block now for dynamic values (solnic)Types.register_class
accepts a second arg which is the name of the class' constructor method, defaults to:new
(solnic)
Struct
will simply pass-through the input if it is already a struct (solnic)default
will raise if a value violates constraints (solnic)- Evaluating a default value tries to use type's constructor which makes it work with types that may coerce an input into nil (solnic)
enum
works just fine with integer-values (solnic)enum
+default
works just fine (solnic)Optional
no longer responds toprimitive
as it makes no sense since there's no single primitive for an optional value (solnic)Optional
passes-through a value which is already a maybe (solnic)
Dry::Types::Definition
is now the base type definition object (solnic)Dry::Types::Constructor
is now a type definition with a constructor function (solnic)
Dry::Data::Type#safe
for types which can skip constructor when primitive does not match input's class (solnic)form.array
andform.hash
safe types (solnic)
Type#default
interface for defining a type with a default value (solnic)
- [BREAKING]
Dry::Data::Type.new
accepts constructor and options now (solnic) - Renamed
Dry::Data::Type::{Enum,Constrained}
=>Dry::Data::{Enum,Constrained}
(solnic) dry-logic
is now a dependency for constrained types (solnic)- Constrained types are now always available (solnic)
strict.*
category uses constrained types with:type?
predicate (solnic)SumType#call
no longer needs to rescue fromTypeError
(solnic)
attribute
raises proper error when type definition is missing (solnic)
- Support for arrays in type compiler (solnic)
- Array member uses type objects now rather than just their constructors (solnic)
- Support for sum-types with constraint type (solnic)
Dry::Data::Type#optional
for defining optional types (solnic)
Dry::Data['optional']
was removed in favor ofDry::Data::Type#optional
(solnic)
Dry::Data::Value
which works like a struct but is a value object with equalizer (solnic)
- Added missing require for
dry-equalizer
(solnic)
- Removed require of constrained type and make it optional (solnic)
Type#constrained
interface for defining constrained types (solnic)Dry::Data
can be configured with a type namespace (solnic)Dry::Data.finalize
can be used to define types as constants under configured namespace (solnic)Dry::Data::Type#enum
for defining an enum from a specific type (solnic)- New types:
symbol
andclass
along with theirstrict
versions (solnic)
- Type compiler supports nested hashes now (solnic)
form.bool
sum is using correct right-sideform.false
type (solnic)
- Improved structure of the ast (solnic)
form.nil
which coerces empty strings tonil
(solnic)bool
sum-type (true | false) (solnic)- Type compiler supports sum-types now (solnic)
- Constructing optional types uses the new
Dry::Data["optional"]
built-in type (solnic)
form.*
coercible types (solnic)Type::Hash#strict
for defining hashes with a strict schema (solnic)Type::Hash#symbolized
for defining hashes that will symbolize keys (solnic)Dry::Data.register_class
short-cut interface for registering a class and setting its.new
method as the constructor (solnic)Dry::Data::Compiler
for building a type from a simple ast (solnic)
First public release