README.md in dry-data-0.3.2 vs README.md in dry-data-0.4.0
- old
+ new
@@ -144,11 +144,11 @@
```
You can define your own optional types too:
``` ruby
-maybe_string = Dry::Data["optional"] | Dry::Data["string"]
+maybe_string = Dry::Data["string"].optional
maybe_string[nil]
# => None
maybe_string[nil].fmap(&:upcase)
@@ -162,19 +162,37 @@
maybe_string['something'].fmap(&:upcase).value
# => "SOMETHING"
```
+### Sum-types
+
+You can specify sum types using `|` operator, it is an explicit way of defining
+what are the valid types of a value.
+
+In example `dry-data` defines `bool` type which is a sum-type consisting of `true`
+and `false` types which is expressed as `Dry::Data['true'] | Dry::Data['false']`
+(and it has its strict version, too).
+
+Another common case is defining that something can be either `nil` or something else:
+
+``` ruby
+nil_or_string = Dry::Data['strict.nil'] | Dry::Data['strict.string']
+
+nil_or_string[nil] # => nil
+nil_or_string["hello"] # => "hello"
+```
+
### Constrained Types
You can create constrained types that will use validation rules to check if the
input is not violating any of the configured contraints. You can treat it as
a lower level guarantee that you're not instantiating objects that are broken.
All types support constraints API, but not all constraints are suitable for a
particular primitive, it's up to you to set up constraints that make sense.
-Under the hood it uses `dry-validation`[https://github.com/dryrb/dry-validation]
+Under the hood it uses [`dry-validation`](https://github.com/dryrb/dry-validation)
and all of its predicates are supported.
IMPORTANT: `dry-data` does not have a runtime dependency on `dry-validation` so
if you want to use contrained types you need to add it to your Gemfile