Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit f6ff3f2

Browse files
authored
feat(console): mark immutable resource fields as read-only in edit forms (#6)
* feat(console): add immutable schema-path helpers Introduce pure helpers used to recognise and enforce Kubernetes-style immutability rules on resource schemas. findImmutablePaths(schema) walks an OpenAPI/JSON-schema tree and returns every path whose subschema carries an x-kubernetes-validations entry with rule "self == oldSelf". Object properties contribute named segments; array items and additionalProperties contribute the wildcard segment "*". oneOf, anyOf and allOf branches are considered when determining whether a node is immutable. overlayImmutable(submitted, original, paths) deep-clones the submitted value, then for each path copies the value from original. Wildcard semantics dispatch on the runtime value shape: - *-last on an array: replace with a clone of source (whole-array immutable). - *-last on an object map: replace with a clone of source (whole-map immutable). - *-not-last on an array: iterate the user's array, restore the immutable nested subfield from source at shared indices, keep user-added entries verbatim. Length shrinks are detected and the overlay is skipped with a console.warn so admission stays the authoritative enforcer (index-aligned overlay would otherwise silently swap which element the user deleted). - *-not-last on an object map: freeze the whole map. The UI marks such fields ui:disabled (Add/Remove hidden); the overlay matches so YAML-editor and devtools bypasses can't add or rename keys. A root-level immutable path or a top-level wildcard immutable path (both pathological schema shapes) replaces submitted wholesale with a clone of original and logs a warning. IMMUTABLE_HELP_TEXT centralises the user-facing string consumed by later UI wiring. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la> * feat(console): strip x-kubernetes-validations during schema sanitisation AJV has no CEL evaluator, so the rule does nothing for JSON-Schema validation; the UI consults the raw schema once to harvest immutability paths and then drops the extension so RJSF traversal stays small. Also add characterization tests for keysOrderToUiSchema and sanitizeSchema so existing behaviour (int-or-string coercion, preserve-unknown-fields → additionalProperties, "Chart Values" → "Parameters") is locked in alongside the new strip. Widen keysOrderToUiSchema's parameter to ReadonlyArray<ReadonlyArray<string>> so call sites can pass immutable-path-style arrays without casts. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la> * feat(console): plumb immutable mode through SchemaForm Add an immutableMode prop ("enforce" | "off", default "off"). When "enforce", every immutable schema path (as reported by findImmutablePaths) receives ui:disabled=true plus a ui:help string sourced from IMMUTABLE_HELP_TEXT. RJSF then renders the corresponding inputs greyed out and surfaces the helper text underneath. The default keeps every field editable so create flows are unaffected. Wildcard segments map to RJSF's "items" key for arrays; for object maps (additionalProperties) the field itself is marked disabled and the existing AdditionalPropertiesField renderer propagates that to the nested form, hiding Add/Remove controls. Parse the raw schema once and derive both the sanitised RJSFSchema and the immutable-path set from the shared parsed object so the JSON string isn't decoded twice per render. Add component-level tests covering: - enforce / off / omitted prop branches on scalar fields, - per-element-nested-immutable fields inside array items, - whole-array immutable shape, - the deliberate UI/overlay asymmetry on additionalProperties object maps (the whole map is frozen in both UI and overlay). Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la> * feat(console): enforce immutable fields in edit pages ApplicationOrderPage and BackupResourceEditPage now opt their SchemaForm into immutableMode="enforce" when in edit flow so the relevant fields render greyed out with helper text. On submit, prepareUpdateSpec walks the schema for immutable paths and copies the original values from the persisted spec into the outgoing body. The API server therefore observes no delta on those paths and the CEL immutability check is not triggered, even if the user managed to bypass the read-only UI (YAML editor, devtools, RJSF bug). Both pages anchor the overlay source to a mount-time snapshot stored in a ref so a background React-Query refetch can't sneak a different immutable value into the PUT. The snapshot capture lives inside useEffect to keep render side-effect-free. prepareUpdateSpec is a thin pure wrapper around findImmutablePaths + overlayImmutable. It short-circuits to a deep clone when the persisted spec is null/undefined (avoids blanking immutable fields with undefined) and logs a console.warn when the schema string fails to parse instead of silently disabling enforcement. BackupResourceEditPage.handleSubmit also refuses to save when the CRD schema hasn't loaded yet, mirroring the existing !resource guard. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la> * docs: surface pnpm test in the README The workspace already configures vitest + jsdom + @testing-library in apps/console; the README mentioned only install/dev/build. Add a one-line Test section pointing at the workspace-wide "pnpm test". Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la> --------- Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent 7c20a20 commit f6ff3f2

12 files changed

Lines changed: 1374 additions & 9 deletions

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ pnpm build
3636
```
3737

3838
The console is built into `apps/console/dist/`.
39+
40+
## Test
41+
42+
```sh
43+
pnpm test
44+
```
45+
46+
Runs the workspace test suites (vitest + jsdom for the console).
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, it, expect } from "vitest"
2+
import { render, screen } from "@testing-library/react"
3+
import { SchemaForm } from "./SchemaForm.tsx"
4+
import { IMMUTABLE_HELP_TEXT } from "../lib/immutable-paths.ts"
5+
6+
const schemaWithMap = JSON.stringify({
7+
type: "object",
8+
properties: {
9+
labels: {
10+
type: "object",
11+
additionalProperties: { type: "string" },
12+
"x-kubernetes-validations": [
13+
{ rule: "self == oldSelf", message: "labels are immutable" },
14+
],
15+
},
16+
},
17+
})
18+
19+
const noop = () => {}
20+
21+
describe("AdditionalPropertiesField immutable", () => {
22+
it("renders Add/Remove controls when not immutable", () => {
23+
render(
24+
<SchemaForm
25+
openAPISchema={schemaWithMap}
26+
formData={{ labels: { env: "prod" } }}
27+
onChange={noop}
28+
/>,
29+
)
30+
expect(screen.getByPlaceholderText("Enter key name...")).toBeInTheDocument()
31+
expect(screen.getByRole("button", { name: /add/i })).toBeInTheDocument()
32+
expect(screen.getByRole("button", { name: /remove/i })).toBeInTheDocument()
33+
})
34+
35+
it("treats nested *-not-last on additionalProperties as whole-map disabled in the UI; overlay matches", () => {
36+
// Per-value-immutable schema (additionalProperties carries a nested
37+
// CEL rule). The UI freezes the whole map and the overlay freezes the
38+
// whole map (see overlayImmutable's additionalProperties branch).
39+
// Both sides therefore agree: YAML-editor bypasses that add or rename
40+
// keys are caught by the overlay before the PUT lands.
41+
const nestedImmutableSchema = JSON.stringify({
42+
type: "object",
43+
properties: {
44+
labels: {
45+
type: "object",
46+
additionalProperties: {
47+
type: "object",
48+
properties: {
49+
value: {
50+
type: "string",
51+
"x-kubernetes-validations": [
52+
{ rule: "self == oldSelf", message: "value is immutable" },
53+
],
54+
},
55+
},
56+
},
57+
},
58+
},
59+
})
60+
render(
61+
<SchemaForm
62+
openAPISchema={nestedImmutableSchema}
63+
formData={{ labels: { env: { value: "prod" } } }}
64+
onChange={noop}
65+
immutableMode="enforce"
66+
/>,
67+
)
68+
expect(
69+
screen.queryByPlaceholderText("Enter key name..."),
70+
).not.toBeInTheDocument()
71+
expect(screen.getByText(IMMUTABLE_HELP_TEXT)).toBeInTheDocument()
72+
})
73+
74+
it("hides Add/Remove and disables inner fields when the map is immutable in enforce mode", () => {
75+
render(
76+
<SchemaForm
77+
openAPISchema={schemaWithMap}
78+
formData={{ labels: { env: "prod" } }}
79+
onChange={noop}
80+
immutableMode="enforce"
81+
/>,
82+
)
83+
expect(
84+
screen.queryByPlaceholderText("Enter key name..."),
85+
).not.toBeInTheDocument()
86+
expect(
87+
screen.queryByRole("button", { name: /add/i }),
88+
).not.toBeInTheDocument()
89+
expect(
90+
screen.queryByRole("button", { name: /remove/i }),
91+
).not.toBeInTheDocument()
92+
93+
const innerInput = screen.getByDisplayValue("prod") as HTMLInputElement
94+
expect(innerInput).toBeDisabled()
95+
96+
expect(screen.getByText(IMMUTABLE_HELP_TEXT)).toBeInTheDocument()
97+
})
98+
})
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { describe, it, expect } from "vitest"
2+
import { render, screen } from "@testing-library/react"
3+
import { SchemaForm } from "./SchemaForm.tsx"
4+
import { IMMUTABLE_HELP_TEXT } from "../lib/immutable-paths.ts"
5+
6+
const schema = JSON.stringify({
7+
type: "object",
8+
properties: {
9+
version: {
10+
type: "string",
11+
"x-kubernetes-validations": [
12+
{ rule: "self == oldSelf", message: "version is immutable" },
13+
],
14+
},
15+
description: {
16+
type: "string",
17+
},
18+
},
19+
})
20+
21+
const noop = () => {}
22+
23+
describe("SchemaForm immutableMode", () => {
24+
it("renders fields editable and without help text when immutableMode is omitted", () => {
25+
render(
26+
<SchemaForm
27+
openAPISchema={schema}
28+
formData={{ version: "1.0", description: "hi" }}
29+
onChange={noop}
30+
/>,
31+
)
32+
const versionInput = screen.getByLabelText("version") as HTMLInputElement
33+
expect(versionInput).not.toBeDisabled()
34+
expect(screen.queryByText(IMMUTABLE_HELP_TEXT)).not.toBeInTheDocument()
35+
})
36+
37+
it("greys out immutable fields and shows help text when immutableMode is enforce", () => {
38+
render(
39+
<SchemaForm
40+
openAPISchema={schema}
41+
formData={{ version: "1.0", description: "hi" }}
42+
onChange={noop}
43+
immutableMode="enforce"
44+
/>,
45+
)
46+
const versionInput = screen.getByLabelText("version") as HTMLInputElement
47+
expect(versionInput).toBeDisabled()
48+
const descriptionInput = screen.getByLabelText(
49+
"description",
50+
) as HTMLInputElement
51+
expect(descriptionInput).not.toBeDisabled()
52+
expect(screen.getByText(IMMUTABLE_HELP_TEXT)).toBeInTheDocument()
53+
})
54+
55+
it("treats immutableMode='off' the same as omitting the prop", () => {
56+
render(
57+
<SchemaForm
58+
openAPISchema={schema}
59+
formData={{ version: "1.0", description: "hi" }}
60+
onChange={noop}
61+
immutableMode="off"
62+
/>,
63+
)
64+
const versionInput = screen.getByLabelText("version") as HTMLInputElement
65+
expect(versionInput).not.toBeDisabled()
66+
expect(screen.queryByText(IMMUTABLE_HELP_TEXT)).not.toBeInTheDocument()
67+
})
68+
69+
it("greys out every element of a whole-array immutable field", () => {
70+
// *-as-last: the items schema itself carries the rule, so the entire
71+
// array is immutable. RJSF receives ui:disabled on `items` and
72+
// disables each element's input.
73+
const wholeArraySchema = JSON.stringify({
74+
type: "object",
75+
properties: {
76+
tags: {
77+
type: "array",
78+
items: {
79+
type: "string",
80+
"x-kubernetes-validations": [
81+
{ rule: "self == oldSelf", message: "tags is immutable" },
82+
],
83+
},
84+
},
85+
},
86+
})
87+
render(
88+
<SchemaForm
89+
openAPISchema={wholeArraySchema}
90+
formData={{ tags: ["alpha", "beta"] }}
91+
onChange={noop}
92+
immutableMode="enforce"
93+
/>,
94+
)
95+
expect(screen.getByDisplayValue("alpha")).toBeDisabled()
96+
expect(screen.getByDisplayValue("beta")).toBeDisabled()
97+
expect(screen.getAllByText(IMMUTABLE_HELP_TEXT).length).toBeGreaterThan(0)
98+
// Mirroring the AdditionalPropertiesField map case: the array
99+
// wrapper is disabled, so RJSF disables Add (and per-element
100+
// Remove) too. Otherwise the user could append an entry that the
101+
// overlay would silently drop on save.
102+
expect(screen.getByRole("button", { name: /add/i })).toBeDisabled()
103+
})
104+
105+
it("greys out immutable nested fields inside array items", () => {
106+
const arraySchema = JSON.stringify({
107+
type: "object",
108+
properties: {
109+
volumes: {
110+
type: "array",
111+
items: {
112+
type: "object",
113+
properties: {
114+
name: {
115+
type: "string",
116+
"x-kubernetes-validations": [
117+
{ rule: "self == oldSelf", message: "name immutable" },
118+
],
119+
},
120+
size: { type: "string" },
121+
},
122+
},
123+
},
124+
},
125+
})
126+
render(
127+
<SchemaForm
128+
openAPISchema={arraySchema}
129+
formData={{ volumes: [{ name: "disk1", size: "10Gi" }] }}
130+
onChange={noop}
131+
immutableMode="enforce"
132+
/>,
133+
)
134+
const nameInput = screen.getByDisplayValue("disk1") as HTMLInputElement
135+
const sizeInput = screen.getByDisplayValue("10Gi") as HTMLInputElement
136+
expect(nameInput).toBeDisabled()
137+
expect(sizeInput).not.toBeDisabled()
138+
expect(screen.getByText(IMMUTABLE_HELP_TEXT)).toBeInTheDocument()
139+
})
140+
})

0 commit comments

Comments
 (0)