Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Tim Riley
@timriley
@apotonick if I don't have a new baby at start of the work, I'll see if I can take a quick stab at a strategy for you
Nick Sutterer
@apotonick
@timriley :heart_eyes: so, i would basically not implement define_initializer, right?
my initializer looks like
def initialize(params, options)
  @options = options #this is the dependency hash
end
so i kinda want to avoid the overriding from Ai
because we don't need separate instance variables per dependency, i store them all in one hash
@yuriidanyliak what does Schema#output do?
Yurii Danyliak
@yuriidanyliak
output's result of validation, those data that passed the validation of schema
@apotonick, usually using like
validation = schema.call(params)
validation.success? ? validation.output : validation.messages
Tim Riley
@timriley
Yeah, looks like you could skip defining the initializer, @apotonick
Nick Sutterer
@apotonick
@timriley i'll play with it now and let ya know
@yuriidanyliak ok, but what's the meaning of "output" in this specific case?
Fran Worley
@fran-worley
@apotonick output gives you pack your params
Nick Sutterer
@apotonick
i see
so basically form.sync
Fran Worley
@fran-worley
Not its used after validation and just literally returns params. More like form.to_nested_hash
Nick Sutterer
@apotonick
here we go
Fran Worley
@fran-worley
:) it's a dry-validation method that we don't use in reform.
Piotr Solnica
@solnic
Please use Result#to_h instead of output
Ivo Benedito
@ivobenedito
hey guys!
starting to try to replace my Virtus addiction by dry-types 😜
I have a domain object that maps a remote API resource object ... and for example some of the attributes are DateTime instances
& for example, the responsability of coercion for DateTime values should be done outside of the object itself?
Ivo Benedito
@ivobenedito
just asking, because if I run something like: class User < Dry::Struct; attribute :created_at, Types::DateTime; end; User.new(created_at: "2016-10-06T10:03:03.785Z")
it won't coerce anything and will just put a string on the object state
Piotr Solnica
@solnic
@ivobenedito use dry-validation schema to process data before passing it to your structs
Ivo Benedito
@ivobenedito
@solnic I'm really interested in shifting to dry-rb, but I'm finding it to be a little hard to read and find docs / blog posts about how to connect all this sub-modules together
Piotr Solnica
@solnic
@ivobenedito just apply schema validation to api input, and pass result to struct constructor, there’s no need to “connect” anything
User.new(schema.call(some_data_from_api)) <= just this
of course validation can fail so you may want to handle this however you want
Ivo Benedito
@ivobenedito
yeah I've understood that, it's a great tip thx
but that forces the schema to be defined twice somehow, right?
on the struct, and on the schema definition
and the schema definition contains validation logic, also
and how will a DateTime coercion/parsing will be done on a Schema object :/
Piotr Solnica
@solnic
@ivobenedito use Dry::Validation.JSON and add type expections, like date_time? etc. it will infer coercions from that and try to apply them
I’d do something like this:
UserSchema = Dry::Validation.JSON do
  required(:created_at).value(:date_time?)
end

class User < Dry::Struct
  attribute :created_at, Types::Strict::DateTime
end

user_attrs = UserSchema.(user_data)

User.new(user_attrs)
in 1.0.0 dry-validation will support infering schemas from struct definitions, for now you need to have it defined explicitly in both places
Ivo Benedito
@ivobenedito
and that JSON validation enforces the coercion? cool
Screen Shot 2016-10-14 at 17.33.33.png
this is the service object where I'm trying to inject that Schema logic you're talking about
so that you understand the context a little better
Piotr Solnica
@solnic
@ivobenedito aah so the structure is different (camel-cases keys)
anyhow, yes it will enforce coercions
Ivo Benedito
@ivobenedito
yah ... camel-cased but I don't want to make the case switch automatically
Piotr Solnica
@solnic
why do you have a guard for an empty string? is it an allowed value?
Ivo Benedito
@ivobenedito
the external API is returning unexpected values in some cases
let's say ... users that never logged in, the last_sign_in_at datetime doesn't exist
don't even know if I just tell the API owners to returning an empty field or just skip the field on the response
Piotr Solnica
@solnic
yeah this is exactly why I recommend using schemas instead of just blindly passing input to domain structs