Skip to content

Commit

Permalink
Merge pull request #181 from ChALkeR/chalker/fix-undef
Browse files Browse the repository at this point in the history
Fix 'required' implementation
  • Loading branch information
LinusU committed Jul 21, 2020
2 parents df5b313 + c224619 commit 5389c5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -243,19 +243,20 @@ var compile = function(schema, cache, root, reporter, opts) {
}

if (Array.isArray(node.required)) {
var n = gensym('missing')
validate('var %s = 0', n)
var checkRequired = function (req) {
var prop = genobj(name, req);
validate('if (%s === undefined) {', prop)
error('is required', prop)
validate('missing++')
validate('%s++', n)
validate('}')
}
validate('if ((%s)) {', type !== 'object' ? types.object(name) : 'true')
validate('var missing = 0')
node.required.map(checkRequired)
validate('}');
if (!greedy) {
validate('if (missing === 0) {')
validate('if (%s === 0) {', n)
indent++
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/required-unique-noconflict.js
@@ -0,0 +1,19 @@
const tape = require('tape')
const validator = require('..')

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

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

0 comments on commit 5389c5b

Please sign in to comment.