These are chat archives for dry-rb/chat

11th
Jun 2016
Oskar Szrajer
@gotar
Jun 11 2016 06:23
nice :+1: more production projects it's what we need. Others always ask ok nice, but did anyone have something on production that use those technologies
Tim Riley
@timriley
Jun 11 2016 07:04
Alrighty, got the first steps of the dry-web-roda extraction made. Need to write a few tests for the (now pretty small) dry-web.
Zack Piper
@zackp30
Jun 11 2016 10:24
hi all \o
wondering how i'd make a key required if another one is present
Fran Worley
@fran-worley
Jun 11 2016 10:43
@zackp30 there is an example here: http://dry-rb.org/gems/dry-validation/high-level-rules/
Zack Piper
@zackp30
Jun 11 2016 12:10
thanks! :)
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 14:12
Hi. I'm new to dry and have a question. I'm trying to use dry-types + dry-validation together and i have trouble to understand how to create Dry::Types::Struct objects. If I use schemas to validate input and then provide output of validation to struct then I can omit validation somewhere in code when creating structs. And in struct there wont be validations beside defined types. Plus there will be duplication in types in schema and struct. How can I define some hook to validate struct when i create it. I hope you will understand :)
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:11
@onemanstartup depending on the kind of validations that you want to perform you might use contrained types: http://dry-rb.org/gems/dry-types/constraints/
About hooks, afaik the libraries doesn't provide any, you should run your schema validation before passing it to the struct.
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:13
@mrbongiolo But i want to protect myself from myself :)
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:14
@onemanstartup not sure there's such kind of tool... hahaha
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:17
It will be great if you could define struct based on schema. Why use schema only in sanitizing inputs from outside world.
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:17
USually you would trust the internals of your app
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:20
You could make typo somewhere and notice after a while in production that you have garbage data because somewhere inside you made typo. Trust, but verify
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:20
Then you should use constrained types, since it uses dry-logic you can get most of dry-validation predicates with it, probably wouldn't have access to high-level rules, but those seem a bit of for Values and Structs
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:21
So then it seems you must create types, create schemas with those types and create structs with those types.
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:22
class User < Dry::Types::Struct
  attribute :name, Types::Maybe::Coercible::String(exclusion: ['bad name'])
  attribute :age, Types::Coercible::Int.constrained(min_size: 18)
end
if you're just dealing with internal fully controlled data, then types would suffice, since you don't need to provide any validation errors
if you're dealing with data coming from outside then you should first get in through your validations
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:25
Basically i want both. To do this i will need to create UserName = Types::Maybe::Coercible::String(exclusion: ['bad name']) And use this type in struct and validations
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:26
Yes, I think so.
But data-structs and data-validation are two different things, they can reuse some parts, but they are not the same
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:27
One question remains how to test types. If i have types then I don't need check garbage data in structs.
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:28
You can unit test your custom types, or you can unit test your structs
or what kind of tests are you reffering to?
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:30
So many possibilities. I come from rails where is always mostly one way. Right now i think of how to test validations and I am testing schemas
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:32
@onemanstartup TBH I just test my custom validations, not saying that you can't your shouldn't test the build-in predicates, but I know that if I'm calling them they will work. BTW are you using dry-v with rails or with kind of project?
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:32
Thats why the question arises. If i testing schemas I don't testing struct. If I testing struct i need testing validations as well. Then I need testing types and test only special rules in validations. That way I don't duplicate code.
I'm trying to build on roda with full stack rom-dry-roda
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:33
Ah, I see.
Yeah, they have a bit different "way of doing things" than rails, basically dry-rb is more verbose and explicit
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:35
I don't have problem with explicitness only with how to stitch together everything :)
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:36
ikr
still tyring to grasp some of the concepts hahaha
the way I would do it now is something like: unit test your custom types, then test your complex structs (maybe not even needed), if needed you could test the schemas also, but it really depends. After that you would start testing the higher parts of your app that depend on the schema and such
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:39
Yeah, it seems so. Thanks!
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:39
Currently I'm using an operation style, so my main business specs are done there, basically I want to know if sending the right data I got the expected results and effects, I don't need to exactly re-test my schema details, just that my operation is using it
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 15:41
Operations and commands will be my next step in learning this stack
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 15:44
When I started testing I tested just about everything, I had a lot of duplicated specs, but later on I was able to refactor a good amount of those, it's hard to know what to "extract" or separate if you don't even know what is duplicated or not
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 16:15
Is this a bug or I am using it wrong?
# dry-validation-0.7.4/lib/dry/validation/error_compiler/input.rb:47:in `visit_predicate': 
# message for included_in? was not found (Dry::Validation::MissingMessageError)

require 'i18n'
require 'dry-validation'

module Types
  include Dry::Types.module
  ArticleStatus = Types::Strict::String
                  .default('draft')
                  .enum('draft', 'published', 'archived')
end

schema = Dry::Validation.Schema do
  configure { config.messages = :i18n }

  key(:email).required
  key(:status).required(Types::ArticleStatus)
end

p schema.call(email: '').messages
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 17:00
I guess version 0.7.4 uses :inclusion? instead of included_in
but to fix this error you just have to add included_in? to your errors.yml file
Andy Holland
@AMHOL
Jun 11 2016 17:05
@timriley sorry for the late reply, that would be brilliant, thanks, I'll give rubygems and repi access when I'm at my laptop
Dmitriy Plekhanov
@onemanstartup
Jun 11 2016 17:27
@mrbongiolo but it is dry-validation error. I don't have MY errors.yml
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 11 2016 19:58
@onemanstartup but you can override it anyway and add the missing key, I guess this is happening because your version of dry-types is using a newer version of dry-logic that uses included_in instead of inclusion. But I can't be so sure.
Zack Piper
@zackp30
Jun 11 2016 21:09
require 'dry-validation'
schema = Dry::Validation.Form do
  optional :test
  optional :test_date
  rule(test_r: [:test, :test_date]) do |test, test_date|
    test.filled?.then(test_date.true?)
  end
end

puts schema.call(test: '1').messages # shouldn't `:test_date' be invalid, since `test'
                                     # is supplied but `test_date' isn't?
I'm confused (as mentioned in the comment at the bottom), shouldn't that print something and not {}?