All files / json-patch validate.ts

88.35% Statements 167/189
73.33% Branches 66/90
100% Functions 26/26
100% Lines 144/144

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                                                8x 8x   8x 44x 43x 42x 44x 44x 44x   25x 25x         8x 298x 297x 297x 293x 291x   73x 72x   19x 19x   9x 9x   1x 1x   15x 14x   5x   11x 11x   11x 11x   4x 4x   6x 6x   11x 11x   7x 7x   119x       8x 131x 131x 131x   32x 31x   11x 8x   12x 12x   6x 6x   3x 3x 1x       19x 13x   5x 3x     8x 6x   3x 2x 2x     12x       15x 15x 12x 9x   5x       8x 73x     8x 19x     8x 9x     8x 1x 1x 1x     8x 15x 15x 15x 15x 15x     8x 32x 31x     8x 11x 11x 18x     8x 8x 20x     8x 12x 12x 12x     8x 6x 6x     8x 11x     8x 11x 11x     8x 4x 4x 4x 2x   2x       8x 6x 6x 4x     8x 11x 11x 11x     8x 7x 7x     8x     22x 18x     8x 105x     8x 25x 20x     8x 18x 4x     8x 49x     8x 53x     8x 35x 35x    
import type {
  Operation,
  OperationAdd,
  OperationRemove,
  OperationReplace,
  OperationCopy,
  OperationMove,
  OperationTest,
  OperationTestType,
  OperationTestString,
  OperationTestStringLen,
  OperationInc,
  OperationStrIns,
  OperationStrDel,
  OperationExtend,
  OperationMerge,
  OperationSplit,
  PredicateOperation,
  OperationContains,
  OperationEnds,
  OperationStarts,
  OperationMatches,
  JsonPatchTypes,
} from './types';
import {validateJsonPointer} from '@jsonjoy.com/json-pointer/lib/validate';
import {hasOwnProperty as hasOwnProp} from '@jsonjoy.com/util/lib/hasOwnProperty';
 
export const validateOperations = (ops: Operation[], allowMatchesOp: boolean = false) => {
  if (!Array.isArray(ops)) throw new Error('Not a array.');
  if (!ops.length) throw new Error('Empty operation patch.');
  for (let i = 0; i < ops.length; i++) {
    const op = ops[i];
    try {
      validateOperation(op, allowMatchesOp);
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      throw new Error(`Error in operation [index = ${i}] (${message}).`);
    }
  }
};
 
export const validateOperation = (op: Operation, allowMatchesOp: boolean) => {
  if (!op || typeof op !== 'object') throw new Error('OP_INVALID');
  const path = op.path;
  if (typeof path !== 'string') throw new Error('OP_PATH_INVALID');
  validateJsonPointer(path);
  switch (op.op) {
    case 'add':
      validateOperationAdd(op);
      break;
    case 'remove':
      validateOperationRemove(op);
      break;
    case 'replace':
      validateOperationReplace(op);
      break;
    case 'copy':
      validateOperationCopy(op);
      break;
    case 'move':
      validateOperationMove(op);
      break;
    case 'flip':
      break;
    case 'inc':
      validateOperationInc(op);
      break;
    case 'str_ins':
      validateOperationInsertText(op);
      break;
    case 'str_del':
      validateOperationRemoveText(op);
      break;
    case 'extend':
      validateOperationExtend(op);
      break;
    case 'merge':
      validateOperationMerge(op);
      break;
    case 'split':
      validateOperationSplit(op);
      break;
    default:
      validatePredicateOperation(op, allowMatchesOp);
  }
};
 
export const validatePredicateOperation = (op: PredicateOperation, allowMatchesOp: boolean) => {
  Iif (!op || typeof op !== 'object') throw new Error('OP_INVALID');
  validateJsonPointer(op.path);
  switch (op.op) {
    case 'test':
      validateOperationTest(op);
      break;
    case 'test_type':
      validateOperationTestType(op);
      break;
    case 'test_string':
      validateOperationTestString(op);
      break;
    case 'test_string_len':
      validateOperationTestStringLen(op);
      break;
    case 'matches':
      Iif (!allowMatchesOp) throw new Error('"matches" operation not allowed.');
      validateOperationPredicateWithValueAndCase(op);
      break;
    case 'contains':
    case 'ends':
    case 'starts':
      validateOperationPredicateWithValueAndCase(op);
      break;
    case 'in':
      if (!Array.isArray(op.value)) throw new Error('"in" operation "value" must be an array.');
      break;
    case 'more':
    case 'less':
      if (typeof op.value !== 'number') throw new Error('Value must be a number.');
      break;
    case 'type':
      validateValueString(op.value);
      validateTestType(op.value);
      break;
    case 'defined':
    case 'undefined':
      break;
    case 'and':
    case 'or':
    case 'not':
      Iif (!Array.isArray(op.apply)) throw new Error(`"${op.op}" predicate operators must be an array.`);
      if (!op.apply.length) throw new Error('Predicate list is empty.');
      for (const predicate of op.apply) validatePredicateOperation(predicate, allowMatchesOp);
      break;
    default:
      throw new Error('OP_UNKNOWN');
  }
};
 
