MCPcopy Create free account
hub / github.com/Starcounter-Jack/JSON-Patch / validator

Function validator

src/core.ts:375–426  ·  view source on GitHub ↗
(operation: Operation, index: number, document?: any, existingPathFragment?: string)

Source from the content-addressed store, hash-verified

373 * @param {string} [existingPathFragment] - comes along with `document`
374 */
375export function validator(operation: Operation, index: number, document?: any, existingPathFragment?: string): void {
376 if (typeof operation !== 'object' || operation === null || Array.isArray(operation)) {
377 throw new JsonPatchError('Operation is not an object', 'OPERATION_NOT_AN_OBJECT', index, operation, document);
378 }
379
380 else if (!objOps[operation.op]) {
381 throw new JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, document);
382 }
383
384 else if (typeof operation.path !== 'string') {
385 throw new JsonPatchError('Operation `path` property is not a string', 'OPERATION_PATH_INVALID', index, operation, document);
386 }
387
388 else if (operation.path.indexOf('/') !== 0 && operation.path.length > 0) {
389 // paths that aren't empty string should start with "/"
390 throw new JsonPatchError('Operation `path` property must start with "/"', 'OPERATION_PATH_INVALID', index, operation, document);
391 }
392
393 else if ((operation.op === 'move' || operation.op === 'copy') && typeof operation.from !== 'string') {
394 throw new JsonPatchError('Operation `from` property is not present (applicable in `move` and `copy` operations)', 'OPERATION_FROM_REQUIRED', index, operation, document);
395 }
396
397 else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && operation.value === undefined) {
398 throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, document);
399 }
400
401 else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) {
402 throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, document);
403 }
404
405 else if (document) {
406 if (operation.op == "add") {
407 var pathLen = operation.path.split("/").length;
408 var existingPathLen = existingPathFragment.split("/").length;
409 if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
410 throw new JsonPatchError('Cannot perform an `add` operation at the desired path', 'OPERATION_PATH_CANNOT_ADD', index, operation, document);
411 }
412 }
413 else if (operation.op === 'replace' || operation.op === 'remove' || (<any>operation.op) === '_get') {
414 if (operation.path !== existingPathFragment) {
415 throw new JsonPatchError('Cannot perform the operation at a path that does not exist', 'OPERATION_PATH_UNRESOLVABLE', index, operation, document);
416 }
417 }
418 else if (operation.op === 'move' || operation.op === 'copy') {
419 var existingValue: any = { op: "_get", path: operation.from, value: undefined };
420 var error = validate([existingValue], document);
421 if (error && error.name === 'OPERATION_PATH_UNRESOLVABLE') {
422 throw new JsonPatchError('Cannot perform the operation from a path that does not exist', 'OPERATION_FROM_UNRESOLVABLE', index, operation, document);
423 }
424 }
425 }
426}
427
428/**
429 * Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.

Callers 1

applyOperationFunction · 0.70

Calls 2

hasUndefinedFunction · 0.90
validateFunction · 0.70

Tested by

no test coverage detected