Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Piotr Solnica
@solnic
@timriley I guess we should coerce nil to empty array
Fran Worley
@fran-worley
@solnic did you ever get anywhere with passing options to a message? I have a validation that tries to pass a Liquid Template and fails if Liquid has any errors. I'd love to be able to pass the Liquid message back to the user rather than my very unhelpful Your liquid template is invalid message set via my Yml file. Any thoughts?
Piotr Solnica
@solnic
@fran-worley planned for 1.0
niedhui
@niedhui
Hi, I was trying to switch Grape's coercible feature from virtus to dry-types.
Grape support mutiple candidate coercbile type,like requires :a, types: [Integer, String],
first it will try coerces input to Integer, if fails , try coerces to String.
I was planning to implements this using the Sum type.
(Dry::Types['coercible.int'] | Dry::Types['coercible.string'].call 'something' raise ArgumentError
I also tried Dry::Types['coercible.int'].safe or Dry::Types['maybe.coercible.int'], also failed with ArgumentError
Piotr Solnica
@solnic
irb(main):007:0> (Types::Form::Int | Types::String)[1]
=> 1
irb(main):008:0> (Types::Form::Int | Types::String)["foo"]
=> “foo"
irb(main):009:0> (Types::Form::Int | Types::String)["1"]
=> 1
@niedhui ^^
you want Form or JSON, not Coercible
Coercible uses kernel coercion methods, that’s for low-level coercions, not stuff like Grape and HTTP
niedhui
@niedhui
thanks , I'll try this,
Piotr Solnica
@solnic
Form and JSON are also “safe”, as in, they passthrough values that they couldn’t coerce
Piotr Solnica
@solnic
it’d be absolutely awesome if Grape switched to dry-types for coercion and dry-validation for, well, validation :D
niedhui
@niedhui
I was doing some work to see how much it will change the Grape's internal API, very early stage :smile:
BTW, Constructor#try only rescue the TypeError, why not rescue all the StandardError? so the try will work 'more as expected'
Piotr Solnica
@solnic
@niedhui that would be too much, our intention is to rescue from type errors, not any error
niedhui
@niedhui
@solnic seem's Dry::Types['form.int'] is a little unexpected , (Dry::Types['form.int'] | Dry::Types['string']).call('12a') will return 12, not the string '12a'
Piotr Solnica
@solnic
@niedhui UGH, it’s because we do ”12a”.to_i
niedhui
@niedhui
I look at the source code, maybe I should add Dry::Types.register('grape.int', Dry::Types['int'].constructor(->(input) { Integer(input) rescue nil})) as a work around, and use it in the Grape for the first step, to make all the orignal spec pass, than discuss what this should behave like
Piotr Solnica
@solnic
sounds good
John Backus
@backus
Finally getting around to commenting on #99
Simon Schmid
@sled
is it possible to "wrap" a validation schema inside Dry::Validation.Form only for the coercions? Otherwise I'd have to do everything twice, i.e for Form and JSON
Joe Van Dyk
@joevandyk
@solnic submitting issue. should that code work in the first place?
Ngan Pham
@ngan
Hi, is there a way to validate that an attribute is of a certain class? (using dry-validation). I tried doing key(:user).required(type?: User) and it works, but as soon as I add config.input_processor = :sanitizer to the config block, I get this error: Dry::Container::Error: Nothing registered with the key "user"
Sorry, just to be clear: "of a certain class", meaning "is an instance of a certain class"
Piotr Solnica
@solnic
@ngan at the moment type?: Foo requires a corresponding dry type because we use it for potential coercions. We could make it skip it gracefuly though when a type is not found
@sled could you clarify? you can set up a base schema with rules and infer Form and JSON schemas from it w/o duplication of rule definitions
Simon Schmid
@sled
ah cool
@solnic like defining the rules once, and use a different pre-processor when needed
stigchristian
@stigchristian
  article_schema_base = -> {
    key(:id).required(:str?)
    key(:proquint).required(:str?)
    key(:_links).schema do
      key(:self).schema do
        key(:href).required(:str?)
      end
    end
    key(:type).required(:str?)
    key(:sub_type).required(:str?)
    key(:title).required(:str?)
    key(:byline).required(:str?)
    key(:live_from).required(:date_time?)
    key(:live_to).required(:date_time?)
    key(:featured_image).required(:str?)
  }

  ArticleSchema = Dry::Validation.JSON do
    instance_exec(&article_schema_base)
    key(:text).required(:str?)
    key(:published).required(:bool?)
    key(:created_at).required(:date_time?)
    key(:updated_at).required(:date_time?)
    key(:last_saved_by).required(:hash?)
  end

  ArticleSchemaWithStack = Dry::Validation.JSON do
    instance_exec(&article_schema_base)
    key(:stack).required(:array?)
    key(:text_text).required(:str?)
    key(:text_ale).each(:hash?)
  end
Is there a better way of doing this?
Artem Pyankov
@iBublik
@stigchristian try this one
  ArticleSchemaBase = Dry::Validation.JSON do
    key(:id).required(:str?)
    key(:proquint).required(:str?)
    key(:_links).schema do
      key(:self).schema do
        key(:href).required(:str?)
      end
    end
    key(:type).required(:str?)
    key(:sub_type).required(:str?)
    key(:title).required(:str?)
    key(:byline).required(:str?)
    key(:live_from).required(:date_time?)
    key(:live_to).required(:date_time?)
    key(:featured_image).required(:str?)
  end

  ArticleSchema = Dry::Validation.Schema(ArticleSchemaBase) do
    key(:text).required(:str?)
    key(:published).required(:bool?)
    key(:created_at).required(:date_time?)
    key(:updated_at).required(:date_time?)
    key(:last_saved_by).required(:hash?)
  end

  ArticleSchemaWithStack = Dry::Validation.Schema(ArticleSchemaBase) do
    key(:stack).required(:array?)
    key(:text_text).required(:str?)
    key(:text_ale).each(:hash?)
  end
Piotr Solnica
@solnic
yes, what @iBublik said ^
Simon Schmid
@sled
@solnic How can I infer a JSON schema from an existing (plain) schema? :)
like Dry::Validation.JSON(plain_schema)
ah I got it thanks :D
Artem Pyankov
@iBublik
@sled as far as I know this won't work, cause Form ans JSON method doesn't accept constant (class) as paramater (look at https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation.rb#L45-L47). But you can use Schema method and specify config.input_processor = :json in configure block.
Piotr Solnica
@solnic
gimme a sec
require 'dry-validation'

base = Dry::Validation.Schema do
  key(:name).required
  key(:email).required
end

json = Dry::Validation.Schema(base) do
  configure { config.input_processor = :json }
end

form = Dry::Validation.Schema(base) do
  configure { config.input_processor = :form }
end
Piotr Solnica
@solnic
@sled ^^ this will work
but I gotta say just using Dry::Validation.Form|JSON should work too, something to improve…
Simon Schmid
@sled
@solnic, yes I'm building a service object, and this can be fed from either form input or an API call (JSON)
so I'm basically injecting the input processor from the outside :)
stigchristian
@stigchristian
thanks @iBublik
Simon Schmid
@sled
I'm using dry-types + dry-validation as replacement for virtus to model JSON documents, is it overkill to both use dry-type and dry-validation?
I'm feeling like I'm writting a lot of things twice
e.g an Address type + a Schema for the address
Andy Holland
@AMHOL
@sled personally I think there's value in keeping coercion/type-safety logic separate from validation logic, having said that, Dry::Validation::Schema::JSON landed in dry-validation master branch recently, which uses a json input processor to infer coercions in the same way Dry::Validation::Schema::Form works
Piotr Solnica
@solnic
@sled how exactly do you use dry-types?
Simon Schmid
@sled
mh basically what our app does is porting paper forms to "digital" forms
so I want to use dry-types to describe the structure of these forms/documents
I just got confused whether I should use Types::Form::Int, Types::Coercible::Int in my document type definition