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

Function validate

commonjs/core.js:364–388  ·  view source on GitHub ↗

* Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document. * If error is encountered, returns a JsonPatchError object * @param sequence * @param document * @returns {JsonPatchError|undefined}

(sequence, document, externalValidator)

Source from the content-addressed store, hash-verified

362 * @returns {JsonPatchError|undefined}
363 */
364function validate(sequence, document, externalValidator) {
365 try {
366 if (!Array.isArray(sequence)) {
367 throw new exports.JsonPatchError('Patch sequence must be an array', 'SEQUENCE_NOT_AN_ARRAY');
368 }
369 if (document) {
370 //clone document and sequence so that we can safely try applying operations
371 applyPatch(helpers_js_1._deepClone(document), helpers_js_1._deepClone(sequence), externalValidator || true);
372 }
373 else {
374 externalValidator = externalValidator || validator;
375 for (var i = 0; i < sequence.length; i++) {
376 externalValidator(sequence[i], i, document, undefined);
377 }
378 }
379 }
380 catch (e) {
381 if (e instanceof exports.JsonPatchError) {
382 return e;
383 }
384 else {
385 throw e;
386 }
387 }
388}
389exports.validate = validate;
390// based on https://github.com/epoberezkin/fast-deep-equal
391// MIT License

Callers 1

validatorFunction · 0.70

Calls 1

applyPatchFunction · 0.70

Tested by

no test coverage detected