All files / validator/yupSource/util runTests.js

68.97% Statements 20/29
59.09% Branches 13/22
100% Functions 2/2
73.08% Lines 19/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53        255x   255x 255x   255x 255x   255x 10x       245x 289x   289x 289x   57x     57x 57x 57x         232x 188x               188x         188x          
import ValidationError from '../ValidationError'
import { once } from './async'
 
export default function runTests(options, cb) {
  let { endEarly, tests, args, value, errors, sort, path } = options
 
  let callback = once(cb)
  let count = tests.length
 
  const nestedErrors = []
  errors = errors ? errors : []
 
  if (!count)
    return errors.length
      ? callback(new ValidationError(errors, value, path))
      : callback(null, value)
 
  for (let i = 0; i < tests.length; i++) {
    const test = tests[i]
 
    test(args, function finishTestRun(err) {
      if (err) {
        // always return early for non validation errors
        Iif (!ValidationError.isError(err)) {
          return callback(err)
        }
        if (endEarly) {
          err.value = value
          return callback(err)
        }
        nestedErrors.push(err)
      }
 
      if (--count <= 0) {
        Iif (nestedErrors.length) {
          if (sort) nestedErrors.sort(sort)
 
          //show parent errors after the nested ones: name.first, name
          if (errors.length) nestedErrors.push(...errors)
          errors = nestedErrors
        }
 
        Iif (errors.length) {
          callback(new ValidationError(errors, value, path))
          return
        }
 
        callback(null, value)
      }
    })
  }
}