flash-gordon on v1.2.2
flash-gordon on master
Update CHANGELOG (compare)
flash-gordon on reject-string-keys
flash-gordon on master
Reject non-symbol keys in schem… Test invalid exception message Merge pull request #378 from dr… (compare)
flash-gordon on reject-string-keys
Test invalid exception message (compare)
flash-gordon on reject-string-keys
Test invalid exception message (compare)
flash-gordon on reject-string-keys
Reject non-symbol keys in schem… (compare)
dry-bot on master
[devtools] config sync (compare)
dry-bot on master
[devtools] config sync (compare)
dry-bot on master
[devtools] config sync (compare)
dry-bot on master
[devtools] config sync (compare)
dry-bot on master
[devtools] config sync (compare)
dry-bot on master
[devtools] config sync (compare)
dry-bot on master
[devtools] config sync (compare)
rule(location_provided: [:address, :latitude, :longitude]) do |address, latitude, longitude|
(address.filled? & latitude.filled? & longitude.filled?) |
(address.none? & latitude.none? & longitude.none?)
end
each attribute name repeated 4 times. Seems not readable.
I could extract internal code to some helper function like all_or_none(address, latitude, longitude)
but this will only reduce 1 mention of each name
Is it possible to use something like this?
rule(:location_provided) do |s| #s for schema
all_or_none(s.address, s.latitude, s.longitude)
end
Is this something that's possible with dry-validation?
Probably not. At least dry-v should be used for other. Look at https://github.com/solnic/transproc, I use it for the same tasks as your.
dry-validation
's schema to validate the inputs from an internal system, call out to one of two external APIs, massage the responses from them, and then map those responses to an internal model. The problem we're running into is that we'd like to share the schema between dry-validation and the internal model (currently using dry-struct
) but we can't really
The ability to reuse a schema in a flat / non-nested manner (issue #204) following conventions already established, like so:schema(EmailAddress::Schema::Create) required(:email_address_id, Types::String).value(:str?)
transform_keys(&:to_sym)
is the way to go.
schema.output.merge(errors: schema.errors)
to get the errors on the form object to cooperate with simple_form
app/schemas/bill_basic_schema.rb
:BillBasicSchema = Dry::Validation::Schema(build: false) do
required(:period_month) { filled? & int? & included_in?(1..12) }
required(:period_year) { filled? & int? & gt?(2010) }
required(:cost) { filled? & float? & gt?(0) }
optional(:paid_at) { filled? > date? }
optional(:state) { filled? > included_in?(Bill.state.values) }
optional(:payment_method_id) { filled? > int? }
optional(:tags) { array? }
end
app/forms/bill_form.rb
:class BillForm < ApplicationForm
attribute :period_month, Types::Params::Integer.default { Time.zone.today.month }
attribute :period_year, Types::Params::Integer.default { Time.zone.today.year }
attribute :cost, Types::Params::Float.default(0.0)
attribute :paid_at, Types::Params::Date.optional.default(nil)
attribute :state, Types::Coercible::String.default('init')
attribute :payment_method_id, Types::Params::Integer.optional.default(nil)
attribute :tags, Types::Coercible::Array.of(Types::Coercible::String).default([])
SCHEMA = Dry::Validation.Params(BillBasicSchema)
attr_accessor :errors
def self.from_model(model)
period = model.period || Time.zone.today
attrs = model.values.slice(:cost, :state, :paid_at,
:payment_method_id, :tags)
.merge(period_month: period.month, period_year: period.year)
new(attrs)
end
def self.new_attrs(overrides = {})
period = Time.zone.today
overrides.reverse_merge(state: :init,
period_month: period.month,
period_year: period.year)
end
end
TypeError (no implicit conversion of Hash into String)
errors
and the messages
methods but I get the same error everytime...
Is it possible to use validate
in combination with each
? Let me show the example:
Dry::Validation.Params do
required(:tickets).each do
validate(allowed_quantity: %i(selling_strategy quantity)) do |selling_strategy, quantity|
# Some logic for checking if quantity can be used
end
required(:price).filled(:int?)
required(:quantity).filled(:int?)
required(:selling_strategy).filled(:str?)
end
end
While required
checks are nicely nested under tickets, allowed_quantity always bubbles to the top. It would look like this:
{:tickets => { 0 => {:price => ["is missing"]}, :allowed_quantity => ["some msg]}
Any ideas how to nest this custom validation block as well? Cheers!
Hi, is it possible to validate the keys and values of a hash like this? These are the posted params.
{
"sample_tests"=>{
"1"=>{"sample_id"=>149, "lod"=>"200.0", "lod_top_unit_id"=>2, "lod_bottom_unit_id"=>1, "top_unit_id"=>2},
"2"=>{"sample_id"=>149, "lod"=>"101.0", "lod_top_unit_id"=>2, "lod_bottom_unit_id"=>1, "top_unit_id"=>2},
"3"=>{"sample_id"=>149, "lod"=>"101.0", "lod_top_unit_id"=>2, "lod_bottom_unit_id"=>1, "top_unit_id"=>2},
..... repeated
}
}
This is all I have right now. :
Schema = Dry::Validation.Params do
required(:sample_tests).filled
end
I need to validate the part that goes {"sample_id"=>149, "lod"=>"200.0", "lod_top_unit_id"=>2.....
include
for resuable methods and i use guard style for conditional steps
Dry::Validation::Params
stored_proc = proc { |a, b| a <= b }
I do stored_proc = Proc.new { |a, b| a<= b }
, but that way makes rubocop unhappy. why does doing the proc {}
syntax not work?
::Kernel.proc
attr_writer
statement used to work in dry-struct v0.4 but it doesn't work in v0.6 - is this by design?