1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/builtin/interopRequireDefault");
|
4 |
|
5 | exports.__esModule = true;
|
6 | exports.default = ValidationError;
|
7 |
|
8 | var _printValue = _interopRequireDefault(require("./util/printValue"));
|
9 |
|
10 | var strReg = /\$\{\s*(\w+)\s*\}/g;
|
11 |
|
12 | var replace = function replace(str) {
|
13 | return function (params) {
|
14 | return str.replace(strReg, function (_, key) {
|
15 | return (0, _printValue.default)(params[key]);
|
16 | });
|
17 | };
|
18 | };
|
19 |
|
20 | function ValidationError(errors, value, field, type) {
|
21 | var _this = this;
|
22 |
|
23 | this.name = 'ValidationError';
|
24 | this.value = value;
|
25 | this.path = field;
|
26 | this.type = type;
|
27 | this.errors = [];
|
28 | this.inner = [];
|
29 | if (errors) [].concat(errors).forEach(function (err) {
|
30 | _this.errors = _this.errors.concat(err.errors || err);
|
31 | if (err.inner) _this.inner = _this.inner.concat(err.inner.length ? err.inner : err);
|
32 | });
|
33 | this.message = this.errors.length > 1 ? this.errors.length + " errors occurred" : this.errors[0];
|
34 | if (Error.captureStackTrace) Error.captureStackTrace(this, ValidationError);
|
35 | }
|
36 |
|
37 | ValidationError.prototype = Object.create(Error.prototype);
|
38 | ValidationError.prototype.constructor = ValidationError;
|
39 |
|
40 | ValidationError.isError = function (err) {
|
41 | return err && err.name === 'ValidationError';
|
42 | };
|
43 |
|
44 | ValidationError.formatError = function (message, params) {
|
45 | if (typeof message === 'string') message = replace(message);
|
46 |
|
47 | var fn = function fn(params) {
|
48 | params.path = params.label || params.path || 'this';
|
49 | return typeof message === 'function' ? message(params) : message;
|
50 | };
|
51 |
|
52 | return arguments.length === 1 ? fn : fn(params);
|
53 | };
|
54 |
|
55 | module.exports = exports["default"]; |
\ | No newline at end of file |