You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
882 B
JavaScript

5 years ago
"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;
}
};