spec/integration/schema/form_spec.rb in dry-validation-0.7.2 vs spec/integration/schema/form_spec.rb in dry-validation-0.7.3
- old
+ new
@@ -7,15 +7,13 @@
key(:address).schema do
key(:city).required
key(:street).required
- key(:loc) do
- hash? do
- key(:lat).required(:float?)
- key(:lng).required(:float?)
- end
+ key(:loc).schema do
+ key(:lat).required(:float?)
+ key(:lng).required(:float?)
end
end
optional(:password).maybe.confirmation
@@ -121,11 +119,11 @@
end
describe 'with nested schema in a high-level rule' do
subject(:schema) do
Dry::Validation.Form do
- key(:address).maybe
+ key(:address).maybe(:hash?)
key(:delivery).required(:bool?)
rule(address: [:delivery, :address]) do |delivery, address|
delivery.true?.then(address.schema(AddressSchema))
@@ -142,19 +140,23 @@
after do
Object.send(:remove_const, :AddressSchema)
end
- it 'applies nested form schema' do
+ it 'succeeds when nested form schema succeeds' do
result = schema.(delivery: '1', address: { city: 'NYC', zipcode: '123' })
expect(result).to be_success
+ end
+ it 'does not apply schema when there is no match' do
+ result = schema.(delivery: '0', address: nil)
+ expect(result).to be_success
+ end
+
+ it 'fails when nested schema fails' do
result = schema.(delivery: '1', address: { city: 'NYC', zipcode: 'foo' })
expect(result.messages).to eql(
address: { zipcode: ['must be an integer'] }
)
-
- result = schema.(delivery: '0', address: nil)
- expect(result).to be_success
end
end
end