Skip to content

feat!: Validate monotonic numbers for rich parameters #6046

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 10 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
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
UI changes
  • Loading branch information
mtojek committed Feb 6, 2023
commit 2a65b720b407a166175c39fe2c561cd2b6737cac
4 changes: 2 additions & 2 deletions codersdk/richparameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func ValidateWorkspaceBuildParameter(richParameter TemplateVersionParameter, bui
switch richParameter.ValidationMonotonic {
case MonotonicOrderIncreasing:
if lastBuildParameter.Value > buildParameter.Value {
return xerrors.Errorf("parameter value must be equal or lower than previous value: %s", lastBuildParameter.Value)
return xerrors.Errorf("parameter value must be equal or greater than previous value: %s", lastBuildParameter.Value)
}
case MonotonicOrderDecreasing:
if lastBuildParameter.Value < buildParameter.Value {
return xerrors.Errorf("parameter value must be equal or greater than previous value: %s", lastBuildParameter.Value)
return xerrors.Errorf("parameter value must be equal or lower than previous value: %s", lastBuildParameter.Value)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion site/src/i18n/en/workspaceBuildParametersPage.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"validationRequiredParameter": "Parameter is required.",
"validationNumberNotInRange": "Value must be between {{min}} and {{max}}.",
"validationPatternNotMatched": "{{error}} (value does not match the pattern {{pattern}}).",
"updateWorkspace": "Update workspace"
"updateWorkspace": "Update workspace",
"validationNumberNotIncreasing": "The value must be equal or greater than the previous one {{last}}.",
"validationNumberNotDecreasing": "The value must be equal or lower than the previous one {{last}}."
}
60 changes: 48 additions & 12 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ export const workspaceBuildParameterValue = (
export const ValidationSchemaForRichParameters = (
ns: string,
templateParameters?: TypesGen.TemplateVersionParameter[],
lastBuildParameters?: TypesGen.WorkspaceBuildParameter[],
): Yup.AnySchema => {
const { t } = useTranslation(ns)

Expand All @@ -515,27 +516,62 @@ export const ValidationSchemaForRichParameters = (
switch (templateParameter.type) {
case "number":
if (
!templateParameter.validation_min || !templateParameter.validation_max
templateParameter.validation_min &&
templateParameter.validation_max
) {
return true
if (
Number(val) < templateParameter.validation_min ||
templateParameter.validation_max < Number(val)
) {
return ctx.createError({
path: ctx.path,
message: t("validationNumberNotInRange", {
min: templateParameter.validation_min,
max: templateParameter.validation_max,
}),
})
}
}

if (
Number(val) < templateParameter.validation_min ||
templateParameter.validation_max < Number(val)
templateParameter.validation_monotonic &&
lastBuildParameters
) {
return ctx.createError({
path: ctx.path,
message: t("validationNumberNotInRange", {
min: templateParameter.validation_min,
max: templateParameter.validation_max,
}),
})
const lastBuildParameter = lastBuildParameters.find(
(last) => last.name === name,
)
if (lastBuildParameter) {
switch (templateParameter.validation_monotonic) {
case "increasing":
if (Number(lastBuildParameter.value) > Number(val)) {
return ctx.createError({
path: ctx.path,
message: t("validationNumberNotIncreasing", {
last: lastBuildParameter.value,
}),
})
}
break
case "decreasing":
if (Number(lastBuildParameter.value) < Number(val)) {
return ctx.createError({
path: ctx.path,
message: t("validationNumberNotDecreasing", {
last: lastBuildParameter.value,
}),
})
}
break
}
}
}
break
case "string":
{
if (!templateParameter.validation_regex || templateParameter.validation_regex.length === 0) {
if (
!templateParameter.validation_regex ||
templateParameter.validation_regex.length === 0
) {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const WorkspaceBuildParametersPageView: FC<
rich_parameter_values: ValidationSchemaForRichParameters(
"workspaceBuildParametersPage",
props.templateParameters,
initialRichParameterValues,
),
}),
enableReinitialize: true,
Expand Down