* 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)
| 362 | * @returns {JsonPatchError|undefined} |
| 363 | */ |
| 364 | function 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 | } |
| 389 | exports.validate = validate; |
| 390 | // based on https://github.com/epoberezkin/fast-deep-equal |
| 391 | // MIT License |
no test coverage detected