From b893fc0ff4415cf3a4abccaf2c048fb47fe2b11c Mon Sep 17 00:00:00 2001 From: Ayobami Akingbade Date: Tue, 6 Feb 2024 18:54:28 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(validations):=20add=20slug=20r?= =?UTF-8?q?egex=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shared/validations/validations-map.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/shared/validations/validations-map.ts b/src/shared/validations/validations-map.ts index 3e4a13199..45beb5682 100644 --- a/src/shared/validations/validations-map.ts +++ b/src/shared/validations/validations-map.ts @@ -185,3 +185,16 @@ export const ENTITY_VALIDATION_CONFIG: Record< implementation: handleValidation(matches, "pattern"), }, }; + +const LOWERCASE_NUMBERS_HYPEN_AND_UNDERSCORE_REGEX = (delimeter: "-" | "_") => + `^[a-z0-9${delimeter}]+$`; + +export const SLUG_VALIDATION = (delimiter: "hyphens" | "underscores") => ({ + validationType: "regex" as const, + constraint: { + pattern: LOWERCASE_NUMBERS_HYPEN_AND_UNDERSCORE_REGEX( + delimiter === "hyphens" ? "-" : "_" + ), + }, + errorMessage: `Only lowercase letters, numbers and ${delimiter} are allowed`, +});