@allgemein/schema-api
Version:
Library for schema api
47 lines • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Regex = Regex;
const lodash_1 = require("lodash");
const AnnotationsHelper_1 = require("../../lib/AnnotationsHelper");
const DefaultValidator_1 = require("../../lib/validation/DefaultValidator");
function Regex(regex, options) {
return function (source, propertyName) {
const opts = {
pattern: (0, lodash_1.isString)(regex) ? regex : regex.source,
};
if (!(0, lodash_1.isString)(regex) && !(0, lodash_1.isEmpty)(regex.flags)) {
opts.validateOptions = {
regex: {
flags: regex.flags,
}
};
}
if (options) {
if (!opts.validateOptions) {
opts.validateOptions = { regex: {} };
}
(0, lodash_1.assign)(opts.validateOptions.regex, options);
}
AnnotationsHelper_1.AnnotationsHelper.forPropertyOn(source.constructor, propertyName, opts, 'merge');
};
}
DefaultValidator_1.DefaultValidator.define({
name: 'regex',
fn: (value, options, instance) => {
// if (isUndefined(value) || isNull(value)) {
// return true;
// }
if ((0, lodash_1.isString)(value)) {
const r = new RegExp(options.pattern, options.flags);
if (r.test(value)) {
return true;
}
}
return false;
},
defaultOptions: {
message: 'Value of property "%propertyName" doesn\'t match the regular expression "%options.pattern".'
},
involveOnOptionKey: 'pattern'
});
//# sourceMappingURL=Regex.js.map