From 23c2bfc4ea0a650485bd71c21151f45eb4c96b9e Mon Sep 17 00:00:00 2001 From: flyinghermit Date: Mon, 10 Jun 2024 17:40:36 -0400 Subject: [PATCH 1/2] requiredAll rule that wraps multiple input validation rule --- .../shared/components/Validation/rules.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/web/packages/shared/components/Validation/rules.ts b/web/packages/shared/components/Validation/rules.ts index 84a7802000754..7ba6b3f892146 100644 --- a/web/packages/shared/components/Validation/rules.ts +++ b/web/packages/shared/components/Validation/rules.ts @@ -194,6 +194,34 @@ const requiredEmailLike: Rule = email => () => { }; }; +/** + * A rule function that combines multiple inner rule functions. All rules must + * return `valid`, otherwise it returns a comma separated string containing all + * invalid rule messages. + * @param rules a list of rule functions to apply + * @returns a rule function that ANDs all input rules + */ +const requiredAll = + (...rules: Rule[]): Rule => + (value: T) => + () => { + let messages = []; + for (let r of rules) { + let result = r(value)(); + if (!result.valid) { + messages.push(result.message); + } + } + + if (messages.length > 0) { + return { + valid: false, + message: messages.join('. '), + }; + } + return { valid: true }; + }; + export { requiredToken, requiredPassword, @@ -202,4 +230,5 @@ export { requiredRoleArn, requiredIamRoleName, requiredEmailLike, + requiredAll }; From 81e860c612be6f0309982aa47a1eea7d15408bd6 Mon Sep 17 00:00:00 2001 From: flyinghermit Date: Tue, 11 Jun 2024 10:18:19 -0400 Subject: [PATCH 2/2] prettier-write --- web/packages/shared/components/Validation/rules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/packages/shared/components/Validation/rules.ts b/web/packages/shared/components/Validation/rules.ts index 7ba6b3f892146..456782f10955b 100644 --- a/web/packages/shared/components/Validation/rules.ts +++ b/web/packages/shared/components/Validation/rules.ts @@ -230,5 +230,5 @@ export { requiredRoleArn, requiredIamRoleName, requiredEmailLike, - requiredAll + requiredAll, };