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

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

Closed
ChALkeR opened this issue Jun 4, 2020 · 0 comments · Fixed by #181
Closed

Comments

@ChALkeR
Copy link
Contributor

ChALkeR commented Jun 4, 2020

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()
})
ChALkeR added a commit to ChALkeR/is-my-json-valid that referenced this issue Jul 15, 2020
Now it doesn't rely on undeclared variables anymore.

Fixes: mafintosh#179
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

Successfully merging a pull request may close this issue.

1 participant