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

Error when validating array items with different conditionally required properties #2473

Open
dub-wt opened this issue Jul 16, 2024 · 6 comments
Labels

Comments

@dub-wt
Copy link

dub-wt commented Jul 16, 2024

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:

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.

@dblock
Copy link
Member

dblock commented Jul 16, 2024

Check whether this is still the behavior on HEAD and turn it into a (failing) PR/spec?

Note that your post may need a correct content-type, transforming what you post to a string, this may be a side-effect of form encoding.

@dblock dblock added the bug? label Jul 16, 2024
@dub-wt
Copy link
Author

dub-wt commented Jul 16, 2024

I checked it on the latest commit and just have rechecked with passing Content-Type = application/json.
Validation fails.

@dub-wt
Copy link
Author

dub-wt commented Jul 17, 2024

Here is the PR with failing spec: #2474

@dub-wt
Copy link
Author

dub-wt commented Jul 17, 2024

params do
        requires :items, type: Array, allow_blank: false do
          requires :item_type, type: String, allow_blank: false

          given item_type: ->(val) { val == 'type_a' } do
            requires :inner_items, type: Array do
              requires :prop_a, type: String
            end
          end

          given item_type: ->(val) { val == 'type_b' } do
            requires :inner_items, type: Array do
              requires :prop_b, type: String
            end
          end
        end
      end

Looks like condition given item_type: ->(val) { val == 'type_a' } is ignored when validating nested array properties.
They are always validated, but it should happen when item_type meets dependency.

@dblock
Copy link
Member

dblock commented Jul 17, 2024

Looks like condition given item_type: ->(val) { val == 'type_a' } is ignored when validating nested array properties.
They are always validated, but it should happen when item_type meets dependency.

And it works for nested hashes? Want to try to fix it?

@dub-wt
Copy link
Author

dub-wt commented Jul 19, 2024

No, it also doesn't work for nested hashes.
Sure, will try to fix it.

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