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

Function validator

commonjs/core.js:311–355  ·  view source on GitHub ↗

* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error. * @param {object} operation - operation object (patch) * @param {number} index - index of operation in the sequence * @param {object} [document] - object where the operation is supposed

(operation, index, document, existingPathFragment)

Source from the content-addressed store, hash-verified

309 * @param {string} [existingPathFragment] - comes along with `document`
310 */
311function validator(operation, index, document, existingPathFragment) {
312 if (typeof operation !== 'object' || operation === null || Array.isArray(operation)) {
313 throw new exports.JsonPatchError('Operation is not an object', 'OPERATION_NOT_AN_OBJECT', index, operation, document);
314 }
315 else if (!objOps[operation.op]) {
316 throw new exports.JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, document);
317 }
318 else if (typeof operation.path !== 'string') {
319 throw new exports.JsonPatchError('Operation `path` property is not a string', 'OPERATION_PATH_INVALID', index, operation, document);
320 }
321 else if (operation.path.indexOf('/') !== 0 && operation.path.length > 0) {
322 // paths that aren't empty string should start with "/"
323 throw new exports.JsonPatchError('Operation `path` property must start with "/"', 'OPERATION_PATH_INVALID', index, operation, document);
324 }
325 else if ((operation.op === 'move' || operation.op === 'copy') && typeof operation.from !== 'string') {
326 throw new exports.JsonPatchError('Operation `from` property is not present (applicable in `move` and `copy` operations)', 'OPERATION_FROM_REQUIRED', index, operation, document);
327 }
328 else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && operation.value === undefined) {
329 throw new exports.JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, document);
330 }
331 else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && helpers_js_1.hasUndefined(operation.value)) {
332 throw new exports.JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, document);
333 }
334 else if (document) {
335 if (operation.op == "add") {
336 var pathLen = operation.path.split("/").length;
337 var existingPathLen = existingPathFragment.split("/").length;
338 if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
339 throw new exports.JsonPatchError('Cannot perform an `add` operation at the desired path', 'OPERATION_PATH_CANNOT_ADD', index, operation, document);
340 }
341 }
342 else if (operation.op === 'replace' || operation.op === 'remove' || operation.op === '_get') {
343 if (operation.path !== existingPathFragment) {
344 throw new exports.JsonPatchError('Cannot perform the operation at a path that does not exist', 'OPERATION_PATH_UNRESOLVABLE', index, operation, document);
345 }
346 }
347 else if (operation.op === 'move' || operation.op === 'copy') {
348 var existingValue = { op: "_get", path: operation.from, value: undefined };
349 var error = validate([existingValue], document);
350 if (error && error.name === 'OPERATION_PATH_UNRESOLVABLE') {
351 throw new exports.JsonPatchError('Cannot perform the operation from a path that does not exist', 'OPERATION_FROM_UNRESOLVABLE', index, operation, document);
352 }
353 }
354 }
355}
356exports.validator = validator;
357/**
358 * 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 1

validateFunction · 0.70

Tested by

no test coverage detected