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