Skip to content

Commit c6e7797

Browse files
committed
perf: optimize out contains: bool
1 parent 2ec8076 commit c6e7797

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/compile.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,10 @@ const compileSchema = (schema, root, opts, scope, basePathRoot = '') => {
469469
const nexthistory = () => [...history, { stat, prop: current }]
470470
// Can not be used before undefined check! The one performed by present()
471471
const rule = (...args) => visit(errors, nexthistory(), ...args).stat
472-
const subrule = (suberr, ...args) => {
473-
if (args[0] === current) {
472+
const subrule = (...args) => subruleImpl(false, ...args)
473+
const subruleSub = (...args) => subruleImpl(true, ...args).sub
474+
const subruleImpl = (noDelta, suberr, ...args) => {
475+
if (args[0] === current || noDelta) {
474476
const constval = constantValue(args[1])
475477
if (constval === true) return { sub: format('true'), delta: {} }
476478
if (constval === false) return { sub: format('false'), delta: { type: [] } }
@@ -894,7 +896,7 @@ const compileSchema = (schema, root, opts, scope, basePathRoot = '') => {
894896

895897
const suberr = suberror()
896898
iterate((prop, evaluate) => {
897-
const { sub } = subrule(suberr, prop, node.contains, subPath('contains'))
899+
const sub = subruleSub(suberr, prop, node.contains, subPath('contains'))
898900
fun.if(sub, () => {
899901
fun.write('%s++', passes)
900902
if (getMeta().containsEvaluates) {
@@ -914,7 +916,7 @@ const compileSchema = (schema, root, opts, scope, basePathRoot = '') => {
914916
}
915917

916918
const checkGeneric = () => {
917-
handle('not', ['object', 'boolean'], (not) => subrule(null, current, not, subPath('not')).sub)
919+
handle('not', ['object', 'boolean'], (not) => subruleSub(null, current, not, subPath('not')))
918920
if (node.not) uncertain('not')
919921

920922
const thenOrElse = node.then || node.then === false || node.else || node.else === false

test/json-schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const unsafe = new Set([
5353
'ref.json/ref to then',
5454
'ref.json/ref to else',
5555
"not.json/collect annotations inside a 'not', even if collection is disabled",
56+
'contains.json/contains keyword with boolean schema false', // optimized out
5657

5758
'ref.json/ref overrides any sibling keywords', // this was fixed in draft/2019-09 spec
5859
'ref.json/$ref prevents a sibling id from changing the base uri', // in pre-2019-09, any $ref siblings are not handled

0 commit comments

Comments
 (0)