#2473·grape

Error when validating array items with different conditionally required properties

Author: dub-wtCreated Jul 16, 2024Updated Jul 19, 2024
Labelsbug?

Hello,

If we define different properties required for array items by given condition then validation fails even if correct request is sent.

Please check the example below:

rb
it 'test to demonstrate the validation issue' do
  subject.params do
    requires :a, type: String, allow_blank: false, values: %w[x y z]
    given a: ->(val) { val == 'z' } do
      requires :inner3, type: Array, allow_blank: false do
        requires :bar, type: String, allow_blank: false

        given bar: ->(val) { val == 'b' } do
          requires :baz, type: Array do
            requires :new_category, type: String
          end
        end

        given bar: ->(val) { val == 'c' } do
          requires :baz, type: Array do
            requires :baz_category, type: String
          end
        end

      end
    end
  end
  subject.post('/nested-dependency') { declared(params).to_json }

  test = {
    a: 'z',
    inner3: [
      { bar: 'b', baz: [{ new_category: 'nope' }] },
      { bar: 'c', baz: [{ baz_category: 'nope' }] }
    ]
  }

  post '/nested-dependency', **test

  expect(last_response.status).to eq(200)
end

Expected behavior: 200 OK

Actual behavior: 400 Bad request, "inner3[1][baz][0][new_category] is missing, inner3[1][baz][0][baz_category] is missing"

Thank you.