These are chat archives for dry-rb/chat

2nd
Aug 2018
Maciej Mensfeld
@mensfeld
Aug 02 2018 11:57
Good afternoon
is there a way to validate that a certain key is not present with dry-validations?
I mean I need to make sure that a key is not present at all in the incoming hash
it would be an inversion of required I guess
Jaromír Červenka
@Cervajz
Aug 02 2018 12:08
@mensfeld Hi :) What is the use case?
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:09
I have an incoming hash with multiple values and some are legacy. Wanted to use dry-validation to catch those keys that shouldn't be in the incoing hash
as a feedback for users
Jaromír Červenka
@Cervajz
Aug 02 2018 12:10
Just from top of my head: What about a custom validation against dry-struct
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:11
@Cervajz could you provide a simple snippet pretty please?
or how can I check that something is not present or not filled at all?
Jaromír Červenka
@Cervajz
Aug 02 2018 12:19
Or meaybe this:
ExampleSchema = Dry::Validation.Schema do
  configure do
    def self.messages
      super.merge(
        en: { errors: { deprecated?: 'is deprecated' } }
      )
    end
  end


  required(:valid_attr).filled
  optional(:deprecated_attr1).maybe(:str?)

  validate(deprecated?: :deprecated_attr1) do |deprecated_attr1|
    deprecated_attr1.blank?
  end
end
[11] pry(main)> reload!; ExampleSchema.call({valid_attr: "some value"}).errors
Reloading...
=> {}
[12] pry(main)> reload!; ExampleSchema.call({valid_attr: "some value", deprecated_attr1: "this is deprecated"}).errors
Reloading...
=> {:deprecated_attr1=>["is deprecated"]}
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:20
yeah, did exactly that right now :D
thanks!
still does not solve the problem of the key being present but I guess I can tackle that a bit differently
Jaromír Červenka
@Cervajz
Aug 02 2018 12:24
Hmm not sure how I'd do that
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:25
is there a way to inject messatges to all the schemas?
the deprecation rule will be common amongs many schemas
and I don't want to repeat this over and over
Cassio Godinho
@cpgo
Aug 02 2018 12:26
is the project discourse still active or gitter is the right place to ask for help?
I posted a question here https://discourse.dry-rb.org/t/dry-validation-with-custom-range-type/597
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:26
I see that I can overwrite the whole file
Jaromír Červenka
@Cervajz
Aug 02 2018 12:29
module Types
  include Dry::Types.module
end

class ExampleStruct < Dry::Struct
  input input.strict

  attribute :valid_attr, Types::Strict::String.optional
end
[12] pry(main)> reload!; ExampleStruct.new(valid_attr: 'xxx', deprecated: 'aaa')
Reloading...
Dry::Struct::Error: [ExampleStruct.new] unexpected keys [:deprecated] in Hash input
from /Users/cervajz/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/dry-struct-0.5.0/lib/dry/struct/class_interface.rb:208:in `rescue in new'
[13] pry(main)>
But I am not sure this is the proper way, as you'd have to define your schema twice
@mensfeld ^^
but it works with deprecated: nil as well
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:30
thanks. I will just do custom checks and use the Dry::Validation::Result as a result to my custom logic
so I have standarized api despite not using dry-validation for that
Jaromír Červenka
@Cervajz
Aug 02 2018 12:30
I use yml file and have parent class for all schemas
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:31
thanks!
Jaromír Červenka
@Cervajz
Aug 02 2018 12:31
class ApplicationSchema < Dry::Validation::Schema
  configure do |config|
    config.messages_file = 'config/locales/validations.yml'
  end
...
end
ExampleSchema = Dry::Validation.Schema(ApplicationSchema) do
Maciej Mensfeld
@mensfeld
Aug 02 2018 12:32
<3