diff --git a/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.test.ts b/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.test.ts index 61afc9e1d0..cab72cbb8d 100644 --- a/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.test.ts +++ b/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.test.ts @@ -1,5 +1,6 @@ import { test, describe, expect } from 'vitest'; import { + baseDomainValidationSchema, dnsNameValidationSchema, hostPrefixValidationSchema, ipBlockValidationSchema, @@ -317,4 +318,51 @@ describe('validationSchemas', () => { expect(counter).toBe(invalid.length); }); + + test('baseDomainNameValidationSchema', async () => { + const valid = [ + 'a.com', + 'co', + '1c', + '1-c', + '1--c', + 'aaa', + 'abc.def', + 'a-aa.com', + 'a--aa.com', + 'aa.com.com.com.com', + 'red.cat--rahul.com', + ]; + const invalid = [ + 'a', + '-', + 'a-', + '-aaa.com.', + 'aaa-.com', + 'a.c', + 'aaa.c', + 'DNSnamescancontainonlyalphabeticalcharactersa-znumericcharacters0-9theminussign-andtheperiod', + 'DNSnamescancontainonlyalphabeticalcharactersa-znumericcharacters0-9theminussign-andtheperiod.com', + ]; + + await Promise.all( + valid.map((value) => + baseDomainValidationSchema + .validate(value) + .catch(() => expect(value).toBe(`was rejected but is valid`)), + ), + ); + + let counter = 0; + await Promise.all( + invalid.map((value) => + baseDomainValidationSchema.validate(value).then( + () => expect(value).toBe('should be rejected since it is invalid'), + () => counter++, + ), + ), + ); + + expect(counter).toBe(invalid.length); + }); }); diff --git a/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.ts b/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.ts index 8825410c68..c22191269d 100644 --- a/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.ts +++ b/libs/ui-lib/lib/common/components/ui/formik/validationSchemas.ts @@ -38,8 +38,8 @@ const CLUSTER_NAME_VALID_CHARS_REGEX = /^[a-z0-9-]*$/; const SSH_PUBLIC_KEY_REGEX = /^(ssh-rsa|ssh-ed25519|ecdsa-[-a-z0-9]*) AAAA[0-9A-Za-z+/]+[=]{0,3}( .+)?$/; const DNS_NAME_REGEX = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/; -const BASE_DOMAIN_REGEX = /^([a-z0-9]+(-[a-z0-9]+)*)$/; -const DNS_NAME_REGEX_OCM = /^([a-z0-9]+(-[a-z0-9]+)*[.])+[a-z]{2,}$/; +const BASE_DOMAIN_REGEX = /^[a-z\d][\-]*[a-z\d]+$/; +const DNS_NAME_REGEX_OCM = /^([a-z\d]([\-]*[a-z\d]+)*\.)+[a-z\d]+[\-]*[a-z\d]+$/; const PROXY_DNS_REGEX = /(^\.?([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*$)/;