Next-gen ruby libs! » github.com/dry-rb » website: https://dry-rb.org » forum: https://discourse.dry-rb.org
rspec ./spec/integration/schema/using_types_spec.rb:44 # Dry::Validation::Schema defining schema using dry types fails when sum-type rule did not pass
rspec ./spec/integration/schema/using_types_spec.rb:36 # Dry::Validation::Schema defining schema using dry types correctly responds to messages
rspec ./spec/integration/schema/using_types_spec.rb:25 # Dry::Validation::Schema defining schema using dry types passes when input is valid
rspec ./spec/integration/schema/using_types_spec.rb:30 # Dry::Validation::Schema defining schema using dry types fails when input is not valid
rspec ./spec/integration/schema/using_types_spec.rb:66 # Dry::Validation::Schema defining schema using dry types structs handles nested structs
rspec ./spec/integration/schema/using_types_spec.rb:70 # Dry::Validation::Schema defining schema using dry types structs fails when input is not valid
rspec ./spec/integration/schema/using_types_spec.rb:86 # Dry::Validation::Schema defining schema using dry types custom coercions applies custom types to input prior validation
rspec ./spec/integration/schema/using_types_spec.rb:103 # Dry::Validation::Schema defining schema using dry types custom types applies custom types to input prior validation
rspec ./spec/integration/schema/predicates/array_spec.rb:216 # Predicates: Array as macro with required when using an input_processor with nil input is not successful
rspec ./spec/integration/schema/predicates/array_spec.rb:224 # Predicates: Array as macro with required when using an input_processor with blank input is not successful
Failures:
1) Predicates: Array as macro with required when using an input_processor with nil input is not successful
Failure/Error: processed_input = input_processor[input]
NoMethodError:
undefined method `map' for nil:NilClass
Did you mean? tap
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/array/member.rb:13:in `call'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:15:in `block in call'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:13:in `each'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:13:in `each_with_object'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:13:in `call'
# ./lib/dry/validation/schema.rb:203:in `call'
# ./spec/support/predicates_integration.rb:5:in `result'
# ./spec/integration/schema/predicates/array_spec.rb:217:in `block (6 levels) in <top (required)>'
2) Predicates: Array as macro with required when using an input_processor with blank input is not successful
Failure/Error: processed_input = input_processor[input]
NoMethodError:
undefined method `map' for "":String
Did you mean? tap
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/array/member.rb:13:in `call'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:15:in `block in call'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:13:in `each'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:13:in `each_with_object'
# /Users/francesworley/.rvm/gems/ruby-2.3.0/bundler/gems/dry-types-861c0b2619a2/lib/dry/types/hash/schema.rb:13:in `call'
# ./lib/dry/validation/schema.rb:203:in `call'
# ./spec/support/predicates_integration.rb:5:in `result'
# ./spec/integration/schema/predicates/array_spec.rb:225:in `block (6 levels) in <top (required)>'
Using dry-logic 0.2.3 from git://github.com/dry-rb/dry-logic.git (at master@8f8dffb)
Using dry-types 0.7.2 from git://github.com/dry-rb/dry-types.git (at master@861c0b2)
expected
and current
now because we automatically grab args from predicates
options_for_*
methods because they were not needed anymore
def self.messages
Dry::Validation::Messages.default.merge(
en: { errors: { with_lots_of_args?: '%{foo} %{bar} %{baz} %{value}' } }
)
end
def with_lots_of_args?(foo, bar, baz, input)
false
end
Failure/Error:
expect(schema.(email: 'email').messages).to eql(
email: ['foo bar baz email']
)
expected: {:email=>["foo bar baz email"]}
got: {:email=>["foo bar baz email", "foo bar baz "]}
(compared using eql?)
Diff:
@@ -1,2 +1,2 @@
-:email => ["foo bar baz email"],
+:email => ["foo bar baz email", "foo bar baz "],
# ./spec/integration/custom_predicates_spec.rb:148:in `block (2 levels) in <top (required)>'
.constructor
on Strict::String
overrides the existing constructor applying the strictness semantics. So I don’t actually see any reason why someone would want to actually make a custom type on top of Strict::String, in this case.
value
in all cases yet
#constructor
on top of existing types like that? It should be a “build it yourself from scratch” thing instead.
ErrorCompiler::Input
each
errors with messages for specific elements is tricky
each
results have predicates with proper info in the args…then we’re good
list
we could introduce some conventions, ie an arg named list
would be transformed with list.join(‘, ‘)
def some_validate?(list1, list2) ... end
etc
Constrained#constructor
should raise an error that this makes no sense?
join
-ed
options_for
stuff
Constrained
types it’s important that their original #call
is run, and not overridden by someone else’s constructor, because otherwise we’re losing their constraints.
size
constraint
en:
errors:
valid_role?: 'isn\'t a valid role'
How I can bind
configure do
config.messages_file = '/path/to/errors.yml'
def valid_role?(value)
Role.all.map(&:name).include?(value)
end
end
with the errors.yml? :)
I got
did not find expected key while parsing a block mapping at line 3 column 5
another problem now :)
ArgumentError: +valid_role?+ is not a valid predicate name
key(:name) { filled? & str? & valid_role? }
configure do
config.messages_file = '/path/errors.yml'
def valid_role?(value)
Role.all.map(&:name).include?(value)
end
end
Strict::String
runs before my own constructor, since I'm using it as a "base" for my type. So this base type basically is there to identify the final valid type, I could have a Strict::String::TrimmedDowncasedAndSnaked
but in the end it would just be a String. We might just need some improvements on the docs to explain that the custom constructors will run before the base and it's your responsibility to check the types, if needed.
@schema = Dry::Validation.Schema do
# Some stuff
key(:values).schema(values_schema)
.
.
.
end
def values_schema
Dry::Validation.Schema do
configure do
config.messages_file = '/path/to/errors.yml'
def valid_role?(value)
Chef::Role.all.map(&:name).include?(value)
end
end
optional(:roles).each do
schema do
key(:name) { filled? & str? & valid_role? }
end
end
end
end
i18n
IIRC there’s a known bug in plain-yaml messages where custom messages are not being merged correctl with defaults :/
input.is_a?(::String) ? input.gsub(/\D/, '') : input
, if it's a String then get the numbers, if not then send it to strict.string to raise an error...
I have another problem ^^
undefined method `merge' for #<Dry::Validation::Messages::I18n:0x00000004d217d0>
configure do
config.messages = :i18n
config.messages_file = '/home/lairan/Documents/selfdeploy/app/validators/errors.yml'
def valid_role?(value)
Chef::Role.all.map(&:name).include?(value)
end
end
irb(main):008:0> Types::Coercible::String.constructor(&:strip).constrained(type: String)[" foo "]
=> "foo"
irb(main):009:0> Types::Coercible::String.constructor(&:strip).constrained(type: String)[1]
=> “1”
require 'dry-validation'
schema = Dry::Validation.Schema do
configure do
config.messages_file = './errors.yml'
def valid_role?(value)
%w(Admin Viewer Actor).include?(value)
end
end
optional(:roles).each do
schema do
key(:name) { filled? & str? & valid_role? }
end
end
end
pp schema.call(roles: [{name: 'Admin'}, {name: 'Test'}]).messages
This code fail with this error:
/home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:195:in `[]': +valid_role?+ is not a valid predicate name (ArgumentError)
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:54:in `visit_predicate'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:32:in `visit_key'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:59:in `visit_and'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:59:in `visit_and'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:254:in `block in initialize_rules'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:253:in `each'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:253:in `each_with_object'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:253:in `initialize_rules'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:174:in `initialize'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:46:in `new'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:46:in `new'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema_compiler.rb:33:in `visit_schema'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:59:in `visit_and'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:13:in `block in call'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:13:in `map'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:13:in `call'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:45:in `visit_set'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:49:in `visit_each'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:59:in `visit_and'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:32:in `visit_key'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:74:in `visit_implication'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-logic-0.2.3/lib/dry/logic/rule_compiler.rb:18:in `visit'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:254:in `block in initialize_rules'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:253:in `each'
from /home/lairan/.rvm/gems/ruby-2.2.3/gems/dry-validation-0.7.4/lib/dry/validation/schema.rb:253:in `each_wit
schema(RoleSchema)
"10/02/2016", '2016-02-10', [2016, 10, 02], 02/10/2016
and so on, so the constructor would check all the possibilities and return a Date
, after that we could transform that value, if needed. I'm not sure if Dry::Types is aiming for this type of data transformation, but some constructors are used like that.