These are chat archives for dry-rb/chat

28th
Jun 2018
sarahdiendorfer
@sarahdiendorfer
Jun 28 2018 13:48

Hello everyone!

I’m trying to delete the value of some model attributes by sending null as value in the update request.
When I send null for a top-level attribute, everything is fine and I get the key with value nil back.
But when I want to nullify a nested attribute, I don’t get the key back at all from dry.
You can try it out with this snippet.
Is this expected behaviour or a bug?
Do you have any suggestions/hacks for how I can fix this in my code? - Every hint is appreciated.
Thanks a lot in advance!

Mikhail Slyusarev
@mikhailvs
Jun 28 2018 14:50
Any way I can get my PR approved? dry-rb/dry-rb.org#252
Gustavo Caso
@GustavoCaso
Jun 28 2018 14:59
Hi @sarahdiendorfer
can you try with this
schema = ::Dry::Validation.JSON do
  configure { config.type_specs = true }

  optional(:first_name, :string).maybe(:str?)
  optional(:last_name, :string).filled(:str?)

  optional(:address).schema do
    required(:city).filled(:str?)
    required(:zip).filled(:int?)
  end
end

puts schema.call(first_name: nil, last_name: "Smith", address: nil).output
# => {:first_name=>nil, :last_name=>"Smith", :address=>nil}
@mikhailvs I find your PR valuable but I do have the last word
Mikhail Slyusarev
@mikhailvs
Jun 28 2018 15:04
I'm totally open to feedback. I'm only adding a thing that confused me while I was adding the lib
sarahdiendorfer
@sarahdiendorfer
Jun 28 2018 15:32
Hi @GustavoCaso
thanks for you quick reply.
I tried out your suggestion, and the output is correct, but i also get errors:
errors={:address=>["must be a hash"]}that's why it's not working for me
Gustavo Caso
@GustavoCaso
Jun 28 2018 16:25
Hi @sarahdiendorfer I think that is the expected behaviour for schema
sorry if I can not be of much help. I haven't use in depth dry-validation
Mikhail Slyusarev
@mikhailvs
Jun 28 2018 16:36
@sarahdiendorfer try this:
schema = ::Dry::Validation.JSON do
  configure { config.type_specs = true }

  optional(:first_name, :string).maybe(:str?)
  optional(:last_name, :string).filled(:str?)

  optional(:address, [:nil]).maybe do
    schema do
      required(:city).filled(:str?)
      required(:zip).filled(:int?)
    end
  end
end
Gustavo Caso
@GustavoCaso
Jun 28 2018 16:37
It works @mikhailvs :tada:
Thanks for helping
Mikhail Slyusarev
@mikhailvs
Jun 28 2018 16:40
Unclear if by doing this it implies a [:nil, :hash] in that second parameter (because of the schema call), so maybe add that in explicitly.
Silas Baronda
@silasb
Jun 28 2018 16:52
Hey guys/gals, I'm working on parsing my current schema rules and generating validate.js JSON file. Has anyone tried this before or does anyone have any code that parses the Dry::Validation schema and do something with it?
Piotr Solnica
@solnic
Jun 28 2018 17:02
should be optional(:address, [:nil, :hash]).schema { ... }
Mikhail Slyusarev
@mikhailvs
Jun 28 2018 17:08
@solnic it doesn't seem to work if you dooptional(:address, [:nil, :hash]).schema
docs also say "If a nested hash could be nil, simply use maybe macro with a block"
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 17:53
Hello. Any chance to reach @flash-gordon here?)
Nikita Shilnikov
@flash-gordon
Jun 28 2018 17:58
@andriytyurnikov guess so
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:03
Nice to see you @flash-gordon ) Being impressed with your implementation of Dry::Monads::Validated, I hope to have your feedback and recommendations about particular approach to customization of dry-operations.
basically, instead of creating feature request and having endless arguments, I am looking for a way to create alternative implementation of dry-operation, which will enforce structure on input and output data, and would provide error messages
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:06
thanks, what's dry-operation? :sweat_smile:
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:07
sorry, dry-transaction, reading too much about trailblaizer :)
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:09
likely, you'll want dry-validation for this. Validated is kind of low-level, I didn't try to have long validation checks with it
but it's a common pattern in scala or haskell I guess
also, performance will likely suffer
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:10
I am focusing on function on current stage
and with your experience with Dry::Monads::Validated, what would you suggest for implementation of Failure which would be able to have freeform error(messages) datastructure associated with it?
Like Failure({some_hash: {to: {merge: "with other"}}})
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:16
If you're using List you don't need to use hash. List will collect all Invalid(...) objects
it's a bit tricky to explain
although it will take some time if you're unfamiliar with scala
tldr I would go with something like Invalid(invalid_email: params[:email])
then it would become Failure(List[ { invalid_email: 'foo@bar' }, { invalid_password: '123' } ])
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:19
errorMessage looks like a key feature here, as examples at http://dry-rb.org/gems/dry-monads/1.0/validated/ do not show if Invalid is capable to carry configurable message
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:19
it's not, not a goal of Validated
it will never become more than it is
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:20
Wow, so Dry::Validated is already capable of doing that, but it is not documented? Or I'll have to create my variation?
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:21
no no no, no messages :)
use dry-validation instead
Validated is merely a tool for overcoming shortcomings of Result when you need to validate something and return all errors rather than only first
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:22
Which is awesome
But we need more verbose errors
I'll try to hack error message into Invalid
:D
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:23
you don't need to "hack" it, you can create your own Invalid method that will translate messages, I'll show in a sec
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:28
and this is not a hack :)
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:32
I see, what I am saying that practical scenario we facing is that error branch of railway-oriented programming have to carry data too.
Structures, collections, strings, dates and numbers of error data.
Nikita Shilnikov
@flash-gordon
Jun 28 2018 18:33
yeah, probably you can use Validated then
you can reduce the resulting list easily
just add another method to MyValidation for this
call it Validate or whatever, pass all validations there, grab the reduced result
that's what I would do
Andriy Tyurnikov
@andriytyurnikov
Jun 28 2018 18:37
Thank you very much