UNPKG

@antdv/pro-utils

Version:

@antdv/pro-utils

300 lines (299 loc) 8.77 kB
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); var stdin_exports = {}; __export(stdin_exports, { anyType: () => anyType, arrayOf: () => arrayOf, arrayType: () => arrayType, boolType: () => boolType, custom: () => custom, funcType: () => funcType, nullable: () => nullable, numberType: () => numberType, objectOf: () => objectOf, objectType: () => objectType, oneOf: () => oneOf, oneOfType: () => oneOfType, shape: () => shape, stringType: () => stringType, symbol: () => symbol, vNodeType: () => vNodeType }); module.exports = __toCommonJS(stdin_exports); var import_is = require("../is"); var import_utils = require("./utils"); const anyType = () => (0, import_utils.toValidateType)("any", {}); const vNodeType = () => (0, import_utils.toValidateType)("vnode", {}); function funcType() { return (0, import_utils.toValidateType)("function", { type: Function }); } function boolType() { return (0, import_utils.toValidateType)("boolean", { type: Boolean }); } function stringType() { return (0, import_utils.toValidateType)("string", { type: String }); } function numberType() { return (0, import_utils.toValidateType)("number", { type: Number }); } function arrayType() { return (0, import_utils.toValidateType)("array", { type: Array }); } function arrayOf(type) { return (0, import_utils.toType)("arrayOf", { type: Array, validator(values) { let vResult = ""; const valid = values.every((value) => { vResult = (0, import_utils.validateType)(type, value, true); return vResult === true; }); if (!valid) { (0, import_utils.warn)(`arrayOf - value validation error: ${(0, import_utils.indent)(vResult)}`); } return valid; } }); } function objectType() { return (0, import_utils.toValidateType)("object", { type: Object }); } function objectOf(type) { return (0, import_utils.toType)("objectOf", { type: Object, validator(obj) { let vResult = ""; const valid = Object.keys(obj).every((key) => { vResult = (0, import_utils.validateType)(type, obj[key], true); return vResult === true; }); if (!valid) { (0, import_utils.warn)(`objectOf - value validation error: ${(0, import_utils.indent)(vResult)}`); } return valid; } }); } function symbol() { return (0, import_utils.toType)("symbol", { validator(value) { const res = typeof value === "symbol"; if (res === false) { (0, import_utils.warn)(`symbol - invalid value "${value}"`); } return res; } }); } function nullable() { return Object.defineProperty( { type: null, validator(value) { const res = value === null; if (res === false) { (0, import_utils.warn)(`nullable - value should be null`); } return res; } }, "_vueTypes_name", { value: "nullable" } ); } function custom(validatorFn, warnMsg = "custom validation failed") { if (typeof validatorFn !== "function") { throw new TypeError( "[VueTypes error]: You must provide a function as argument" ); } return (0, import_utils.toType)(validatorFn.name || "<<anonymous function>>", { type: null, validator(value) { const valid = validatorFn(value); if (!valid) (0, import_utils.warn)(`${this._vueTypes_name} - ${warnMsg}`); return valid; } }); } function oneOf(arr) { if (!(0, import_is.isArray)(arr)) { throw new TypeError( "[VueTypes error]: You must provide an array as argument." ); } const msg = `oneOf - value should be one of "${arr.map((v) => typeof v === "symbol" ? v.toString() : v).join('", "')}".`; const base = { validator(value) { const valid = arr.includes(value); if (!valid) (0, import_utils.warn)(msg); return valid; } }; if (!arr.includes(null)) { const type = arr.reduce( (ret, v) => { if (v !== null && v !== void 0) { const constr = v.constructor; !ret.includes(constr) && ret.push(constr); } return ret; }, [] ); if (type.length > 0) { base.type = type; } } return (0, import_utils.toType)("oneOf", base); } function shape(obj) { const keys = Object.keys(obj); const requiredKeys = keys.filter((key) => { var _a; return !!((_a = obj[key]) == null ? void 0 : _a.required); }); const type = (0, import_utils.toType)("shape", { type: Object, validator(value) { if (!(0, import_utils.isPlainObject)(value)) { return false; } const valueKeys = Object.keys(value); if (requiredKeys.length > 0 && requiredKeys.some((req) => !valueKeys.includes(req))) { const missing = requiredKeys.filter( (req) => !valueKeys.includes(req) ); if (missing.length === 1) { (0, import_utils.warn)(`shape - required property "${missing[0]}" is not defined.`); } else { (0, import_utils.warn)( `shape - required properties "${missing.join( '", "' )}" are not defined.` ); } return false; } return valueKeys.every((key) => { if (!keys.includes(key)) { if (this._vueTypes_isLoose === true) return true; (0, import_utils.warn)( `shape - shape definition does not include a "${key}" property. Allowed keys: "${keys.join( '", "' )}".` ); return false; } const _type = obj[key]; const valid = (0, import_utils.validateType)(_type, value[key], true); if (typeof valid === "string") { (0, import_utils.warn)(`shape - "${key}" property validation error: ${(0, import_utils.indent)(valid)}`); } return valid === true; }); } }); Object.defineProperty(type, "_vueTypes_isLoose", { writable: true, value: false }); Object.defineProperty(type, "loose", { get() { this._vueTypes_isLoose = true; return this; } }); return type; } function oneOfType(arr) { if (!(0, import_is.isArray)(arr)) { throw new TypeError( "[VueTypes error]: You must provide an array as argument" ); } let hasCustomValidators = false; let hasNullable = false; let nativeChecks = []; for (let i = 0; i < arr.length; i += 1) { const type = arr[i]; if ((0, import_utils.isComplexType)(type)) { if ((0, import_is.isFunction)(type.validator)) { hasCustomValidators = true; } if ((0, import_utils.isVueTypeDef)(type, "oneOf") && type.type) { nativeChecks = nativeChecks.concat(type.type); continue; } if ((0, import_utils.isVueTypeDef)(type, "nullable")) { hasNullable = true; continue; } if (type.type === true || !type.type) { (0, import_utils.warn)('oneOfType - invalid usage of "true" and "null" as types.'); continue; } nativeChecks = nativeChecks.concat(type.type); } else { nativeChecks.push(type); } } nativeChecks = nativeChecks.filter((t, i) => nativeChecks.indexOf(t) === i); const typeProp = hasNullable === false && nativeChecks.length > 0 ? nativeChecks : null; if (!hasCustomValidators) { return (0, import_utils.toType)("oneOfType", { type: typeProp }); } return (0, import_utils.toType)("oneOfType", { type: typeProp, validator(value) { const err = []; const valid = arr.some((type) => { const res = (0, import_utils.validateType)(type, value, true); if (typeof res === "string") { err.push(res); } return res === true; }); if (!valid) { (0, import_utils.warn)( `oneOfType - provided value does not match any of the ${err.length} passed-in validators: ${(0, import_utils.indent)(err.join("\n"))}` ); } return valid; } }); }