Comments (5)
The only way to add custom types is to use public type definition API. It's a new thing, ie:
class Email
end
# shortcut which assumes `Email.method(:new)` is the constructor
Dry::Types.register_class(Email)
# this expands to:
type = Dry::Types::Definition.new(Email).constructor(Email.method(:new))
Dry::Types.register('email', type)
from dry-types.
Thanks for reporting this. I'd say it's a bug. I'll address this in the current refactoring.
from dry-types.
I had a problem that led me to this and it's basically that I cannot make the output of a type be a valid input for itself given this situation. This had the issue that I would take an object and coerce it at the entry of an HTTP call and then would pass that data to a series of steps that in the end get coerced again with the exact same coercer at the exit point. This is used for consistency so that we guarantee the format in both directions.
I solved the problem by defining a new type Nothing
that can be nil
or None
:
require 'kleisli'
module Types
class Nothing
extend Dry::Data::TypeBuilder
class << self
def call(input)
input.nil? ? Kleisli::Maybe::None.new : input
end
def try(input)
call(input)
end
def valid?(input)
#binding.pry
input.nil? || input.is_a?(Kleisli::Maybe::None)
end
alias_method :[], :call
end
end
end
Dry::Data.register "types.nothing", Types::Nothing
class Kleisli::Maybe::None
def to_json
nil.to_json
end
end
class Kleisli::Maybe::Some
def to_json
value.to_json
end
end
Then instead of using #optional
I created sum types by hand, e.g.: OptionalString = Nothing | String
and this solved all my issues. I wanted to share this because maybe it can help the conversation by showing why another type besides Nil
could be needed.
from dry-types.
I'm not sure this is the best way to create custom typed, but it's what I managed to figure out by reading the code since there were no docs about building custom types/coercers without recurring to sums or fmap.
from dry-types.
@solnic thanks for the PR and for the help with custom types!
from dry-types.
Related Issues (20)
- Types::Params::Integer incorrect coercion for numeric values HOT 9
- Types::Params::*.optional not handling empty string HOT 4
- Anonymous Dry::Types based module cannot be extended HOT 4
- Params namespace disapearing HOT 9
- Params coercion for Array works in an unintuitive way HOT 1
- https://dry-rb.org/gems/dry-types/ not at latest version HOT 1
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- Compound Types with Examples HOT 1
- Dry::Container::Error: There is already an item registered with the key "nominal.string" on jruby-head HOT 4
- Future-proof local override for Types::Instance/Constructor classes that potentially collide with predicates
- Unexpected error raised from Sum type HOT 7
- Types::Coeercible::Integer, should this use Base 10? HOT 2
- Add Zeitwerk autoloader
- Using transform_keys breaks error handling
- Undefined method after upgrading dependencies. HOT 5
- Missing method implementation for `Enum.each_value` HOT 1
- Types.Constructor failing on Ruby 3.1 HOT 8
- A bug with array, struct and sum of structs in `try` method
- Fix BigDemical warning
- default causes try/1 to raise an ConstraintError exception rather than an Failure
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dry-types.