UNPKG

7.97 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getValidateList = exports.dealTypeValidate = exports.getValidateText = void 0;
7
8var _isLength = _interopRequireDefault(require("validator/lib/isLength"));
9
10var _color = _interopRequireDefault(require("color"));
11
12var _isHidden = require("./isHidden");
13
14var _utils = require("./utils");
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
19
20function _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
22function _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
24function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
25
26function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
27
28function _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
30var isEmptyObject = function isEmptyObject(obj) {
31 return Object.keys(obj).length === 0 && obj.constructor === Object;
32}; // 值是是否为空
33
34
35var 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
48var 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; // fix: number = 0 返回空字符串
68
69 if (type === 'number' && value === 0) {
70 finalValue = 0;
71 }
72
73 var needPattern = pattern && ['string', 'number'].indexOf(type) > -1; // schema 里面没有内容的,直接退出
74
75 if (isEmptyObject(schema)) {
76 return false;
77 } // 校验是否为required
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
158exports.getValidateText = getValidateText;
159
160var 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); // eslint-disable-line
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}; // for backward compatibility
187
188
189exports.dealTypeValidate = dealTypeValidate;
190
191var 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
204var 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; // 校验必填(required 属性只在 type:object 下存在)
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
239exports.getValidateList = getValidateList;
\No newline at end of file