diff --git a/web/packages/shared/components/Validation/rules.ts b/web/packages/shared/components/Validation/rules.ts index 84a7802000754..456782f10955b 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, };