UNPKG

@re-flex/form-validator

Version:

Re-flex UTILS _> form-validator

161 lines (160 loc) 6.04 kB
const default_message = "Not Valid"; const invalidDate = new Date(""); const isArray = Array.isArray; const getUnixDate = (e) => new Date(e).getTime(); class CorePrototypes { constructor() { this.options = { nullable: true, required: false, }; this.matcher = []; this.nullable = (value = true) => { this.options.nullable = !!value; return { validate: this.validate }; }; this.required = (value = true) => { this.options.required = !!value; return { validate: this.validate }; }; this.defaultChain = () => { return { required: this.required, nullable: this.nullable, validate: this.validate, }; }; } validate(value) { if (isArray(this.matcher)) { for (const { message, checker } of this.matcher) { let isValid = checker(value); console.log("isValid", isValid, message); console.log("this.options.required", this.options); if (this.options.required && value === undefined && value !== null) { return message || default_message; } if (this.options.nullable === false && value === null) { return message || default_message; } if (!isValid) { return message || default_message; } continue; } } return null; } } const EMAIL_REGXP = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; const PASSWORD_REGXP = /^[A-Za-z]\w{7,14}$/; const PHONE_NO_REGEXP = /^\d{10}$/; const CREDIT_CARD_REGEX = /^(?:3[47][0-9]{13})$/; const URL_REGEX = /[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gim; class StringBase extends CorePrototypes { constructor(customRegexp) { super(); this.email = this.BindRegexpHook(EMAIL_REGXP); this.password = this.BindRegexpHook(PASSWORD_REGXP); this.phone = this.BindRegexpHook(PHONE_NO_REGEXP); this.creditCard = this.BindRegexpHook(CREDIT_CARD_REGEX); this.url = this.BindRegexpHook(URL_REGEX); this.match = this.BindRegexpHook(/.*/gim); this.customRegexp = customRegexp; } BindRegexpHook(regexp) { return (message = default_message) => { this.matcher.push({ message, type: "string", checker: (msg) => (this.customRegexp || regexp).test(msg), }); return this.defaultChain(); }; } } class NumberBase extends CorePrototypes { constructor(maxMin) { super(); this.BindNumberHook = (checkFunc) => { return (message = default_message) => { this.matcher.push({ message, type: "number", checker: (val) => { return !Number.isNaN(val) && checkFunc(val, this.maxMin); }, }); return this.defaultChain(); }; }; this.positive = this.BindNumberHook((e) => e > 0); this.negative = this.BindNumberHook((e) => e < 0); this.max = this.BindNumberHook((e, max = 0) => e <= max); this.min = this.BindNumberHook((e, min = 0) => e >= min); this.float = this.BindNumberHook((e) => !String(e).includes(".")); this.integer = this.BindNumberHook((e) => !Number.isInteger(e)); this.safeInteger = this.BindNumberHook((e) => !Number.isSafeInteger(e)); this.bigInteger = this.BindNumberHook((e) => !BigInt(e) === e); this.maxMin = maxMin; } } class DateBase extends CorePrototypes { constructor(maxMin) { super(); this.BindDateHook = (checkFunc) => { return (message = default_message) => { this.matcher.push({ message, type: "date", checker: (val) => new Date(val) !== invalidDate && checkFunc(val, this.maxMin), }); return this.defaultChain(); }; }; this.max = this.BindDateHook((e, maxMin = new Date()) => getUnixDate(e) <= getUnixDate(maxMin)); this.min = this.BindDateHook((e, maxMin = new Date()) => getUnixDate(e) >= getUnixDate(maxMin)); this.maxMin = maxMin; } } class BooleanBase extends CorePrototypes { constructor(message = default_message) { super(); this.matcher.push({ message, type: "boolean", checker: (val) => ["true", "false"].includes(String(val)), }); } } class ObjectBase extends CorePrototypes { constructor(rules, each, message = default_message) { super(); this.message = default_message; this.init = () => { this.matcher.push({ message: this.message, type: "object", checker: (validatingObj) => { let result = {}; for (const key in this.rules) { if (Object.prototype.hasOwnProperty.call(validatingObj, key)) { const hasError = this.rules[key].validate(validatingObj[key]); result[key] = hasError; } } return result; }, }); return this; }; this.rules = rules; this.each = each; this.message = message; } } export const string = () => new StringBase(); export const number = (maxMin) => new NumberBase(maxMin); export const date = () => new DateBase(); export const boolean = (msg) => new BooleanBase(msg); export const object = (rules, message = default_message, each) => new ObjectBase(rules, each, message).init();