These are chat archives for dry-rb/chat

24th
Jul 2018
Lairan
@alex-lairan
Jul 24 2018 09:51

Hi all :)

I have a transaction who call MongoDB and Redis.

What's the best way to test it?

Eduardo de Oliveira Hernandes
@eduardodeoh
Jul 24 2018 18:37
hi guys, how do i know about differences between Dry::Validation.JSON | Dry::Validation.Schema and Dry::Validation.Params, there are some docs with details about this?
Grant Shangreaux
@gcentauri
Jul 24 2018 18:39
i assume you have read this already? http://dry-rb.org/gems/dry-validation/
i can't say for sure but i believe Schema is not going to make any assumptions besides the object being validated quacking like a ruby Hash. JSON of course has a slightly different structure, where keys are always strings. It looks like it may automatically convert strings to symbols. Params is designed to work more directly with the type of structures you would get from an http request. As far as I know there isn't much other documentation besides the link above
Eduardo de Oliveira Hernandes
@eduardodeoh
Jul 24 2018 18:44
@gcentauri yeap, i see, but i only see about Dry::Validation.JSONhere: https://github.com/dry-rb/dry-validation/blob/master/examples/json.rb - but i don’t see in docs. Should i use this type of schema or only Dry::Validation.Schema. I would like to know about when use one or other.
Eduardo de Oliveira Hernandes
@eduardodeoh
Jul 24 2018 18:51
thanks @gcentauri
Grant Shangreaux
@gcentauri
Jul 24 2018 18:53
depends on the input/output data. I use the Dry::Validation.Schema everywhere because i am validating plain ruby hashes. I also had not seen the JSON schema until now. it looks like it does some processing on input and symbolizes the names for you so you can define the schema rules using ruby symbols. I would say if you know you're validating JSON or http Params, use those more specifically. I will have to play around to see what kinds of things are actually different about them.
Eduardo de Oliveira Hernandes
@eduardodeoh
Jul 24 2018 19:03
so, for while, i will use ::JSON.parse(request.body.read, symbolize_names: true) as my input and Dry::Validation.Schema using symbols to define schema
Grant Shangreaux
@gcentauri
Jul 24 2018 19:22
that seems pretty safe, my guess is that if you are processing JSON, you could use the Dry::Validation.JSON and avoid the symbolize_names part. it looks like dry might have some predicates that are customized for JSON if using that schema which is the only other difference i could find quickly