Custom Type Builders
It is idiomatic to construct new types based on existing.
source_type = Dry::Types['integer']
constructor_type = source_type.constructor(Kernel.method(:Integer))
-consrained_type = constructor_type.constrained(gteq: 18)
+constrained_type = constructor_type.constrained(gteq: 18)
This API can be extended with Dry::Types.define_builder
Dry::Types.define_builder(:or) { |type, value| type.fallback(value) }
diff --git a/gems/dry-types/1.7/custom-type-builders/index.html.gz b/gems/dry-types/1.7/custom-type-builders/index.html.gz
index 85a44f4c1d..a3908435aa 100644
Binary files a/gems/dry-types/1.7/custom-type-builders/index.html.gz and b/gems/dry-types/1.7/custom-type-builders/index.html.gz differ
diff --git a/gems/dry-types/1.7/custom-types/index.html.gz b/gems/dry-types/1.7/custom-types/index.html.gz
index ad06da3723..b4875d1c12 100644
Binary files a/gems/dry-types/1.7/custom-types/index.html.gz and b/gems/dry-types/1.7/custom-types/index.html.gz differ
diff --git a/gems/dry-types/1.7/default-values/index.html.gz b/gems/dry-types/1.7/default-values/index.html.gz
index 563b3415f9..a2d133dbeb 100644
Binary files a/gems/dry-types/1.7/default-values/index.html.gz and b/gems/dry-types/1.7/default-values/index.html.gz differ
diff --git a/gems/dry-types/1.7/enum/index.html.gz b/gems/dry-types/1.7/enum/index.html.gz
index ae55d06586..c2ed4e9354 100644
Binary files a/gems/dry-types/1.7/enum/index.html.gz and b/gems/dry-types/1.7/enum/index.html.gz differ
diff --git a/gems/dry-types/1.7/extensions/index.html.gz b/gems/dry-types/1.7/extensions/index.html.gz
index 615884720f..a1d62d1d01 100644
Binary files a/gems/dry-types/1.7/extensions/index.html.gz and b/gems/dry-types/1.7/extensions/index.html.gz differ
diff --git a/gems/dry-types/1.7/extensions/maybe/index.html.gz b/gems/dry-types/1.7/extensions/maybe/index.html.gz
index 8caa63f727..f839f55652 100644
Binary files a/gems/dry-types/1.7/extensions/maybe/index.html.gz and b/gems/dry-types/1.7/extensions/maybe/index.html.gz differ
diff --git a/gems/dry-types/1.7/extensions/monads/index.html.gz b/gems/dry-types/1.7/extensions/monads/index.html.gz
index 1dba52c9cd..23b1bed41e 100644
Binary files a/gems/dry-types/1.7/extensions/monads/index.html.gz and b/gems/dry-types/1.7/extensions/monads/index.html.gz differ
diff --git a/gems/dry-types/1.7/fallbacks/index.html.gz b/gems/dry-types/1.7/fallbacks/index.html.gz
index eb176af5e4..e0c0cab8a5 100644
Binary files a/gems/dry-types/1.7/fallbacks/index.html.gz and b/gems/dry-types/1.7/fallbacks/index.html.gz differ
diff --git a/gems/dry-types/1.7/getting-started/index.html.gz b/gems/dry-types/1.7/getting-started/index.html.gz
index 9556a98b67..f60c240399 100644
Binary files a/gems/dry-types/1.7/getting-started/index.html.gz and b/gems/dry-types/1.7/getting-started/index.html.gz differ
diff --git a/gems/dry-types/1.7/hash-schemas/index.html.gz b/gems/dry-types/1.7/hash-schemas/index.html.gz
index 92cc012fd0..8a86ace625 100644
Binary files a/gems/dry-types/1.7/hash-schemas/index.html.gz and b/gems/dry-types/1.7/hash-schemas/index.html.gz differ
diff --git a/gems/dry-types/1.7/index.html.gz b/gems/dry-types/1.7/index.html.gz
index 3ad842fbcb..402edec0fc 100644
Binary files a/gems/dry-types/1.7/index.html.gz and b/gems/dry-types/1.7/index.html.gz differ
diff --git a/gems/dry-types/1.7/map/index.html.gz b/gems/dry-types/1.7/map/index.html.gz
index 3deeb7af80..5992bf0f12 100644
Binary files a/gems/dry-types/1.7/map/index.html.gz and b/gems/dry-types/1.7/map/index.html.gz differ
diff --git a/gems/dry-types/1.7/optional-values/index.html.gz b/gems/dry-types/1.7/optional-values/index.html.gz
index e3efb7fe62..f978641f49 100644
Binary files a/gems/dry-types/1.7/optional-values/index.html.gz and b/gems/dry-types/1.7/optional-values/index.html.gz differ
diff --git a/gems/dry-types/1.7/sum/index.html.gz b/gems/dry-types/1.7/sum/index.html.gz
index bcc0e12d79..92d2b71cc8 100644
Binary files a/gems/dry-types/1.7/sum/index.html.gz and b/gems/dry-types/1.7/sum/index.html.gz differ
diff --git a/gems/dry-types/index.html.gz b/gems/dry-types/index.html.gz
index c54fe2b9c9..9976d5ff09 100644
Binary files a/gems/dry-types/index.html.gz and b/gems/dry-types/index.html.gz differ
diff --git a/gems/dry-types/main/array-with-member/index.html b/gems/dry-types/main/array-with-member/index.html
index 2327ae31e2..9dec56519a 100644
--- a/gems/dry-types/main/array-with-member/index.html
+++ b/gems/dry-types/main/array-with-member/index.html
@@ -3,7 +3,7 @@
indexName: 'dry-rb',
inputSelector: '#search-input',
debug: false // Set debug to true if you want to inspect the dropdown
-});Github
dry-types main
Array With Member
The built-in array type supports defining the member's type:
+});dry-types main
Array With Member
The built-in array type supports defining the member's type:
PostStatuses = Types::Array.of(Types::Coercible::String)
PostStatuses[[:foo, :bar]] # ["foo", "bar"]
diff --git a/gems/dry-types/main/array-with-member/index.html.gz b/gems/dry-types/main/array-with-member/index.html.gz
index 2a258e323b..8e1af90e36 100644
Binary files a/gems/dry-types/main/array-with-member/index.html.gz and b/gems/dry-types/main/array-with-member/index.html.gz differ
diff --git a/gems/dry-types/main/built-in-types/index.html b/gems/dry-types/main/built-in-types/index.html
index 37b2e38c36..ece1a94658 100644
--- a/gems/dry-types/main/built-in-types/index.html
+++ b/gems/dry-types/main/built-in-types/index.html
@@ -3,7 +3,7 @@
indexName: 'dry-rb',
inputSelector: '#search-input',
debug: false // Set debug to true if you want to inspect the dropdown
-});Github
dry-types main
Built-in Types
Built-in types are grouped under 6 categories:
+});dry-types main
Built-in Types
Built-in types are grouped under 6 categories:
nominal
- base type definitions with a primitive class and options
diff --git a/gems/dry-types/main/built-in-types/index.html.gz b/gems/dry-types/main/built-in-types/index.html.gz
index b13b153f6f..9cf2081dc2 100644
Binary files a/gems/dry-types/main/built-in-types/index.html.gz and b/gems/dry-types/main/built-in-types/index.html.gz differ
diff --git a/gems/dry-types/main/combining-types/index.html b/gems/dry-types/main/combining-types/index.html
new file mode 100644
index 0000000000..c24e2e7c23
--- /dev/null
+++ b/gems/dry-types/main/combining-types/index.html
@@ -0,0 +1,8 @@
+- Github
dry-types main
Combining Types
Types can be combined to create new types, using intersection types +to further restrict values, and sum types to allow more acceptable values.
+dry-types main
Intersection
Intersection types are specified using the &
operator. It combines two
+compatible types into a single one with properties from each.
One example is a Hash
that allows any keys, but requires one of them to be named id
:
Id = Types::Hash.schema(id: Types::Integer)
+HashWithId = Id & Types::Hash
+
+Id[{id: 1}] # => {:id=>1}
+Id[{id: 1, message: 'foo'}] # => {:id=>1}
+Id[{message: 'foo'}] # => Dry::Types::MissingKeyError: :id is missing in Hash input
+
+HashWithId[{ message: 'hello' }] # => Dry::Types::MissingKeyError: :id is missing in Hash input
+HashWithId[{ id: 1, message: 'hello' }] # => {:id=>1, :message=>"hello"}
+
dry-types main
Sum
You can specify sum types using |
operator, it is an explicit way of defining what the valid types of a value are.
dry-types main
Sum
You can specify sum types using |
operator, it is an explicit way of defining what the valid types of a value are.
For example dry-types
defines the Bool
type which is a sum consisting of the True
and False
types, expressed as Types::True | Types::False
.