Skip to content

Commit c224619

Browse files
committed
Fix 'required' implementation
Now it doesn't rely on undeclared variables anymore. Fixes: #179
1 parent 2534af4 commit c224619

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,20 @@ var compile = function(schema, cache, root, reporter, opts) {
243243
}
244244

245245
if (Array.isArray(node.required)) {
246+
var n = gensym('missing')
247+
validate('var %s = 0', n)
246248
var checkRequired = function (req) {
247249
var prop = genobj(name, req);
248250
validate('if (%s === undefined) {', prop)
249251
error('is required', prop)
250-
validate('missing++')
252+
validate('%s++', n)
251253
validate('}')
252254
}
253255
validate('if ((%s)) {', type !== 'object' ? types.object(name) : 'true')
254-
validate('var missing = 0')
255256
node.required.map(checkRequired)
256257
validate('}');
257258
if (!greedy) {
258-
validate('if (missing === 0) {')
259+
validate('if (%s === 0) {', n)
259260
indent++
260261
}
261262
}

test/required-unique-noconflict.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const tape = require('tape')
2+
const validator = require('..')
3+
4+
tape('required + uniqueItems', (t) => {
5+
const validate = validator({ required: ['a'], uniqueItems: true })
6+
t.notOk(validate([1, 1]), 'required + uniqueItems')
7+
t.end()
8+
})
9+
10+
tape('required + uniqueItems inside allOf', (t) => {
11+
const validate = validator({
12+
required: ['a'],
13+
allOf: [
14+
{ required: ['b'], uniqueItems: true }
15+
]
16+
})
17+
t.notOk(validate([1, 1]), 'required + uniqueItems in allOf')
18+
t.end()
19+
})

0 commit comments

Comments
 (0)