spec/integration/schema/form_spec.rb in dry-validation-0.7.4 vs spec/integration/schema/form_spec.rb in dry-validation-0.8.0
- old
+ new
@@ -1,33 +1,33 @@
RSpec.describe Dry::Validation::Schema::Form, 'defining a schema' do
subject(:schema) do
Dry::Validation.Form do
- key(:email).required
+ configure do
+ def email?(value)
+ true
+ end
+ end
- key(:age).maybe(:int?, gt?: 18)
+ required(:email).filled
- key(:address).schema do
- key(:city).required
- key(:street).required
+ required(:age).maybe(:int?, gt?: 18)
- key(:loc).schema do
- key(:lat).required(:float?)
- key(:lng).required(:float?)
+ required(:address).schema do
+ required(:city).filled
+ required(:street).filled
+
+ required(:loc).schema do
+ required(:lat).filled(:float?)
+ required(:lng).filled(:float?)
end
end
optional(:password).maybe.confirmation
optional(:phone_number).maybe(:int?, gt?: 0)
rule(:email_valid) { value(:email).email? }
-
- configure do
- def email?(value)
- true
- end
- end
end
end
describe '#messages' do
it 'returns compiled error messages' do
@@ -116,26 +116,98 @@
}
)
end
end
+ describe 'with an each and nested schema' do
+ subject(:schema) do
+ Dry::Validation.Form do
+ required(:items).each do
+ schema do
+ required(:title).filled(:str?)
+ end
+ end
+ end
+ end
+
+ it 'passes when each element passes schema check' do
+ expect(schema.(items: [{ title: 'Foo' }])).to be_success
+ end
+
+ it 'fails when one or more elements did not pass schema check' do
+ expect(schema.(items: [{ title: 'Foo' }, { title: :Foo }]).messages).to eql(
+ items: { 1 => { title: ['must be a string'] } }
+ )
+ end
+ end
+
+ describe 'symbolizing keys when coercion fails' do
+ subject(:schema) do
+ Dry::Validation.Form do
+ required(:email).value(size?: 8..60)
+ required(:birthdate).value(:date?)
+ required(:age).value(:int?, gt?: 23)
+ required(:tags).maybe(max_size?: 3)
+ end
+ end
+
+ let(:tags) { %(al b c) }
+
+ it 'symbolizes keys and coerces values' do
+ result = schema.(
+ 'email' => 'jane@doe.org',
+ 'birthdate' => '2001-02-03',
+ 'age' => '24',
+ 'tags' => tags
+ ).to_h
+
+ expect(result.to_h).to eql(
+ email: 'jane@doe.org',
+ birthdate: Date.new(2001, 2, 3),
+ age: 24,
+ tags: tags
+ )
+ end
+
+ it 'symbolizes keys even when coercion fails' do
+ result = schema.(
+ 'email' => 'jane@doe.org',
+ 'birthdate' => '2001-sutin-03',
+ 'age' => ['oops'],
+ 'tags' => nil
+ )
+
+ expect(result.to_h).to eql(
+ email: 'jane@doe.org',
+ birthdate: '2001-sutin-03',
+ age: ['oops'],
+ tags: nil
+ )
+
+ expect(result.messages).to eql(
+ birthdate: ['must be a date'],
+ age: ['must be an integer', 'must be greater than 23']
+ )
+ end
+ end
+
describe 'with nested schema in a high-level rule' do
subject(:schema) do
Dry::Validation.Form do
- key(:address).maybe(:hash?)
+ required(:address).maybe(:hash?)
- key(:delivery).required(:bool?)
+ required(:delivery).filled(:bool?)
rule(address: [:delivery, :address]) do |delivery, address|
delivery.true?.then(address.schema(AddressSchema))
end
end
end
before do
AddressSchema = Dry::Validation.Form do
- key(:city).required
- key(:zipcode).required(:int?)
+ required(:city).filled
+ required(:zipcode).filled(:int?)
end
end
after do
Object.send(:remove_const, :AddressSchema)