ExodusMovement / schemasafe

A reasonably safe JSON Schema validator with draft-04/06/07/2019-09/2020-12 support.
https://npmjs.com/@exodus/schemasafe
MIT License
161 stars 12 forks source link

Don't check for present() twice #43

Closed ChALkeR closed 4 years ago

ChALkeR commented 4 years ago

After #42.

Checking for present() twice makes no sense.

Also, now {items:[{allOf:[{default:10}]} fails with useDefaults, because defaults don't make any sense in nested checks.

rule and subrule moved below present() check because they now circumvent it when called on the current node, so we must be sure that those are not used before that check.

Only dependencies/dependentRequired, not, if/then/else, allOf/anyOf/oneOf can cause such nested checks -- i.e. anything that invokes rule(current, or subrule(current,.

ChALkeR commented 4 years ago

E.g. require('.').validator({items:[{not:{required:['x']}},{}]}).toModule():

Before:

(function() {
'use strict'
const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
return (function validate(data) {
  if (data === undefined) data = null
  let errors = 0
  if (Array.isArray(data)) {
    if (data[0] !== undefined && hasOwn(data, 0)) {
      const prev0 = errors
      if (data[0] !== undefined && hasOwn(data, 0)) {
        if (typeof data[0] === "object" && data[0] && !Array.isArray(data[0])) {
          if (!(data[0]["x"] !== undefined && hasOwn(data[0], "x"))) {
            errors++
          }
        }
      }
      if (prev0 === errors) {
        return false
      } else {
        errors = prev0
      }
    }
  }
  return errors === 0
})})();

After:

(function() {
'use strict'
const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
return (function validate(data) {
  if (data === undefined) data = null
  let errors = 0
  if (Array.isArray(data)) {
    if (data[0] !== undefined && hasOwn(data, 0)) {
      const prev0 = errors
      if (typeof data[0] === "object" && data[0] && !Array.isArray(data[0])) {
        if (!(data[0]["x"] !== undefined && hasOwn(data[0], "x"))) {
          errors++
        }
      }
      if (prev0 === errors) {
        return false
      } else {
        errors = prev0
      }
    }
  }
  return errors === 0
})})();