Added regex

This commit is contained in:
Sebastian Seedorf
2020-05-27 21:44:00 +02:00
parent 24810302b2
commit 69a350c4da
2 changed files with 48 additions and 1 deletions

View File

@@ -140,3 +140,19 @@ export function isEmail(): Validator {
},
};
}
export function fulfillsRegex({regex}: {regex: RegExp}): Validator {
return {
type: "fulfillsRegex",
extends: [isString()],
check: (value: any) => {
if (value === null || value === undefined) return;
if (!value.match(regex)) {
return {};
}
},
message: (value: any, args?: Args) => {
return `This value has to fulfill the regex ${regex}.`;
},
};
}