Next-gen ruby libs! » github.com/dry-rb » website: https://dry-rb.org » forum: https://discourse.dry-rb.org
require 'thread_safe'
require 'dry-validation'
schema = Dry::Validation.Form do
key(:test).required
key(:test_date).maybe
rule(test_date: [:test, :test_date]) do |test, test_date|
test.filled?.then(test_date.true?)
end
end
puts schema.call(test: '1', test_date: nil).messages
# {:test_date=>["must be true"]}
@solnic FYI
#!/usr/bin/env ruby
require 'bundler'
Bundler.require
GC.start
p GC.stat[:total_allocated_objects] # => 598617
start = Time.now.to_f
OrderForm = Dry::Validation.Form do
300.times do |i|
required(:"test_#{i}").filled(:bool?)
end
end
finish = Time.now.to_f
GC.start
p GC.stat[:total_allocated_objects] # => 7094131
p ((finish - start) * 1000).round # => 12288 msec
RSS goes from 31.48 MB to 176.15 MB
I'm looking at https://github.com/solnic/rodakase-blog/blob/master/lib/persistence/commands/create_user.rb
and see this. Where to read about input? Can't find in docs.
input Dry::Data['hash'].schema(
email: 'string', name: 'string', encrypted_password: 'string'
)
I want to use dry-validation schemas there if it possible :)
require 'dry-validation'
schema = Dry::Validation.Form do
configure do
def test_predicate(value)
true
end
end
optional :test_date
required(:test).maybe(:str?).when(:test_predicate) do
value(:test_date).filled?
end
end
puts schema.call(test: '1').messages
is
en:
errors:
ymd_date?: "string must be YYYY/MM/DD formatted"
ccorrect?
en:
errors:
rules:
key_name:
ymd_date?: "string must be YYYY/MM/DD formatted"
message for ymd_date? was not found
Dry::Validation::Schema.config.messages_file = './spec/messages.yml'
configure do
config.messages_file = './spec/messages.yml'
end
Dry::Validation::Schema.configure do |config|
config.messages_file = './spec/messages.yml'
end
require 'dry-validation'
module CustomPredicates
include Dry::Logic::Predicates
predicate(:ymd_date?) do |value|
false
end
end
Dry::Validation::Schema.configure do |config|
config.predicates = CustomPredicates
config.messages_file = './messages.yml'
end
schema = Dry::Validation.Form do
optional(:end_date, &:ymd_date?)
end
schema.call(end_date: 'not a date')
/home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/predicate_registry.rb:81:in `raise_unknown_predicate_error': +ymd_date?+ is not a valid predicate name (ArgumentError)
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/predicate_registry.rb:74:in `ensure_valid_predicate'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/key.rb:42:in `method_missing'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/dsl.rb:81:in `instance_eval'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/dsl.rb:81:in `define'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/dsl.rb:28:in `optional'
from 1.rb:16:in `block in <main>'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation.rb:28:in `instance_exec'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation.rb:28:in `Schema'
from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation.rb:46:in `Form'
from 1.rb:15:in `<main>'
Dry::Validation::Schema
but then calling a Dry::Validation::Form