const validateOperationAdd = (op: OperationAdd) => {
  validateValue(op.value);
};
 
const validateOperationRemove = (op: OperationRemove) => {
  Iif (hasOwnProp(op, 'oldValue') && op.oldValue === undefined) throw new Error('Invalid oldValue.');
};
 
const validateOperationReplace = (op: OperationReplace) => {
  Iif (hasOwnProp(op, 'oldValue') && op.oldValue === undefined) throw new Error('Invalid oldValue.');
};
 
const validateOperationCopy = (op: OperationCopy) => {
  const from = op.from;
  Iif (typeof from !== 'string') throw new Error('OP_FROM_INVALID');
  validateJsonPointer(from);
};
 
const validateOperationMove = (op: OperationMove) => {
  const from = op.from;
  Iif (typeof from !== 'string') throw new Error('OP_FROM_INVALID');
  validateJsonPointer(from);
  const {path} = op;
  if (path.indexOf(from + '/') === 0) throw new Error('Cannot move into own children.');
};
 
const validateOperationTest = (op: OperationTest) => {
  validateValue(op.value);
  validateNot(op.not);
};
 
const validateOperationTestType = (op: OperationTestType) => {
  Iif (!Array.isArray(op.type)) throw new Error('Invalid "type" field.');
  if (op.type.length < 1) throw new Error('Empty type list.');
  for (const type of op.type) validateTestType(type);
};
 
const validTypes = new Set(['string', 'number', 'boolean', 'object', 'integer', 'array', 'null']);
const validateTestType = (type: JsonPatchTypes) => {
  if (!validTypes.has(type)) throw new Error('Invalid type.');
};
 
const validateOperationTestString = (op: OperationTestString) => {
  validateNot(op.not);
  validateNonNegativeInteger(op.pos);
  Iif (typeof op.str !== 'string') throw new Error('Value must be a string.');
};
 
const validateOperationTestStringLen = (op: OperationTestStringLen) => {
  validateNot(op.not);
  validateNonNegativeInteger(op.len);
};
 
const validateOperationInc = (op: OperationInc) => {
  Iif (typeof op.inc !== 'number') throw new Error('Invalid "inc" value.');
};
 
const validateOperationInsertText = (op: OperationStrIns) => {
  validateNonNegativeInteger(op.pos);
  Iif (typeof op.str !== 'string') throw new Error('Expected a string "text" field.');
};
 
const validateOperationRemoveText = (op: OperationStrDel) => {
  validateNonNegativeInteger(op.pos);
  Iif (op.str === undefined && op.len === undefined) throw new Error('Either "text" or "pos" need to be set.');
  if (op.str !== undefined) {
    Iif (typeof op.str !== 'string') throw new Error('Expected a string "text" field.');
  } else {
    validateNonNegativeInteger(op.len!);
  }
};
 
const validateOperationExtend = (op: OperationExtend) => {
  Iif (!op.props || typeof op.props !== 'object') throw new Error('Invalid "props" field.');
  if (op.deleteNull !== undefined)
    Iif (typeof op.deleteNull !== 'boolean') throw new Error('Expected "deleteNull" field to be boolean.');
};
 
const validateOperationMerge = (op: OperationMerge) => {
  validateInteger(op.pos);
  Iif (op.pos < 1) throw new Error('Expected "pos" field to be greater than 0.');
  if (op.props) Iif (typeof op.props !== 'object') throw new Error('Invalid "props" field.');
};
 
const validateOperationSplit = (op: OperationSplit) => {
  validateInteger(op.pos);
  if (op.props) Iif (typeof op.props !== 'object') throw new Error('Invalid "props" field.');
};
 
const validateOperationPredicateWithValueAndCase = (
  op: OperationContains | OperationEnds | OperationStarts | OperationMatches,
) => {
  validateValueString(op.value);
  validateIgnoreCase(op.ignore_case);
};
 
const validateValue = (value: unknown) => {
  if (value === undefined) throw new Error('OP_VALUE_MISSING');
};
 
const validateValueString = (value: string) => {
  if (typeof value !== 'string') throw new Error('Expected "value" to be string.');
  Iif (value.length > 20_000) throw new Error('Value too long.');
};
 
const validateIgnoreCase = (ignore: boolean | undefined) => {
  if (ignore === undefined) return;
  if (typeof ignore !== 'boolean') throw new Error('Expected "ignore_case" to be a boolean.');
};
 
const validateNot = (not?: boolean) => {
  if (not !== undefined) Iif (typeof not !== 'boolean') throw new Error('Invalid "not" modifier.');
};
 
const validateInteger = (num: number) => {
  Iif (typeof num !== 'number' || num !== Math.round(num)) throw new Error('Not an integer.');
};
 
const validateNonNegativeInteger = (num: number) => {
  validateInteger(num);
  Iif (num < 0) throw new Error('Number is negative.');
};