8000 feat: allow masking workspace parameter inputs by aslilac · Pull Request #18595 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: allow masking workspace parameter inputs #18595

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 18 commits into from
Jul 1, 2025
Merged
Prev Previous commit
Next Next commit
STOP IT
  • Loading branch information
aslilac committed Jun 26, 2025
commit 1f3ed2818e5122d83759c314bcda90906b70fd56
205 changes: 103 additions & 102 deletions site/src/modules/workspaces/DynamicParameter/DynamicParameter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import {
TriangleAlert,
} from "lucide-react";
import { type FC, useEffect, useId, useRef, useState } from "react";
import { maskText } from "utils/text";
import type { AutofillBuildParameter } from "utils/richParameters";
import { maskText } from "utils/text";
import * as Yup from "yup";

interface DynamicParameterProps {
Expand Down Expand Up @@ -782,119 +782,120 @@ export const useValidationSchemaForDynamicParameters = (
.of(
Yup.object().shape({
name: Yup.string().required(),
value: Yup.string().test("verify with template", (val, ctx) => {
const name = ctx.parent.name;
const parameter = parameters.find(
(parameter) => parameter.name === name,
);
if (parameter) {
switch (parameter.type) {
case "number": {
const minValidation = parameter.validations.find(
(v) => v.validation_min !== null,
);
const maxValidation = parameter.validations.find(
(v) => v.validation_max !== null,
);

if (
minValidation &&
minValidation.validation_min !== null &&
!maxValidation &&
Number(val) < minValidation.validation_min
) {
return ctx.createError({
path: ctx.path,
message:
parameterError(parameter, val) ??
`Value must be greater than ${minValidation.validation_min}.`,
});
}
value: Yup.string()
.test("verify with template", (val, ctx) => {
const name = ctx.parent.name;
const parameter = parameters.find(
(parameter) => parameter.name === name,
);
if (parameter) {
switch (parameter.type) {
case "number": {
const minValidation = parameter.validations.find(
(v) => v.validation_min !== null,
);
const maxValidation = parameter.validations.find(
(v) => v.validation_max !== null,
);

if (
!minValidation &&
maxValidation &&
maxValidation.validation_max !== null &&
Number(val) > maxValidation.validation_max
) {
return ctx.createError({
path: ctx.path,
message:
parameterError(parameter, val) ??
`Value must be less than ${maxValidation.validation_max}.`,
});
}
if (
minValidation &&
minValidation.validation_min !== null &&
!maxValidation &&
Number(val) < minValidation.validation_min
) {
return ctx.createError({
path: ctx.path,
message:
parameterError(parameter, val) ??
`Value must be greater than ${minValidation.validation_min}.`,
});
}

if (
minValidation &&
minValidation.validation_min !== null &&
maxValidation &&
maxValidation.validation_max !== null &&
(Number(val) < minValidation.validation_min ||
Number(val) > maxValidation.validation_max)
) {
return ctx.createError({
path: ctx.path,
message:
parameterError(parameter, val) ??
`Value must be between ${minValidation.validation_min} and ${maxValidation.validation_max}.`,
});
}
if (
!minValidation &&
maxValidation &&
maxValidation.validation_max !== null &&
Number(val) > maxValidation.validation_max
) {
return ctx.createError({
path: ctx.path,
message:
parameterError(parameter, val) ??
`Value must be less than ${maxValidation.validation_max}.`,
});
}

const monotonic = parameter.validations.find(
(v) =>
v.validation_monotonic !== null &&
v.validation_monotonic !== "",
);
if (
minValidation &&
minValidation.validation_min !== null &&
maxValidation &&
maxValidation.validation_max !== null &&
(Number(val) < minValidation.validation_min ||
Number(val) > maxValidation.validation_max)
) {
return ctx.createError({
path: ctx.path,
message:
parameterError(parameter, val) ??
`Value must be between ${minValidation.validation_min} and ${maxValidation.validation_max}.`,
});
}

if (monotonic && lastBuildParameters) {
const lastBuildParameter = lastBuildParameters.find(
(last: { name: string }) => last.name === name,
const monotonic = parameter.validations.find(
(v) =>
v.validation_monotonic !== null &&
v.validation_monotonic !== "",
);
if (lastBuildParameter) {
switch (monotonic.validation_monotonic) {
case "increasing":
if (Number(lastBuildParameter.value) > Number(val)) {
return ctx.createError({
path: ctx.path,
message: `Value must only ever increase (last value was ${lastBuildParameter.value})`,
});
}
break;
case "decreasing":
if (Number(lastBuildParameter.value) < Number(val)) {
return ctx.createError({
path: ctx.path,
message: `Value must only ever decrease (last value was ${lastBuildParameter.value})`,
});
}
break;

if (monotonic && lastBuildParameters) {
const lastBuildParameter = lastBuildParameters.find(
(last: { name: string }) => last.name === name,
);
if (lastBuildParameter) {
switch (monotonic.validation_monotonic) {
case "increasing":
if (Number(lastBuildParameter.value) > Number(val)) {
return ctx.createError({
path: ctx.path,
message: `Value must only ever increase (last value was ${lastBuildParameter.value})`,
});
}
break;
case "decreasing":
if (Number(lastBuildParameter.value) < Number(val)) {
return ctx.createError({
path: ctx.path,
message: `Value must only ever decrease (last value was ${lastBuildParameter.value})`,
});
}
break;
}
}
}
break;
}
break;
}
case "string": {
const regex = parameter.validations.find(
(v) =>
v.validation_regex !== null && v.validation_regex !== "",
);
if (!regex || !regex.validation_regex) {
return true;
}
case "string": {
const regex = parameter.validations.find(
(v) =>
v.validation_regex !== null && v.validation_regex !== "",
);
if (!regex || !regex.validation_regex) {
return true;
}

if (val && !new RegExp(regex.validation_regex).test(val)) {
return ctx.createError({
path: ctx.path,
message: parameterError(parameter, val),
});
if (val && !new RegExp(regex.validation_regex).test(val)) {
return ctx.createError({
path: ctx.path,
message: parameterError(parameter, val),
});
}
break;
}
break;
}
}
}
return true;
}),
return true;
}),
}),
)
.required();
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/TaskPage/TaskPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { Helmet } from "react-helmet-async";
import { useQuery } from "react-query";
import { useParams } from "react-router-dom";
import { Link as RouterLink } from "react-router-dom";
import { ellipsizeText } from "utils/text";
import { pageTitle } from "utils/page";
import { ellipsizeText } from "utils/text";
import {
ActiveTransition,
WorkspaceBuildProgress,
Expand Down
2 changes: 1 addition & 1 deletion site/src/utils/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ellipsizeText, maskText } from "./text";
import type { Nullable } from "./nullable";
import { ellipsizeText, maskText } from "./text";

describe("ellipsizeText", () => {
it.each([
Expand Down
Loading
0