ng-layout-form
Version:
layout form
147 lines (145 loc) • 4.43 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/form/volidRule.ts
var volidRule_exports = {};
__export(volidRule_exports, {
initRules: () => initRules,
mergeRules: () => mergeRules
});
module.exports = __toCommonJS(volidRule_exports);
var typeSet = /* @__PURE__ */ new Set([
"string",
"number",
"boolean",
"method",
"regexp",
"integer",
"float",
"array",
"object",
"enum",
"date",
"hex",
"email",
"url",
"any"
]);
function initRules(formConf) {
const fields = formConf.children;
if (!fields || !Array.isArray(fields)) {
return {};
}
let defaultRules = {};
fields.forEach((item) => {
var _a, _b;
const { name, onlyRequiredStyle = false } = item;
const nameKey = Array.isArray(name) ? name.join(".") : name;
const comType = ((_b = (_a = item.xtype) == null ? void 0 : _a.toLowerCase) == null ? void 0 : _b.call(_a)) || "";
const skipRules = [
"attachment",
"ngattachment",
"select",
"singlehelp",
"multiplehelp",
"ngsinglehelp",
"ngselect",
"ngmultiplehelp"
].includes(comType);
let rule = [];
const type = item.valueType;
if (typeSet.has(type)) {
rule.push({ type, message: `必须是${type}类型` });
}
if (item.required && !onlyRequiredStyle) {
if (["attachment", "ngattachment"].includes(comType)) {
rule.push({
validator: async (_, value) => {
var _a2, _b2, _c, _d;
const attachmentCount = await ((_d = (_c = (_b2 = (_a2 = formConf.outRef) == null ? void 0 : _a2.current) == null ? void 0 : _b2.getItem(nameKey)) == null ? void 0 : _c.getCount) == null ? void 0 : _d.call(_c));
if (!value || attachmentCount === 0) {
return Promise.reject(`请上传${item.label || ""}`);
} else {
return Promise.resolve();
}
}
});
} else {
rule.push({
required: item.required && item.required !== "false",
message: `请输入${item.label || ""}`
});
}
}
if (item.maxLength && !skipRules) {
rule.push({
max: item.maxLength,
type: "string",
message: `${item.label || ""}最长为${item.maxLength}个字符`
});
}
if (item.min && !skipRules) {
rule.push({
validator: (_, value) => {
const v = +value;
if (isNaN(v) || v >= item.min) {
return Promise.resolve();
} else {
return Promise.reject(`${item.label || ""}最小为${item.min}`);
}
}
});
}
if (item.max && !skipRules) {
rule.push({
validator: (_, value) => {
const v = +value;
if (isNaN(v) || v <= item.max) {
return Promise.resolve();
} else {
return Promise.reject(`${item.label || ""}最大为${item.max}`);
}
}
});
}
if (item.customRule) {
const { test, message } = item.customRule;
rule.push({ validator: test, message });
}
if (item.rules) {
Array.isArray(item.rules) ? rule.push([...item.rules]) : rule.push(item.rules);
}
defaultRules[name] = rule;
if (item.xtype === "container" && Array.isArray(item.items || item.children)) {
defaultRules = {
...defaultRules,
...initRules({ children: item.items || item.children })
};
}
});
return defaultRules;
}
function mergeRules(oldRules, newRules) {
if (!newRules)
return oldRules;
return { ...oldRules, ...newRules };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
initRules,
mergeRules
});