1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getValidateList = exports.dealTypeValidate = exports.getValidateText = void 0;
|
7 |
|
8 | var _isLength = _interopRequireDefault(require("validator/lib/isLength"));
|
9 |
|
10 | var _color = _interopRequireDefault(require("color"));
|
11 |
|
12 | var _isHidden = require("./isHidden");
|
13 |
|
14 | var _utils = require("./utils");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
19 |
|
20 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
21 |
|
22 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
23 |
|
24 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
25 |
|
26 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
27 |
|
28 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
29 |
|
30 | var isEmptyObject = function isEmptyObject(obj) {
|
31 | return Object.keys(obj).length === 0 && obj.constructor === Object;
|
32 | };
|
33 |
|
34 |
|
35 | var isEmptyValue = function isEmptyValue(value, schema) {
|
36 |
|
37 | if (schema.type === 'array' && schema.enum) {
|
38 | return !value || value.length === 0;
|
39 | }
|
40 |
|
41 | if (value === 0) {
|
42 | return false;
|
43 | }
|
44 |
|
45 | return !value;
|
46 | };
|
47 |
|
48 | var getValidateText = function getValidateText() {
|
49 | var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
50 | var value = obj.value,
|
51 | defaultValue = obj.defaultValue,
|
52 | required = obj.required,
|
53 | _obj$schema = obj.schema,
|
54 | schema = _obj$schema === void 0 ? {} : _obj$schema;
|
55 | var type = schema.type,
|
56 | pattern = schema.pattern,
|
57 | message = schema.message,
|
58 | format = schema.format,
|
59 | widget = schema['ui:widget'],
|
60 | minLength = schema.minLength,
|
61 | maxLength = schema.maxLength,
|
62 | minimum = schema.minimum,
|
63 | maximum = schema.maximum,
|
64 | minItems = schema.minItems,
|
65 | maxItems = schema.maxItems,
|
66 | uniqueItems = schema.uniqueItems;
|
67 | var finalValue = value || defaultValue;
|
68 |
|
69 | if (type === 'number' && value === 0) {
|
70 | finalValue = 0;
|
71 | }
|
72 |
|
73 | var needPattern = pattern && ['string', 'number'].indexOf(type) > -1;
|
74 |
|
75 | if (isEmptyObject(schema)) {
|
76 | return false;
|
77 | }
|
78 |
|
79 |
|
80 | if (required && isEmptyValue(finalValue, schema)) {
|
81 | return message && message.required || '不能为空';
|
82 | }
|
83 |
|
84 |
|
85 | if (type === 'string' && finalValue) {
|
86 | if (maxLength && !(0, _isLength.default)(finalValue, 0, parseInt(maxLength, 10))) {
|
87 | return message && message.maxLength || "\u957F\u5EA6\u4E0D\u80FD\u5927\u4E8E ".concat(maxLength);
|
88 | }
|
89 |
|
90 | if ((minLength || minLength === 0) && !(0, _isLength.default)(finalValue, parseInt(minLength, 10), undefined)) {
|
91 | return message && message.minLength || "\u957F\u5EA6\u4E0D\u80FD\u5C0F\u4E8E ".concat(minLength);
|
92 | }
|
93 |
|
94 | if (format === 'color' || widget === 'color') {
|
95 | try {
|
96 | (0, _color.default)(finalValue);
|
97 | } catch (e) {
|
98 | return "\u989C\u8272\u4E0D\u5408\u6CD5";
|
99 | }
|
100 | }
|
101 | }
|
102 |
|
103 |
|
104 | if (type === 'number') {
|
105 | if (typeof finalValue !== 'number') {
|
106 | return '请填写数字';
|
107 | }
|
108 |
|
109 | if (maximum && parseInt(finalValue, 10) > maximum) {
|
110 | return message && message.maximum || "\u6570\u503C\u4E0D\u80FD\u5927\u4E8E ".concat(maximum);
|
111 | }
|
112 |
|
113 | if ((minimum || minimum === 0) && parseInt(finalValue, 10) < minimum) {
|
114 | return message && message.minimum || "\u6570\u503C\u4E0D\u80FD\u5C0F\u4E8E ".concat(minimum);
|
115 | }
|
116 | }
|
117 |
|
118 |
|
119 | if (finalValue && needPattern && !new RegExp(pattern).test(finalValue)) {
|
120 | return message && message.pattern || '格式不匹配';
|
121 | }
|
122 |
|
123 |
|
124 | if (type === 'array') {
|
125 | if (maxItems && finalValue && finalValue.length > maxItems) {
|
126 | return message && message.maxItems || "\u7EC4\u6570\u4E0D\u80FD\u5927\u4E8E ".concat(maxItems);
|
127 | }
|
128 |
|
129 | if ((minItems || minItems === 0) && finalValue && finalValue.length < minItems) {
|
130 | return message && message.minItems || "\u7EC4\u6570\u4E0D\u80FD\u5C0F\u4E8E ".concat(minItems);
|
131 | }
|
132 |
|
133 | if (uniqueItems && Array.isArray(finalValue) && finalValue.length > 1) {
|
134 | if (typeof uniqueItems === 'boolean') {
|
135 | if ((0, _utils.hasRepeat)(finalValue)) {
|
136 | return '存在重复元素';
|
137 | }
|
138 | }
|
139 |
|
140 | if (typeof uniqueItems === 'string') {
|
141 | var nameList = finalValue.map(function (item) {
|
142 | return item[uniqueItems];
|
143 | });
|
144 | var isRepeat = nameList.find(function (x, index) {
|
145 | return nameList.indexOf(x) !== index;
|
146 | });
|
147 |
|
148 | if (isRepeat) {
|
149 | return '存在重复的 ' + uniqueItems + ' 值';
|
150 | }
|
151 | }
|
152 | }
|
153 | }
|
154 |
|
155 | return false;
|
156 | };
|
157 |
|
158 | exports.getValidateText = getValidateText;
|
159 |
|
160 | var dealTypeValidate = function dealTypeValidate(key, value) {
|
161 | var schema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
162 | var checkList = [];
|
163 | var type = schema.type,
|
164 | items = schema.items;
|
165 | var obj = {
|
166 | value: value,
|
167 | schema: schema
|
168 | };
|
169 |
|
170 | if (type === 'object') {
|
171 | var list = getValidateList(value, schema);
|
172 |
|
173 | checkList.push.apply(checkList, _toConsumableArray(list));
|
174 | } else if (type === 'array') {
|
175 | value.forEach(function (v) {
|
176 | var list = dealTypeValidate(key, v, items);
|
177 | checkList.push.apply(checkList, _toConsumableArray(list));
|
178 | });
|
179 | }
|
180 |
|
181 | if (getValidateText(obj)) {
|
182 | checkList.push(key);
|
183 | }
|
184 |
|
185 | return checkList;
|
186 | };
|
187 |
|
188 |
|
189 | exports.dealTypeValidate = dealTypeValidate;
|
190 |
|
191 | var keyHidden = function keyHidden(schema, val) {
|
192 | var hidden = schema && schema['ui:hidden'];
|
193 |
|
194 | if (typeof hidden === 'string' && (0, _utils.isFunction)(hidden) === false) {
|
195 | hidden = (0, _isHidden.isHidden)({
|
196 | hidden: hidden,
|
197 | rootValue: val
|
198 | });
|
199 | }
|
200 |
|
201 | return hidden;
|
202 | };
|
203 |
|
204 | var getValidateList = function getValidateList() {
|
205 | var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
206 | var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
207 | var checkList = [];
|
208 | var properties = prop.properties,
|
209 | required = prop.required;
|
210 |
|
211 | if (required && required.length > 0) {
|
212 | required.forEach(function (key) {
|
213 | var schema = properties && properties[key] || {};
|
214 | var hidden = keyHidden(schema, val);
|
215 | var itemValue = val && val[key];
|
216 |
|
217 | if (isEmptyValue(itemValue, schema) && !hidden) {
|
218 | checkList.push(key);
|
219 | }
|
220 | });
|
221 | }
|
222 |
|
223 | if (properties && val && Object.keys(val) && Object.keys(val).length > 0) {
|
224 | Object.keys(val).forEach(function (key) {
|
225 | var value = val[key];
|
226 | var schema = properties[key] || {};
|
227 | var hidden = keyHidden(schema, val);
|
228 |
|
229 | if (!hidden) {
|
230 | var list = dealTypeValidate(key, value, schema);
|
231 | checkList.push.apply(checkList, _toConsumableArray(list));
|
232 | }
|
233 | });
|
234 | }
|
235 |
|
236 | return checkList;
|
237 | };
|
238 |
|
239 | exports.getValidateList = getValidateList; |
\ | No newline at end of file |