1 | export { _resolvePath as resolvePath };
|
2 | import makeJSONError from './errorReporting/makeJSONError';
|
3 |
|
4 | var validIdentifierOrAccessor = /^[$A-Z_][0-9A-Z_$[\].]*$/i;
|
5 |
|
6 | var Validation = function () {
|
7 | function Validation(context, input) {
|
8 | babelHelpers.classCallCheck(this, Validation);
|
9 | this.path = [];
|
10 | this.prefix = '';
|
11 | this.errors = [];
|
12 |
|
13 | this.context = context;
|
14 | this.input = input;
|
15 | }
|
16 |
|
17 | babelHelpers.createClass(Validation, [{
|
18 | key: 'hasErrors',
|
19 | value: function hasErrors(path) {
|
20 | if (path) {
|
21 | var _iteratorNormalCompletion = true;
|
22 | var _didIteratorError = false;
|
23 | var _iteratorError = undefined;
|
24 |
|
25 | try {
|
26 | for (var _iterator = this.errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
27 | var _ref = _step.value;
|
28 |
|
29 | var _ref2 = babelHelpers.slicedToArray(_ref, 1);
|
30 |
|
31 | var candidate = _ref2[0];
|
32 |
|
33 | if (matchPath(path, candidate)) {
|
34 | return true;
|
35 | }
|
36 | }
|
37 | } catch (err) {
|
38 | _didIteratorError = true;
|
39 | _iteratorError = err;
|
40 | } finally {
|
41 | try {
|
42 | if (!_iteratorNormalCompletion && _iterator.return) {
|
43 | _iterator.return();
|
44 | }
|
45 | } finally {
|
46 | if (_didIteratorError) {
|
47 | throw _iteratorError;
|
48 | }
|
49 | }
|
50 | }
|
51 |
|
52 | return false;
|
53 | } else {
|
54 | return this.errors.length > 0;
|
55 | }
|
56 | }
|
57 | }, {
|
58 | key: 'addError',
|
59 | value: function addError(path, expectedType, message) {
|
60 | this.errors.push([path, message, expectedType]);
|
61 | return this;
|
62 | }
|
63 | }, {
|
64 | key: 'clearError',
|
65 | value: function clearError(path) {
|
66 | var didClear = false;
|
67 | if (path) {
|
68 | var _errors = [];
|
69 | var _iteratorNormalCompletion2 = true;
|
70 | var _didIteratorError2 = false;
|
71 | var _iteratorError2 = undefined;
|
72 |
|
73 | try {
|
74 | for (var _iterator2 = this.errors[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
75 | var error = _step2.value;
|
76 |
|
77 | if (matchPath(path, error[0])) {
|
78 | didClear = true;
|
79 | } else {
|
80 | _errors.push(error);
|
81 | }
|
82 | }
|
83 | } catch (err) {
|
84 | _didIteratorError2 = true;
|
85 | _iteratorError2 = err;
|
86 | } finally {
|
87 | try {
|
88 | if (!_iteratorNormalCompletion2 && _iterator2.return) {
|
89 | _iterator2.return();
|
90 | }
|
91 | } finally {
|
92 | if (_didIteratorError2) {
|
93 | throw _iteratorError2;
|
94 | }
|
95 | }
|
96 | }
|
97 |
|
98 | this.errors = _errors;
|
99 | } else {
|
100 | didClear = this.errors.length > 0;
|
101 | this.errors = [];
|
102 | }
|
103 | return didClear;
|
104 | }
|
105 | }, {
|
106 | key: 'resolvePath',
|
107 | value: function resolvePath(path) {
|
108 | return _resolvePath(this.input, path);
|
109 | }
|
110 | }, {
|
111 | key: 'toJSON',
|
112 | value: function toJSON() {
|
113 | return makeJSONError(this);
|
114 | }
|
115 | }]);
|
116 | return Validation;
|
117 | }();
|
118 |
|
119 | export { Validation as default };
|
120 |
|
121 |
|
122 | export function stringifyPath(path) {
|
123 | if (!path.length) {
|
124 | return 'Value';
|
125 | }
|
126 | var length = path.length;
|
127 |
|
128 | var parts = new Array(length);
|
129 | for (var i = 0; i < length; i++) {
|
130 | var part = path[i];
|
131 | if (part === '[[Return Type]]') {
|
132 | parts[i] = 'Return Type';
|
133 | } else if (typeof part !== 'string' || !validIdentifierOrAccessor.test(part)) {
|
134 | parts[i] = `[${ String(part) }]`;
|
135 | } else if (i > 0) {
|
136 | parts[i] = `.${ part }`;
|
137 | } else {
|
138 | parts[i] = part;
|
139 | }
|
140 | }
|
141 | return parts.join('');
|
142 | }
|
143 |
|
144 | function _resolvePath(input, path) {
|
145 | var subject = input;
|
146 | var length = path.length;
|
147 |
|
148 | for (var i = 0; i < length; i++) {
|
149 | if (subject == null) {
|
150 | return undefined;
|
151 | }
|
152 | var part = path[i];
|
153 | if (part === '[[Return Type]]') {
|
154 | continue;
|
155 | }
|
156 | if (subject instanceof Map) {
|
157 | subject = subject.get(part);
|
158 | } else {
|
159 | subject = subject[part];
|
160 | }
|
161 | }
|
162 | return subject;
|
163 | }
|
164 |
|
165 | export function matchPath(path, candidate) {
|
166 | var length = path.length;
|
167 |
|
168 | if (length > candidate.length) {
|
169 | return false;
|
170 | }
|
171 | for (var i = 0; i < length; i++) {
|
172 | if (candidate[i] !== path[i]) {
|
173 | return false;
|
174 | }
|
175 | }
|
176 | return true;
|
177 | } |
\ | No newline at end of file |