UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

152 lines (145 loc) 4.56 kB
import _createForOfIteratorHelperLoose from "@babel/runtime/helpers/esm/createForOfIteratorHelperLoose"; import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; import { difference, find, includes, intersection, isPlainObject, startsWith } from "../utils/index.js"; import { VaeLocale } from "./VaeLocale.js"; import { VaeSchema } from "./VaeSchema.js"; export var VaeObjectSchema = /*#__PURE__*/function (_VaeSchema) { _inheritsLoose(VaeObjectSchema, _VaeSchema); function VaeObjectSchema(shape, message) { var _this; if (message === void 0) { message = VaeLocale.object.type; } _this = _VaeSchema.call(this, { type: 'object' }) || this; _this.check({ fn: isPlainObject, message: message }); if (shape) { _this.shape(shape); } return _this; } /** * 对象定义 */ var _proto = VaeObjectSchema.prototype; _proto.shape = function shape(_shape) { var _this2 = this; var keys = Object.keys(_shape); this._options.objectKeys = keys; keys.forEach(function (key) { _this2.check({ fn: _shape[key], path: [key], message: '', tag: "field_" + key }); }); return this; } /** * 原地挑选给定字段 */; _proto.pickFields = function pickFields(keys) { this._options.processors = this._options.processors.filter(function (item) { return typeof item === 'object' && startsWith(item.tag, 'field_') ? includes(keys, item.path[0]) : true; }); this._options.objectKeys = intersection(this._options.objectKeys, keys); return this; } /** * 原地剔除给定字段 */; _proto.omitFields = function omitFields(keys) { this._options.processors = this._options.processors.filter(function (item) { return typeof item === 'object' && startsWith(item.tag, 'field_') ? !includes(keys, item.path[0]) : true; }); this._options.objectKeys = difference(this._options.objectKeys, keys); return this; } /** * 原地将给定字段设为可选 */; _proto.optionalFields = function optionalFields(keys) { this._options.processors.forEach(function (item) { if (typeof item === 'object' && startsWith(item.tag, 'field_') && (keys ? includes(keys, item.path[0]) : true)) { ; item.fn.optional(); } }); return this; } /** * 原地将给定字段设为必填 */; _proto.requiredFields = function requiredFields(keys) { this._options.processors.forEach(function (item) { if (typeof item === 'object' && startsWith(item.tag, 'field_') && (keys ? includes(keys, item.path[0]) : true)) { ; item.fn.required(); } }); return this; } /** * 返回给定字段的定义 */; _proto.shapeOfFields = function shapeOfFields(keys) { var shape = {}; this._options.processors.forEach(function (item) { if (typeof item === 'object' && startsWith(item.tag, 'field_') && (keys ? includes(keys, item.path[0]) : true)) { shape[item.path[0]] = item.fn; } }); return shape; } /** * 给定字段中至少有一个必填 */; _proto.requiredFieldsAtLeastOne = function requiredFieldsAtLeastOne(keys, message) { var _this3 = this; if (message === void 0) { message = VaeLocale.object.requiredFieldsAtLeastOne; } return this.check({ fn: function fn(v) { var _loop = function _loop() { var key = _step.value; if (v[key] == null) { return 0; // continue } if (v[key] === '') { var schema = find(_this3._options.processors, function (item) { return typeof item === 'object' && startsWith(item.tag, 'field_') && item.path[0] === key; }); if (!schema) { return 0; // continue } if (schema.options.type === 'string' && !schema.options.stringEmptyable) { return 0; // continue } } return { v: true }; }, _ret; for (var _iterator = _createForOfIteratorHelperLoose(keys), _step; !(_step = _iterator()).done;) { _ret = _loop(); if (_ret === 0) continue; if (_ret) return _ret.v; } return false; }, message: message, messageParams: { keys: keys }, tag: 'requiredFieldsAtLeastOne' }); }; return VaeObjectSchema; }(VaeSchema);