UNPKG

1.37 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import printValue from './util/printValue';
3var strReg = /\$\{\s*(\w+)\s*\}/g;
4export default function ValidationError(errors, value, field, type) {
5 var _this = this;
6
7 this.name = 'ValidationError';
8 this.value = value;
9 this.path = field;
10 this.type = type;
11 this.errors = [];
12 this.inner = [];
13 if (errors) [].concat(errors).forEach(function (err) {
14 _this.errors = _this.errors.concat(err.errors || err);
15 if (err.inner) _this.inner = _this.inner.concat(err.inner.length ? err.inner : err);
16 });
17 this.message = this.errors.length > 1 ? this.errors.length + " errors occurred" : this.errors[0];
18 if (Error.captureStackTrace) Error.captureStackTrace(this, ValidationError);
19}
20ValidationError.prototype = Object.create(Error.prototype);
21ValidationError.prototype.constructor = ValidationError;
22
23ValidationError.isError = function (err) {
24 return err && err.name === 'ValidationError';
25};
26
27ValidationError.formatError = function (message, params) {
28 var path = params.label || params.path || 'this';
29 if (path !== params.path) params = _extends({}, params, {
30 path: path
31 });
32 if (typeof message === 'string') return message.replace(strReg, function (_, key) {
33 return printValue(params[key]);
34 });
35 if (typeof message === 'function') return message(params);
36 return message;
37};
\No newline at end of file