Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v16] move requiredAll rule to shared validation package #42766

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
Loading