UNPKG

aptx-validator

Version:

轻量易用的 Node.js + Typescript 参数校验库

175 lines (174 loc) 8.22 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _StringValidator_instances, _StringValidator_validationTasks, _StringValidator_optional, _StringValidator_addValidation; Object.defineProperty(exports, "__esModule", { value: true }); const AllValidator_1 = __importDefault(require("./AllValidator")); const Utils_1 = __importStar(require("./Utils")); /** * Validator for String parameters, which includes multiple String validation methods. */ class StringValidator extends AllValidator_1.default { constructor() { super(); _StringValidator_instances.add(this); _StringValidator_validationTasks.set(this, []); _StringValidator_optional.set(this, false); this.output = ""; __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.string()); } custom(fn) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, fn); return this; } optional() { __classPrivateFieldSet(this, _StringValidator_optional, true, "f"); return this; } /** * Set minimum length of a string. * Returns true if string length >= min. * This method will be tested when StringValidator.result() is called. * @param {number} min minimum length * @returns {StringValidator} StringValidator */ minLength(min) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.minLength(min)); return this; } /** * Set maximum length of a string. * Returns true if string length <= max. * This method will be tested when StringValidator.result() is called. * @param {number} max maximum length * @returns {StringValidator} StringValidator */ maxLength(max) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.maxLength(max)); return this; } /** * Check if string is numeric. * Returns true if string is numeric. * This method will be tested when StringValidator.result() is called. * @returns {StringValidator} StringValidator */ numeric() { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.numeric()); return this; } /** * Set fixed length of a numeric string. * Returns true if the fixed length equals. * This method will be tested when StringValidator.result() is called. * @param {number} len fixed length * @returns {StringValidator} StringValidator */ toFixed(len) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.toFixed(len)); return this; } /** * Use customized Regular Expression. * Returns true if the Regular Expression matches. * This method will be tested when StringValidator.result() is called. * @param {RegExp} re self defined Regular Expression * @returns {StringValidator} StringValidator */ useRE(re) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.useRE(re)); return this; } /** * Check if string is an email. * Returns true if the (default) Regular Expression matches. * This method will be tested when StringValidator.result() is called. * @param {?RegExp} emailRE self defined email-checking Regular Expression * @returns {StringValidator} StringValidator */ email(emailRE) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.email(emailRE)); return this; } /** * Check if string is a phone number. * Returns true if the (default) Regular Expression matches. * This method will be tested when StringValidator.result() is called. * @param {?RegExp} phoneRE self defined phone-checking Regular Expression * @returns {StringValidator} StringValidator */ phone(phoneRE) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.phone(phoneRE)); return this; } /** * Check if string is a boolean string. * Returns true if string === 'true'. * This method will be tested when StringValidator.result() is called. * @returns {StringValidator} StringValidator */ booleanStr() { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.booleanStr()); return this; } /** * Check if string is one of the array elements. * Returns true if the string is in the array. * This method will be tested when StringValidator.result() is called. * @param {string[]} arr string set * @returns {StringValidator} StringValidator */ oneof(arr) { __classPrivateFieldGet(this, _StringValidator_instances, "m", _StringValidator_addValidation).call(this, Utils_1.default.oneof(arr)); return this; } test(p) { if ((0, Utils_1.isEmptyValue)(p)) { return __classPrivateFieldGet(this, _StringValidator_optional, "f"); } for (const fn of __classPrivateFieldGet(this, _StringValidator_validationTasks, "f")) { if (!fn(p)) return false; } return true; } } _StringValidator_validationTasks = new WeakMap(), _StringValidator_optional = new WeakMap(), _StringValidator_instances = new WeakSet(), _StringValidator_addValidation = function _StringValidator_addValidation(fn) { __classPrivateFieldGet(this, _StringValidator_validationTasks, "f").push(fn); }; exports.default = StringValidator;