55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = void 0;
|
|
|
|
var _printValue = _interopRequireDefault(require("./util/printValue"));
|
|
|
|
var _toArray = _interopRequireDefault(require("./util/toArray"));
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
|
|
let strReg = /\$\{\s*(\w+)\s*\}/g;
|
|
|
|
class ValidationError extends Error {
|
|
static formatError(message, params) {
|
|
const path = params.label || params.path || 'this';
|
|
if (path !== params.path) params = _extends({}, params, {
|
|
path
|
|
});
|
|
if (typeof message === 'string') return message.replace(strReg, (_, key) => (0, _printValue.default)(params[key]));
|
|
if (typeof message === 'function') return message(params);
|
|
return message;
|
|
}
|
|
|
|
static isError(err) {
|
|
return err && err.name === 'ValidationError';
|
|
}
|
|
|
|
constructor(errorOrErrors, value, field, type) {
|
|
super();
|
|
this.name = 'ValidationError';
|
|
this.value = value;
|
|
this.path = field;
|
|
this.type = type;
|
|
this.errors = [];
|
|
this.inner = [];
|
|
(0, _toArray.default)(errorOrErrors).forEach(err => {
|
|
if (ValidationError.isError(err)) {
|
|
this.errors.push(...err.errors);
|
|
this.inner = this.inner.concat(err.inner.length ? err.inner : err);
|
|
} else {
|
|
this.errors.push(err);
|
|
}
|
|
});
|
|
this.message = this.errors.length > 1 ? `${this.errors.length} errors occurred` : this.errors[0];
|
|
if (Error.captureStackTrace) Error.captureStackTrace(this, ValidationError);
|
|
}
|
|
|
|
}
|
|
|
|
exports.default = ValidationError; |