1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
---
title: Built-in Types
layout: gem-single
name: dry-types
---
Built-in types are grouped under 6 categories:
- `nominal` - base type definitions with a primitive class and options
- `strict` - constrained types with a primitive type check applied to input
- `coercible` - types with constructors using kernel coercions
- `params` - types with constructors performing non-strict coercions specific to HTTP parameters
- `json` - types with constructors performing non-strict coercions specific to JSON
- `maybe` - types accepting either nil or a specific primitive type
### Categories
Assuming you included `Dry::Types` ([see instructions](docs::getting-started)) in a module called `Types`:
* Nominal types:
- `Types::Nominal::Any`
- `Types::Nominal::Nil`
- `Types::Nominal::Symbol`
- `Types::Nominal::Class`
- `Types::Nominal::True`
- `Types::Nominal::False`
- `Types::Nominal::Bool`
- `Types::Nominal::Integer`
- `Types::Nominal::Float`
- `Types::Nominal::Decimal`
- `Types::Nominal::String`
- `Types::Nominal::Date`
- `Types::Nominal::DateTime`
- `Types::Nominal::Time`
- `Types::Nominal::Array`
- `Types::Nominal::Hash`
* `Strict` types will raise an error if passed a value of the wrong type:
- `Types::Strict::Nil`
- `Types::Strict::Symbol`
- `Types::Strict::Class`
- `Types::Strict::True`
- `Types::Strict::False`
- `Types::Strict::Bool`
- `Types::Strict::Integer`
- `Types::Strict::Float`
- `Types::Strict::Decimal`
- `Types::Strict::String`
- `Types::Strict::Date`
- `Types::Strict::DateTime`
- `Types::Strict::Time`
- `Types::Strict::Array`
- `Types::Strict::Hash`
> All types in the `strict` category are [constrained](docs::constraints) by a type-check that is applied to make sure that the input is an instance of the primitive:
``` ruby
Types::Strict::Integer[1] # => 1
Types::Strict::Integer['1'] # => raises Dry::Types::ConstraintError
```
* `Coercible` types will attempt to cast values to the correct class using kernel coercion methods:
- `Types::Coercible::String`
- `Types::Coercible::Integer`
- `Types::Coercible::Float`
- `Types::Coercible::Decimal`
- `Types::Coercible::Array`
- `Types::Coercible::Hash`
* Types suitable for `Params` param processing with coercions:
- `Types::Params::Nil`
- `Types::Params::Date`
- `Types::Params::DateTime`
- `Types::Params::Time`
- `Types::Params::True`
- `Types::Params::False`
- `Types::Params::Bool`
- `Types::Params::Integer`
- `Types::Params::Float`
- `Types::Params::Decimal`
- `Types::Params::Array`
- `Types::Params::Hash`
* Types suitable for `JSON` processing with coercions:
- `Types::JSON::Nil`
- `Types::JSON::Date`
- `Types::JSON::DateTime`
- `Types::JSON::Time`
- `Types::JSON::Decimal`
- `Types::JSON::Array`
- `Types::JSON::Hash`
* `Maybe` strict types:
- `Types::Maybe::Strict::Class`
- `Types::Maybe::Strict::String`
- `Types::Maybe::Strict::Symbol`
- `Types::Maybe::Strict::True`
- `Types::Maybe::Strict::False`
- `Types::Maybe::Strict::Integer`
- `Types::Maybe::Strict::Float`
- `Types::Maybe::Strict::Decimal`
- `Types::Maybe::Strict::Date`
- `Types::Maybe::Strict::DateTime`
- `Types::Maybe::Strict::Time`
- `Types::Maybe::Strict::Array`
- `Types::Maybe::Strict::Hash`
* `Maybe` coercible types:
- `Types::Maybe::Coercible::String`
- `Types::Maybe::Coercible::Integer`
- `Types::Maybe::Coercible::Float`
- `Types::Maybe::Coercible::Decimal`
- `Types::Maybe::Coercible::Array`
- `Types::Maybe::Coercible::Hash`
> `Maybe` types are not available by default - they must be loaded using `Dry::Types.load_extensions(:maybe)`. See [Maybe extension](docs::extensions/maybe) for more information.
|