Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional nested array not validated #1636

Closed
ericproulx opened this issue May 21, 2017 · 2 comments
Closed

Optional nested array not validated #1636

ericproulx opened this issue May 21, 2017 · 2 comments
Labels

Comments

@ericproulx
Copy link
Contributor

ericproulx commented May 21, 2017

Branch: master
Works under version : 0.13.0

Hard to explain so here are 2 specs that should pass but the last one is failing :

context 'nesting array validation' do
    before :each do
      subject.params do
        requires :my_array, type: Array do
          requires :a_integer, type: Integer, allow_blank: true
          optional :nested_array, type: Array, allow_blank: true, default: [] do
            requires :a_integer, type: Integer, allow_blank: true
          end
        end
      end
      subject.get('/nested_optional_array') { declared(params).to_json }
      get '/nested_optional_array', params
    end

    context 'when my_array contains only a hash with the optional nested_array' do
      let(:params){
        {
          my_array: [
            {
              an_integer: 1,
              nested_array: [
                {
                  an_integer: :a
                }
              ]
            }
          ]
        }
      }

      it 'should fail' do
        # success
        expect(last_response).to be_bad_request
      end
    end

    context 'when my_array contains hashes with the optional nested_array and one without' do
      let(:params){
        {
          my_array: [
            {
              an_integer: 1,
              nested_array: [
                {
                  an_integer: :a
                }
              ]
            },
            {
              an_integer: 2,
              nested_array: [
                {
                  an_integer: :b
                }
              ]
            },
            {
              an_integer: 1
            }
          ]
        }
      }

      it 'should fail' do
        # failure
        expect(last_response).to be_bad_request
      end
    end
  end

The nested_array is optional but when there's at least one element in the parent array that doesn't have it, all other elements with a nested_array aren't validated

I'll try to figure it out.

Thanks

@dblock dblock added the bug? label May 22, 2017
@ericproulx
Copy link
Contributor Author

ericproulx commented Sep 29, 2017

The problem was inserted with this commit: 9a08358#diff-edc185e941279b3909ef50156ef72cd5

It doesn't validate fields in the optional one if we have one element in the array without the optional field. For instance, that test should return a 400 instead of a 200 because options is missing the field name which is required by definition

params do
  requires :root, type: Hash do
    optional :some_things, type: Array do
      requires :foo
      optional :options, type: Array do
        requires :name, type: String
        requires :value, type: String
      end
    end
  end
end

get '/nested_optional_array' do
  { root: params[:root] }
end

it 'allows optional arrays to be omitted' do
    params = { some_things:
                [{ foo: 'one', options: [{ value: 'nope' }] },
                 { foo: 'two' },
                 { foo: 'three', options: [{ name: 'wooop', value: 'yap' }] }] }
    get '/nested_optional_array', root: params
    expect(last_response.status).to eq(200)
    expect(last_response.body).to eq({ root: params }.to_json)
  end

@ericproulx
Copy link
Contributor Author

I'll do a PR for fixing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants