All files validate.ts

100% Statements 185/185
100% Branches 153/153
100% Functions 27/27
100% Lines 153/153

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 2693x                                         3x 31x 31x           3x   9x 51x 36x 27x   27x   27x 3x     6x   21x         3x 92x   3x 40x   3x 279x   3x 21x 21x 19x 15x   14x   3x 53x   3x 3874x 3816x 3815x     3x 10x 8x 7x 6x 5x 5x     5x     3x 40x   3x   166x   165x 165x 239x 239x 86x 85x     12x     3x   3403x 3400x 3400x 3400x 2x       3398x 3398x 3398x 3679x 3679x 3679x   3398x   3x     3x   3521x 9x     3512x 3512x   3512x 7151x 7151x 1x 1x   7150x 7148x 7148x       3510x 7255x 7255x 107x 107x 107x       3510x     3x   79x 2x     77x 77x 77x 77x 77x 77x 77x 2x       75x 210x 16x 16x 3x 3x 3x     207x 207x 207x     75x   3x 4380x 33x 92x 258x 53x 3863x 40x 40x 1x       3x 4421x   3x 15023x   15022x 15022x 15022x 20x 20x 20x     15022x   15022x 7802x 3422x     4380x     7220x   7054x   3651x   3640x   123x   44x   23x   17x 7x 5x 1x   5x 5x   4x     2x     3x 213x     3x 3x 3x   3x    
import {
  Validation, StringType, ArrayType, ObjectType, SimpleTypes,
  isSimpleType, isArray, isEnum, isObj,
  isMap, isNumber, isMeta, isString, ValueType, isTypeDefValidation, ValueTypes, isAnd, AndType, MapType
} from './validationTypes.js'
 
type InputTypes = any | string | number | object | void | boolean | null
export type ValidationOutputs= ValidationOutput|ValidationOutput[]
export type ValidationOutput =
  | {[key: string]: ValidationOutputs}
  | null
  | {'error': string, output?: ValidationOutputs, value: any}
 
export type ValidationResult = {
  'result': 'pass'|'fail'
  output: ValidationOutputs
}
export type ValidationFailed = { message: string }
type SimpleValidation = string | null
type validateFn = (type: Validation, value: InputTypes) => ValidationResult
 
const failValidation = (error:string, value:any, output?: ValidationOutputs):ValidationResult => {
  const content: ValidationOutputs = { error, value }
  return {
    result: 'fail',
    output: output ? { ...content, output } : content
  }
}
 
export const combineValidationObjects = <T>(type:AndType, customTypes:any, onError: (input:any)=>T)
  :{result: 'error', error:T} | {pass: ObjectType, result? : void} => {
  const resolveMeta = (tpe: Validation) : Validation => {
    if (typeof tpe === 'string') { return resolveMeta(customTypes[tpe]) }
    if (isMeta(tpe)) return resolveMeta(tpe.$type)
    return tpe
  }
  const resolvedType = type.$and.map(x => resolveMeta(x))
 
  if (resolvedType.some(x => !isObj(x))) {
    return { error: onError(resolvedType), result: 'error' }
  }
 
  return {
    pass: resolvedType.reduce((prev:any, current:any) => {
      return { ...prev, ...current }
    }, {})
  }
}
 
const validateUndefined = (value: InputTypes): SimpleValidation =>
  !(value === undefined) ? 'Value is not undefined' : null
 
const validateNull = (value: InputTypes): SimpleValidation =>
  !(value === null) ? 'Value is not null' : null
 
const validateNumber = (value: InputTypes): SimpleValidation =>
  !Number.isFinite(value) || typeof value !== 'number' ? 'Value is not a number' : null
 
