Skip to content

How "anyOf" is working? #107

@SkeLLLa

Description

@SkeLLLa

Hi. I can see that this lib supports anyOf. But according to description it looks like it acts like oneOf.
For example in ajv anyOf should match 1 or more and oneOf should match exactly one.

Also it seems working correctly if anyOf items differ by type. However if they not, it will fail and just take the last object to validate.

Under the hood it converts anyOf to if-else clause with ajv.validate. But ajv supports oneOf and allOf. Why it just not pass the whole parent object (that has oneOf or anyOf) directly to ajv?

test('object with multiple objects', (t) => {
  t.plan(2)

  const schema = {
    title: 'object with multiple objects',
    type: 'object',
    properties: {
      prop: {
        anyOf: [{
          type: 'object',
          properties: {
            str: {
              type: 'string'
            },
            str1: {
              type: 'string'
            }
          }
        },{
          type: 'object',
          properties: {
            str: {
              type: 'string'
            }
          }
        }, {
          type: 'object',
          properties: {
            str1: {
              type: 'string'
            }
          }
        }]
      }
    }
  }
  const stringify = build(schema)

  try {
    const value = stringify({
      prop: {str: 'string', str1: 'string1'}
    })
    t.is(value, '{"prop":{"str":"string", "str1":"string1"}}')
  } catch (e) {
    t.fail()
  }
})
    +++ found
    --- wanted
    -{"prop":{"str":"string", "str1":"string1"}}
    +{"prop":{"str1":"string1"}}

I've tried to make a fix for that, but seems I'm missing something and if I add the code it throws ajv is not defined exception during tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions