Skip to content

feat: support custom validation errors for number-typed parameters #12224

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

Merged
merged 9 commits into from
Feb 20, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fmt
  • Loading branch information
mtojek committed Feb 20, 2024
commit c02c20fbf744131a97d65356c732aec0e6a18447
59 changes: 42 additions & 17 deletions site/src/utils/richParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export const useValidationSchemaForRichParameters = (
if (Number(val) < templateParameter.validation_min) {
return ctx.createError({
path: ctx.path,
message: takeFirstError(errorRendered(templateParameter, val), `Value must be greater than ${templateParameter.validation_min}.`),
message: takeFirstError(
errorRendered(templateParameter, val),
`Value must be greater than ${templateParameter.validation_min}.`,
),
});
}
} else if (
Expand All @@ -89,7 +92,10 @@ export const useValidationSchemaForRichParameters = (
if (templateParameter.validation_max < Number(val)) {
return ctx.createError({
path: ctx.path,
message: takeFirstError(errorRendered(templateParameter, val), `Value must be less than ${templateParameter.validation_max}.`),
message: takeFirstError(
errorRendered(templateParameter, val),
`Value must be less than ${templateParameter.validation_max}.`,
),
});
}
} else if (
Expand All @@ -102,7 +108,10 @@ export const useValidationSchemaForRichParameters = (
) {
return ctx.createError({
path: ctx.path,
message: takeFirstError(errorRendered(templateParameter, val), `Value must be between ${templateParameter.validation_min} and ${templateParameter.validation_max}.`),
message: takeFirstError(
errorRendered(templateParameter, val),
`Value must be between ${templateParameter.validation_min} and ${templateParameter.validation_max}.`,
),
});
}
}
Expand Down Expand Up @@ -165,22 +174,38 @@ export const useValidationSchemaForRichParameters = (

const takeFirstError = (...errorMessages: string[]): string => {
for (const errorMessage of errorMessages) {
if (errorMessage) {
return errorMessage;
}
if (errorMessage) {
return errorMessage;
}
}
return "developer error: error message is not provided";
};

const errorRendered = (parameter: TemplateVersionParameter, value?: string): string => {
if (!parameter.validation_error || !value) {
return "";
}
const errorRendered = (
parameter: TemplateVersionParameter,
value?: string,
): string => {
if (!parameter.validation_error || !value) {
return "";
}

const r = new Map<string, string>([
["{min}", parameter.validation_min !== undefined ? parameter.validation_min.toString() : ""],
["{max}", parameter.validation_max !== undefined ? parameter.validation_max.toString() : ""],
["{value}", value]
]);
return parameter.validation_error.replace(/{min}|{max}|{value}/g, match => r.get(match) || "");
}
const r = new Map<string, string>([
[
"{min}",
parameter.validation_min !== undefined
? parameter.validation_min.toString()
: "",
],
[
"{max}",
parameter.validation_max !== undefined
? parameter.validation_max.toString()
: "",
],
["{value}", value],
]);
return parameter.validation_error.replace(
/{min}|{max}|{value}/g,
(match) => r.get(match) || "",
);
};