const validateNumberComplex = (value: InputTypes, min?: number, max?: number): SimpleValidation => {
  const res = validateNumber(value)
  if (!res) {
    if (min !== undefined && value < min) return 'Value is smaller than the required minimum'
    if (max !== undefined && value > max) return 'Value is bigger than the required maximum'
  }
  return res
}
const validateInteger = (value: InputTypes): SimpleValidation =>
  !Number.isSafeInteger(value) ? 'Value is not an integer ' : null
 
const validateString = (value: InputTypes, enums?: string[]): SimpleValidation => {
  if (typeof value !== 'string') return 'Value is not a string'
  else if (enums && enums.length && !enums.some(x => value === x)) { return `Value needs to be one of the following: [${enums.join(', ')}] ` }
  return null
}
 
const validateStringObject = (value: InputTypes, validator: StringType): SimpleValidation => {
  if (typeof value !== 'string') return 'Value is not a string'
  if (validator.$string.minLength && value.length < validator.$string.minLength) { return 'String is shorter than the required minimum length' }
  if (validator.$string.maxLength && value.length > validator.$string.maxLength) { return 'String is longer than the required maximum length' }
  if (validator.$string.regex) {
    const regex = new RegExp(validator.$string.regex, 'u')
    if (!regex.test(value)) return 'String did not match required regex'
  }
 
  return null
}
 
const validateBool = (value: InputTypes): SimpleValidation =>
  typeof value !== 'boolean' ? 'Value is not a boolean' : null
 
const validateOneOf = (value: InputTypes, validator: ValueType[], validate: validateFn):
 ValidationResult => {
  if (!validator.length) throw new Error('Array of types can not be empty')
 
  const errors: ValidationOutput[] = []
  for (const i of validator) {
    const result = validate(i, value)
    if (result.result === 'pass') return result
    if (Array.isArray(result.output)) result.output.forEach(x => errors.push(x))
    else errors.push(result.output)
  }
 
  return failValidation('Did not match any from the listed types', value, errors)
}
 
const validateArray = (value: InputTypes, validator: ArrayType, validate: validateFn):
ValidationResult => {
  if (Array.isArray(value)) {
    const maxLength = validator.maxLength || Number.MAX_SAFE_INTEGER
    const minLength = validator.minLength || 0
    if (value.length < minLength || value.length > maxLength) {
      return failValidation(
        `Array length needs to be between ${minLength} - ${maxLength}`,
        value.length)
    }
    const resultArray: ValidationOutputs[] = []
    let fail = false
    for (const x of value) {
      const res = validate(validator.$array, x)
      if (res.result === 'fail') fail = true
      resultArray.push(res.output)
    }
    return { result: fail ? 'fail' : 'pass', output: resultArray.flat() }
  }
  return failValidation('Value is not an Array', value)
}
 
const validateObject = (value: InputTypes, validator: ObjectType, validate: validateFn):
 ValidationResult => {
  if (typeof value !== 'object' || value === null || value === undefined) {
    return failValidation('Value is not an Object', value)
  }
 
  let fail = false
  const output: {[key: string]: ValidationOutputs} = {}
 
  for (const key of Object.keys(value)) {
    const validatorKey = key.startsWith('$') ? `\\${key}` : key
    if (!validator[validatorKey]) {
      fail = true
      output[key] = { error: 'Key does not exist on validator', value: value[key] }
    } else {
      const { result, output: outputInternal } = validate(validator[validatorKey], value[key])
      if (result === 'fail') fail = true
      output[key] = outputInternal
    }
  }
 
  for (const validatorKey of Object.keys(validator)) {
    const key = validatorKey.startsWith('\\$') ? validatorKey.slice(1) : validatorKey
    if (!Object.prototype.hasOwnProperty.call(output, key)) {
      const { result, output: outputInternal } = validate(validator[validatorKey], value[key])
      if (result === 'fail') fail = true
      output[key] = outputInternal
    }
  }
 
  return { result: fail ? 'fail' : 'pass', output }
}
 
