Skip to content

Instantly share code, notes, and snippets.

@Morozzzko
Created October 11, 2017 12:06
Show Gist options
  • Save Morozzzko/aa269cc30ba847b72169a8ebe08f267f to your computer and use it in GitHub Desktop.
Save Morozzzko/aa269cc30ba847b72169a8ebe08f267f to your computer and use it in GitHub Desktop.
Broken dry-validation form array schema
# you can simply put this code inside the dry-validation/spec/integration/form/predicates/array_spec.rb
context 'nested' do
subject(:schema) do
Dry::Validation.Form do
configure do
config.type_specs = true
end
required(:foo).schema do
required(:bar, :array).each(:int?)
end
end
end
context 'with missing input' do
let(:input) { { 'foo' => {} } }
it 'is not successful' do
expect(result).to be_failing bar: ['is missing']
end
end
context 'with nil input' do
let(:input) { { 'foo' => { 'bar' => nil } } }
it 'is not successful' do
expect(result).to be_failing bar: ['must be an array']
end
end
context 'with blank input' do
let(:input) { { 'foo' => { 'bar' => '' } } }
it 'is successful' do
expect(result).to be_successful
end
end
context 'with valid input' do
let(:input) { { 'foo' => { 'bar' => ['3', '123', '1456546564465'] } } }
it 'is successful' do
expect(result).to be_successful
end
end
context 'with invalid input' do
let(:input) { { 'foo' => { 'bar' => ['bar'] } } }
it 'is not successful' do
expect(result).to be_failing bar: { 0 => ['must be an integer'] }
end
end
# you can simply put this code inside the dry-validation/spec/integration/form/predicates/array_spec.rb
context 'nested' do
subject(:schema) do
IntArray = Dry::Types['form.array'].of(Dry::Types['coercible.int'])
Dry::Validation.Form do
configure do
config.type_specs = true
end
required(:foo).schema do
required(:bar, IntArray).each(:int?)
end
end
end
context 'with missing input' do
let(:input) { { 'foo' => {} } }
it 'is not successful' do
expect(result).to be_failing bar: ['is missing']
end
end
context 'with nil input' do
let(:input) { { 'foo' => { 'bar' => nil } } }
it 'is not successful' do
expect(result).to be_failing bar: ['must be an array']
end
end
context 'with blank input' do
let(:input) { { 'foo' => { 'bar' => '' } } }
it 'is successful' do
expect(result).to be_successful
end
end
context 'with valid input' do
let(:input) { { 'foo' => { 'bar' => ['3', '123', '1456546564465'] } } }
it 'is successful' do
expect(result).to be_successful
end
end
context 'with invalid input' do
let(:input) { { 'foo' => { 'bar' => ['bar'] } } }
it 'is not successful' do
expect(result).to be_failing bar: { 0 => ['must be an integer'] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment