Skip to content

Non-greedy (default) mode is broken (returning true on invalid) on mixed type rules #179

@ChALkeR

Description

@ChALkeR

Due to

is-my-json-valid/index.js

Lines 234 to 241 in b5f46cd

validate('if ((%s)) {', type !== 'object' ? types.object(name) : 'true')
validate('var missing = 0')
node.required.map(checkRequired)
validate('}');
if (!greedy) {
validate('if (missing === 0) {')
indent++
}

If type is not an object, missing is undefined, which is not strict equal to 0, and all follow-up checks (including ones intended for non-objects) are excluded.

Testcase:

const tape = require('tape')
const validator = require('is-my-json-valid')

tape('test', (t) => {
  const validate = validator({ required: [], uniqueItems: true })
  t.notOk(validate([1, 1]), 'required + uniqueItems')
  t.end()
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions