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

Object should pass validation after it is modified by if-then-else logic. #550

Open
2 tasks done
TommyDew42 opened this issue Oct 7, 2022 · 2 comments
Open
2 tasks done

Comments

@TommyDew42
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

Plugin version

5.4.0

Node.js version

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

Description

From a PR comment. Object should pass validation after it is modified by if-then-else logic.
It seems that it's because the anyof validator.validate receive { a:[] } instead of the object after modified with const value.

Steps to Reproduce

test('anyOf with string and array', (t) => {
  t.plan(2)

  const schema = {
    anyOf: [
      { type: 'string' },
      {
        type: 'object',
        properties: {
          a: {
            type: 'array',
            if: { items: { type: 'number' }, minItems: 1 },
            then: { items: { type: 'number' } },
            else: { const: ['const item'] }
          }
        }
      }
    ]
  }

  const stringify = build(schema)

  t.equal(stringify('foo'), '"foo"') // ok
  t.equal(stringify({ a: [1, 2, 3] }), JSON.stringify({ a: [1, 2, 3] })) // ok
  t.equal(
    stringify({ a: [] }),
    JSON.stringify({ a: ['const item'] })
  ) // throw error: The value {"a":[]} does not match schema definition.
})

Expected Behavior

  t.equal(
    stringify({ a: [] }),
    JSON.stringify({ a: ['const item'] })
  ) // this should pass and no schema error is thrown
@ivan-tymoshenko
Copy link
Member

ivan-tymoshenko commented Oct 8, 2022

We have a discussion, where we are trying to resolve cases like that. It's not done yet, but from what I see now, I think it's a correct behavior. You can join of you want.

#532 (reply in thread)

@ivan-tymoshenko
Copy link
Member

But your case will not work anyway, because you have an array under anyOf, which mean that fjs uses Ajv for validation. And you pass an invalid data.

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

No branches or pull requests

2 participants