UNPKG

@blinkk/selective-edit

Version:
81 lines 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MatchRule = void 0; const validationRules_1 = require("../validationRules"); const dataType_1 = require("../../utility/dataType"); class MatchRule extends validationRules_1.Rule { constructor(config) { super(config); this.defaultMessage = 'Value needs to match the validation rule.'; this.config = config; this.patternCache = {}; } cachePattern(key, pattern) { if (!(key in this.patternCache)) { this.patternCache[key] = new RegExp(pattern); } return this.patternCache[key]; } validate(value) { // Allow for empty fields. // Use the required rule for making sure it exists. if (!value) { return null; } // Handle the allowed matching. let matchConfig = this.config.allowed; if (matchConfig) { // Matching values are allowed. if (matchConfig.pattern) { if (!this.cachePattern('allowed', matchConfig.pattern).test(value)) { return matchConfig.message || this.message; } } // Matching specific values only. if (matchConfig.values && matchConfig.values.length) { let inValues = false; for (const possibleValue of matchConfig.values) { if (dataType_1.DataType.isRegExp(possibleValue)) { if (possibleValue.test(value)) { inValues = true; break; } } else if (possibleValue == value) { inValues = true; break; } } if (!inValues) { return matchConfig.message || this.message; } } } // Handle the excluded matching. matchConfig = this.config.excluded; if (matchConfig) { // Matching values are NOT allowed. if (matchConfig.pattern) { if (this.cachePattern('excluded', matchConfig.pattern).test(value)) { return matchConfig.message || this.message; } } // Matching specific values NOT allowed. if (matchConfig.values) { for (const possibleValue of matchConfig.values) { if (dataType_1.DataType.isRegExp(possibleValue)) { if (possibleValue.test(value)) { return matchConfig.message || this.message; } } else if (possibleValue == value) { return matchConfig.message || this.message; } } } } return null; } } exports.MatchRule = MatchRule; //# sourceMappingURL=match.js.map