UNPKG

aptx-validator

Version:

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

85 lines (84 loc) 3.25 kB
import AllValidator from "./AllValidator"; import { ValidateFunction } from "./types"; /** * Validator for String parameters, which includes multiple String validation methods. */ declare class StringValidator<O extends boolean = false, N extends boolean = false> extends AllValidator<string, O, N> { #private; output: string; constructor(); custom(fn: ValidateFunction): this; optional(): StringValidator<true>; /** * 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: number): 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: number): 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(): 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: number): 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: RegExp): 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?: RegExp): 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?: RegExp): 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(): 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: string[]): this; test(p?: any): boolean; } export default StringValidator;