Comments (4)
We'll address this, related issue is dry-rb/dry-types#194
from dry-struct.
Some entities has a lot of optional fields, that I don't want to specify.
I can use constructor_type :schema, but it allows to 'forget' required key.
I can use strit_with_defaults and .default(nil) to all my optional fields, but Types::Strict::String.optional.default(nil) is too much as for me.
irb(main):023:0> s = Class.new(Dry::Struct) { attribute :foo, Dry::Types['string'].optional }
=> #<Class:0x007f9abf989cf0>
irb(main):024:0> s.new
=> #<#<Class:0x007f9abf989cf0> foo=nil>
irb(main):025:0> s.new(foo: nil)
=> #<#<Class:0x007f9abf989cf0> foo=nil>
irb(main):026:0> s.new(foo: "bar")
=> #<#<Class:0x007f9abf989cf0> foo="bar">
Is this what you need?
I use default attributes defined in Entity because: a) they are often business rules b) it gives encapsulation.
What's the source of data for your entities?
Moreover, strict_with_defaults doesn't accept nil value for attributes with default value.
We do not use nil
as an indicator that a default value should be set. "optional" means "nil or something else", so Types::Strict::String.optional
is just a shortcut for Types::Strict::Nil | Types::Strict::String
, which means if you pass a nil
, then nil
will be set as the value.
I use default attributes defined in Entity because: a) they are often business rules b) it gives encapsulation. Application still may send nil value for that attribute, the most obvious example is FactoryGirl who builds the object. And it's still desirable for Entity to set default value.
FactoryGirl, or any test-related use cases, this is not really relevant. Testing needs tools dedicated to testing. We have rom-factory which can build structs and we can add support for setting all attributes to nil
by default.
P.S. Does it make sense to consider .optional attribute as one which has .default(nil)?
No, as I mentioned above, optional
means that nil
is allowed to be passed as a value.
from dry-struct.
@solnic I just ran into this same issue, where some models have 'optional' attributes that are not set immediately on creation. It would be preferable to just have optional
attributes be optional in the input hash entirely rather than only optional
in the sense that they can be passed with nil
values.
from dry-struct.
This was addressed, see #64
from dry-struct.
Related Issues (20)
- Does not create an accessor method when method exists into the mother class HOT 5
- Error while using optional on Dry::Struct HOT 4
- Sum operator not working when default values are defined HOT 4
- Document how sum types works with structs HOT 4
- Instance method name attribute not available HOT 1
- Compose transforms HOT 2
- Subclasses don't recognize properly when attribute described as Types::Array.of(Parent)
- Missing attribute and maybe type HOT 4
- Expanding array item type in generated schema HOT 2
- `define_accessors` eval block consumes nonsensical values
- MissingAttributeError when access optional field with hash syntax
- Assigning nil to attribute returns hash if attribute struct has optional attibutes only HOT 3
- Errors in structs nested inside a Types::Hash raise as `#<NoMethodError: undefined method 'message' for #<String` instead of Dry Errors with the actual message HOT 1
- Struct with optional nested struct requires key in nested struct HOT 2
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- Dry::Struct by including module HOT 1
- Release 1.5.0 breaks rails app boot with eager-loading HOT 8
- Nested optional struct does not inherit `transform_keys(&:to_sym)` HOT 3
- `Dry::Types::ConstraintError` not propagated when originating from inside a `Dry::Types::Map`
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-struct.