UNPKG

@bshg/validation

Version:

Validation Library for TypeScript projects

80 lines (79 loc) 2.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeValidator = exports.message = exports.messageArgs = exports.messageVars = void 0; exports.messageVars = { prefix: "%", value: "%val", name: "%name", }; const messageArgs = (defaultMsg, options, ...args) => { const msg = (0, exports.message)(defaultMsg, options); return (msg.includes("%") || (args === null || args === void 0 ? void 0 : args.length) > 0) ? msg.replace(/%(\d+)/g, (_, i) => args[i - 1]) : msg; }; exports.messageArgs = messageArgs; const message = (message, options) => { if (options && options.message) { return (typeof options.message == "string") ? options.message : options.message(); } return message; }; exports.message = message; class TypeValidator { constructor() { this.validations = []; this.validationsDepend = []; this.validationsAsync = []; this.validationsDependAsync = []; this.validationsCtx = []; this.validationsDependCtx = []; this.validationsCtxAsync = []; this.validationsDependCtxAsync = []; } onError(config) { if ("error" in config) { if (config.error.length > 1) { this.validationsDepend.push(config); } else { this.validations.push(config); } } return this; } onErrorCtx(config) { if ("error" in config) { if (config.error.length > 2) { this.validationsDependCtx.push(config); } else { this.validationsCtx.push(config); } } return this; } useCostume(arg) { return this.onError({ error: arg.error, message: (0, exports.messageArgs)(arg.message, arg.options, arg.args), }); } onErrorAsync(config) { if ("error" in config && config.error.length > 1) { this.validationsDependAsync.push(config); } else if ("error" in config) { this.validationsAsync.push(config); } return this; } onErrorCtxAsync(config) { if ("error" in config && config.error.length > 2) { this.validationsDependCtxAsync.push(config); } else if ("error" in config) { this.validationsCtxAsync.push(config); } return this; } } exports.TypeValidator = TypeValidator;