Skip to content

Commit

Permalink
[v16] move requiredAll rule to shared validation package (#42766)
Browse files Browse the repository at this point in the history
* requiredAll rule that wraps multiple input validation rule

* prettier-write
  • Loading branch information
flyinghermit authored Jun 11, 2024
1 parent d46e218 commit 81b4ef2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions web/packages/shared/components/Validation/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,34 @@ const requiredEmailLike: Rule<string, EmailValidationResult> = 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 =
<T>(...rules: Rule<T | string | string[], ValidationResult>[]): Rule<T> =>
(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,
Expand All @@ -202,4 +230,5 @@ export {
requiredRoleArn,
requiredIamRoleName,
requiredEmailLike,
requiredAll,
};

0 comments on commit 81b4ef2

Please sign in to comment.