UNPKG

logitar-validation

Version:

JavaScript validation library distributed by Logitar.

24 lines (23 loc) 913 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * A validation rule that checks if a string matches a regular expression. * @param value The value to validate. * @param args The regular expression to validate the string against. * @returns The result of the validation rule execution. */ const pattern = (value, args) => { if (typeof args !== "string" && !(args instanceof RegExp)) { return { severity: "warning", message: "The arguments should be a regular expression." }; } if (typeof value !== "string") { return { severity: "error", message: "{{name}} must be a string." }; } else if (value.length > 0) { if (!new RegExp(args).test(value)) { return { severity: "error", message: "{{name}} must match the pattern {{pattern}}." }; } } return { severity: "information" }; }; exports.default = pattern;