1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import printValue from './util/printValue';
|
3 | var strReg = /\$\{\s*(\w+)\s*\}/g;
|
4 | export 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 | }
|
20 | ValidationError.prototype = Object.create(Error.prototype);
|
21 | ValidationError.prototype.constructor = ValidationError;
|
22 |
|
23 | ValidationError.isError = function (err) {
|
24 | return err && err.name === 'ValidationError';
|
25 | };
|
26 |
|
27 | ValidationError.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 |