const validateMap = (value: InputTypes, validator: MapType, validate: validateFn):
 ValidationResult => {
  if (typeof value !== 'object' || value === null || value === undefined) {
    return failValidation('Value is not an Object', value)
  }
 
  let fail = false
  const output: {[key: string]: ValidationOutputs} = {}
  const keys = Object.keys(value)
  const keyCount = keys.length
  const maxLength = validator.maxLength || Number.MAX_SAFE_INTEGER
  const minLength = validator.minLength || 0
  if (keyCount < minLength || keyCount > maxLength) {
    return failValidation(
        `Map needs to have member count to be between ${minLength} - ${maxLength}`,
        keyCount)
  }
  for (const key of keys) {
    if (validator.regex) {
      const regex = new RegExp(validator.regex, 'u')
      if (!regex.test(key)) {
        fail = true
        output[key] = { error: 'String did not match required regex', value: value }
        continue
      }
    }
    const { result, output: outputInternal } = validate(validator.$map, value[key])
    if (result === 'fail') fail = true
    output[key] = outputInternal
  }
 
  return { result: fail ? 'fail' : 'pass', output }
}
const simpleValidation = (type: SimpleTypes, value: any): SimpleValidation => {
  switch (type) {
    case 'any': return null
    case '?': return validateUndefined(value)
    case 'number': return validateNumber(value)
    case 'integer': return validateInteger(value)
    case 'string': return validateString(value)
    case 'boolean': return validateBool(value)
    case 'null': return validateNull(value)
    default: throw new Error(`Unknown validator:${JSON.stringify(type)}`)
  }
}
 
const toResult = (res: SimpleValidation, value: InputTypes): ValidationResult =>
  ({ result: res ? 'fail' : 'pass', output: res ? { error: res, value } : null })
 
const validateInternal = (typeIn: Validation, value: InputTypes, customTypesIn: {[key:string] : ValueTypes}): ValidationResult => {
  if (typeof typeIn === 'undefined') throw new Error('Type for validation cannot be undefined')
 
  let type: ValueTypes = typeIn
  let customTypes: {[key:string] : ValueTypes} = customTypesIn
  if (isTypeDefValidation(typeIn)) {
    customTypes = typeIn.$types
    type = { ...typeIn }
    delete type.$types
  }
 
  const validateCustom :validateFn = (type:Validation, value:any) => validateInternal(type, value, customTypes)
 
  if (isSimpleType(type)) {
    if (customTypes[type]) {
      return validateCustom(customTypes[type], value)
    }
 
    return toResult(simpleValidation(type, value), value)
  }
 
  if (Array.isArray(type)) { return validateOneOf(value, type, validateCustom) }
 
  if (isArray(type)) { return validateArray(value, type, validateCustom) }
 
  if (isEnum(type)) { return toResult(validateString(value, type.$enum), value) }
 
  if (isObj(type)) { return validateObject(value, type, validateCustom) }
 
  if (isMap(type)) { return validateMap(value, type, validateCustom) }
 
  if (isNumber(type)) { return toResult(validateNumberComplex(value, type.$number.min, type.$number.max), value) }
 
  if (isMeta(type)) { return validateCustom(type.$type, value) }
 
  if (isString(type)) { return toResult(validateStringObject(value, type), value) }
  if (isAnd(type)) {
    const onError = (resolvedType: any):ValidationResult =>
      failValidation('SCHEMA error: $and must only contain objects', resolvedType)
 
    const combined = combineValidationObjects(type, customTypes, onError)
    if (combined.result === 'error') return combined.error
 
    return validateObject(value, combined.pass, validateCustom)
  }
 
  throw new Error(`Unknown validator:${JSON.stringify(type)}`)
}
 
export const validate = (type: Validation, value: InputTypes): ValidationResult => {
  return validateInternal(type, value, {})
}
 
export const loadJson = (json: string | object): Validation => {
  const jsonOut: any = typeof json === 'string' ? JSON.parse(json) : json
  delete jsonOut.$schema
 
  return jsonOut
}