UNPKG

@bshg/validation

Version:

Validation Library for TypeScript projects

129 lines (128 loc) 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Strings = void 0; const base_1 = require("./base"); const utils_1 = require("../utils"); const messages_1 = require("../messages"); const msgs = () => messages_1.CurrentLocalize.string; class Strings extends base_1.TypeValidator { undefined() { return new Strings(); } required(options) { return this.useCostume({ error: value => value === undefined, message: msgs().required, options, }); } notEmpty(options) { return this.useCostume({ error: value => value !== undefined && value === "", message: msgs().notEmpty, options: options, }); } min(length, options) { return this.useCostume({ error: value => value !== undefined && value.length < length, message: msgs().min, options: options, args: [length], }); } max(length, options) { return this.useCostume({ error: value => value !== undefined && value.length > length, message: msgs().max, options: options, args: [length], }); } includes(substring, options) { return this.useCostume({ error: value => value !== undefined && !value.includes(substring), message: msgs().includes, options: options, args: [substring], }); } includesAll(substrings, options) { return this.useCostume({ error: value => value !== undefined && !substrings.every(substring => value.includes(substring)), message: msgs().includesAll, options: options, args: [substrings.join(", ")], }); } startsWith(prefix, options) { return this.useCostume({ error: value => value !== undefined && !value.startsWith(prefix), message: msgs().startsWith, options: options, args: [prefix], }); } endsWith(suffix, options) { return this.useCostume({ error: value => value !== undefined && !value.endsWith(suffix), message: msgs().endsWith, options: options, args: [suffix], }); } matches(pattern, options) { return this.useCostume({ error: value => value !== undefined && !pattern.test(value), message: msgs().matches, options: options, }); } email(options) { return this.matches(utils_1.regex.EMAIL, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().email }); } phone(options) { return this.matches(utils_1.regex.PHONE, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().phone }); } url(options) { return this.matches(utils_1.regex.URL, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().url }); } date(options) { return this.matches(utils_1.regex.DATE, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().date }); } time(options) { return this.matches(utils_1.regex.TIME, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().time }); } hexColor(options) { return this.matches(utils_1.regex.HEX_COLOR, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().hexColor }); } creditCard(options) { return this.matches(utils_1.regex.CREDIT_CARD, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().creditCard }); } htmlTag(options) { return this.matches(utils_1.regex.HTML_TAG, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().htmlTag }); } base64(options) { return this.matches(utils_1.regex.BASE64, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().base64 }); } alphanumeric(options) { return this.matches(utils_1.regex.ALPHANUMERIC, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().alphanumeric }); } numeric(options) { return this.matches(utils_1.regex.NUMERIC, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().numeric }); } alpha(options) { return this.matches(utils_1.regex.ALPHA, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().alpha }); } /////TODO add in() to set the allowed values //////////////////////////////////////////// as(key, options) { return this.useCostume({ error: (value, parent) => value != parent[key], message: msgs().as, options: options, args: [key], }); } } exports.Strings = Strings;