commit 2b80b13ba050dde3e6c4ffaff037b39efc707da0 Author: Sven Slootweg Date: Fri May 15 22:52:08 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/example.js b/example.js new file mode 100644 index 0000000..2e6798a --- /dev/null +++ b/example.js @@ -0,0 +1,30 @@ +"use strict"; + +const annotateErrors = require("./"); +const ValidationError = require("@validatem/error"); + +let errors = [ + new ValidationError("Error A"), + new ValidationError("Error B", { path: [ "somewhere" ] }), +]; + +annotateErrors({ + errors: errors, + pathSegments: ["some", "path"] +}); + +console.log(errors); +/* +[ { ValidationError: Error A + + path: [ 'some', 'path' ], + ___validatem_isValidationError: true, + ___validatem_errorVersion: 1, + message: 'Error A' }, + { ValidationError: Error B + + path: [ 'some', 'path', 'somewhere' ], + ___validatem_isValidationError: true, + ___validatem_errorVersion: 1, + message: 'Error B' } ] +*/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..a2c3624 --- /dev/null +++ b/index.js @@ -0,0 +1,25 @@ +"use strict"; + +const matchValidationError = require("@validatem/match-validation-error"); + +module.exports = function annotateErrors(options) { + if (options.pathSegments == null) { + throw new Error(`'pathSegments' is a required option`); + } else if (!Array.isArray(options.pathSegments)) { + throw new Error(`'pathSegments' must be an array`); + } else if (options.errors == null) { + throw new Error(`'errors' is a required option`); + } else if (!Array.isArray(options.errors)) { + throw new Error(`'errors' must be an array`); + } else { + // NOTE: We mutate the error here, because Error objects are not safely cloneable + for (let error of options.errors) { + // Leave non-ValidationError errors alone, even though they should not be present + if (matchValidationError(error)) { + error.path = options.pathSegments.concat(error.path); + } + } + + return options.errors; + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..9648889 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "@validatem/annotate-errors", + "description": "Utility for annotating one or more ValidationErrors with the path at which they occurred", + "version": "0.1.0", + "main": "index.js", + "repository": "http://git.cryto.net/validatem/annotate-errors.git", + "author": "Sven Slootweg ", + "license": "WTFPL OR CC0-1.0", + "dependencies": { + "@validatem/match-validation-error": "^0.1.0" + }, + "devDependencies": { + "@validatem/error": "^1.0.0" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..12d34a4 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,27 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@validatem/error@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@validatem/error/-/error-1.0.0.tgz#a975904aa4c3e7618d89088a393567a5e1778340" + integrity sha512-7M3tV4DhCuimuCRdC2L/topBByDjhzspzeQGNU0S4/mdn2aDNtESYE43K/2Kh/utCAhqXh2gyw89WYxy//t3fQ== + dependencies: + create-error "^0.3.1" + +"@validatem/match-validation-error@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@validatem/match-validation-error/-/match-validation-error-0.1.0.tgz#fa87f5f1836e7c1d9bf6b75b2addf0a5b21e4c1e" + integrity sha512-6akGTk7DdulOreyqDiGdikwRSixQz/AlvARSX18dcWaTFc79KxCLouL2hyoFcor9IIUhu5RTY4/i756y4T1yxA== + dependencies: + "@validatem/match-versioned-special" "^0.1.0" + +"@validatem/match-versioned-special@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@validatem/match-versioned-special/-/match-versioned-special-0.1.0.tgz#2eacc48debecdbbe7e3d02f0c0a665afaea9bedf" + integrity sha512-xoOTY0bdA2ELj+ntcDVJ8YyMEFIJpjZ4HNPL9lGcbnRFwJBhQcHUAhUpZwkMxu02zH9wkNM1FvYGHxPz40745Q== + +create-error@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/create-error/-/create-error-0.3.1.tgz#69810245a629e654432bf04377360003a5351a23" + integrity sha1-aYECRaYp5lRDK/BDdzYAA6U1GiM=