UNPKG

vtils

Version:

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

157 lines (149 loc) 4.96 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; exports.__esModule = true; exports.VaeObjectSchema = void 0; var _createForOfIteratorHelperLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelperLoose")); var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _utils = require("../utils"); var _VaeLocale = require("./VaeLocale"); var _VaeSchema2 = require("./VaeSchema"); var VaeObjectSchema = exports.VaeObjectSchema = /*#__PURE__*/function (_VaeSchema) { (0, _inheritsLoose2.default)(VaeObjectSchema, _VaeSchema); function VaeObjectSchema(shape, message) { var _this; if (message === void 0) { message = _VaeLocale.VaeLocale.object.type; } _this = _VaeSchema.call(this, { type: 'object' }) || this; _this.check({ fn: _utils.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' && (0, _utils.startsWith)(item.tag, 'field_') ? (0, _utils.includes)(keys, item.path[0]) : true; }); this._options.objectKeys = (0, _utils.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' && (0, _utils.startsWith)(item.tag, 'field_') ? !(0, _utils.includes)(keys, item.path[0]) : true; }); this._options.objectKeys = (0, _utils.difference)(this._options.objectKeys, keys); return this; } /** * 原地将给定字段设为可选 */; _proto.optionalFields = function optionalFields(keys) { this._options.processors.forEach(function (item) { if (typeof item === 'object' && (0, _utils.startsWith)(item.tag, 'field_') && (keys ? (0, _utils.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' && (0, _utils.startsWith)(item.tag, 'field_') && (keys ? (0, _utils.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' && (0, _utils.startsWith)(item.tag, 'field_') && (keys ? (0, _utils.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.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 = (0, _utils.find)(_this3._options.processors, function (item) { return typeof item === 'object' && (0, _utils.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 = (0, _createForOfIteratorHelperLoose2.default)(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; }(_VaeSchema2.VaeSchema);