feat: edit capability setup and inputs
This commit is contained in:
@@ -169,7 +169,7 @@ export const SchemaForm = ({
|
||||
};
|
||||
|
||||
return (
|
||||
<form className="schema-form" onSubmit={handleSubmit}>
|
||||
<form className="schema-form" noValidate onSubmit={handleSubmit}>
|
||||
{renderBeforeFields}
|
||||
<SchemaFieldControl
|
||||
diagnostics={allDiagnostics}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { normalizeSchema, rebaseSchemaField } from "./schema-field.js";
|
||||
import { normalizeSchema, rebaseSchemaField, schemaFieldAtPath } from "./schema-field.js";
|
||||
|
||||
describe("normalizeSchema", () => {
|
||||
it("normalizes primitive, multiline, boolean, and enum fields", () => {
|
||||
@@ -134,4 +134,30 @@ describe("normalizeSchema", () => {
|
||||
"name",
|
||||
]);
|
||||
});
|
||||
|
||||
it("looks up nested object, array, root, and missing schema paths", () => {
|
||||
const root = normalizeSchema({
|
||||
type: "object",
|
||||
properties: {
|
||||
profile: {
|
||||
type: "object",
|
||||
properties: { name: { type: "string" } },
|
||||
},
|
||||
items: { type: "array", items: { type: "integer" } },
|
||||
},
|
||||
});
|
||||
|
||||
expect(schemaFieldAtPath(root, [])).toBe(root);
|
||||
expect(schemaFieldAtPath(root, ["profile", "name"])).toMatchObject({
|
||||
key: "name",
|
||||
kind: "string",
|
||||
path: ["profile", "name"],
|
||||
});
|
||||
expect(schemaFieldAtPath(root, ["items", 2])).toMatchObject({
|
||||
key: "item",
|
||||
kind: "integer",
|
||||
path: ["items", 2],
|
||||
});
|
||||
expect(schemaFieldAtPath(root, ["missing"])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -222,3 +222,30 @@ export const normalizeSchemaField = (
|
||||
|
||||
export const normalizeSchema = (schema: unknown): SchemaField =>
|
||||
normalizeSchemaField(schema);
|
||||
|
||||
/** Find a normalized field without losing the array index in the returned path. */
|
||||
export const schemaFieldAtPath = (
|
||||
root: SchemaField,
|
||||
path: ReadonlyArray<string | number>,
|
||||
): SchemaField | null => {
|
||||
let current = root;
|
||||
const actualPath: Array<string | number> = [];
|
||||
for (const part of path) {
|
||||
if (current.kind === "object") {
|
||||
const child = current.children.find((candidate) => candidate.key === String(part));
|
||||
if (child === undefined) return null;
|
||||
current = child;
|
||||
actualPath.push(child.key);
|
||||
continue;
|
||||
}
|
||||
if (current.kind === "array" && current.item !== null) {
|
||||
const index = typeof part === "number" ? part : Number(part);
|
||||
if (!Number.isSafeInteger(index) || index < 0) return null;
|
||||
current = rebaseSchemaField(current.item, [...actualPath, index]);
|
||||
actualPath.push(index);
|
||||
continue;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return current;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user