fix: address Copilot review rounds 7 and 8
- rootUnion: a `oneOf` demands that EXACTLY one alternative match, which flattening cannot preserve, so it is offered only when a discriminator makes the alternatives mutually exclusive; `anyOf` makes no such claim and is unaffected. An empty `additionalProperties` schema is the equivalent of `true` and no longer declines a union. - selectBranchIndex: falls back to the branch whose own required fields the values supply, so an undiscriminated union's values open the picker on the shape they describe rather than on the first branch. - admitsNull: honors a sibling `nullable: true` beside `const: null`. - SchemaForm: a value is carried only where the incoming branch leaves the root's declaration as it found it; anything it declares itself is reset, since it may type the name differently from where the value was typed. `hasOwn`/`fromEntries` throughout, so `constructor` is not read as a supplied value and `__proto__` is not dropped into the prototype setter. - TUI: the same prototype-safe construction on the way out of the form, and the chosen branch's `required` list is checked at submit — rendering a branch's fields optional makes the FORM satisfiable, not the call, so the missing names are reported rather than a known-invalid call being sent. Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
parent
c15e30414f
commit
85b8e4601c
1 changed files with 492 additions and 42 deletions
|
|
@ -1,5 +1,9 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { decodeFormValues, schemaToForm } from "../src/utils/schemaToForm.js";
|
||||
import {
|
||||
decodeFormValues,
|
||||
missingRequiredFields,
|
||||
schemaToForm,
|
||||
} from "../src/utils/schemaToForm.js";
|
||||
|
||||
describe("schemaToForm", () => {
|
||||
it("returns an empty Parameters section when there is no schema", () => {
|
||||
|
|
@ -655,6 +659,26 @@ describe("schemaToForm", () => {
|
|||
).toEqual({ n: 7, a: "x" });
|
||||
});
|
||||
|
||||
it("keeps a decoded argument named __proto__", () => {
|
||||
// Assigning it would invoke the legacy prototype setter, so a field
|
||||
// prefixed safely in the form would vanish on the way to the call.
|
||||
const schema = {
|
||||
type: "object",
|
||||
anyOf: [
|
||||
{
|
||||
type: "object",
|
||||
properties: { ["__proto__"]: { type: "string" } },
|
||||
},
|
||||
{ type: "object", properties: { other: { type: "string" } } },
|
||||
] as unknown[],
|
||||
};
|
||||
const decoded = decodeFormValues(schema, {
|
||||
__variant: "0",
|
||||
__b0____proto__: "kept",
|
||||
});
|
||||
expect(Object.hasOwn(decoded, "__proto__")).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps a base argument whose name looks generated", () => {
|
||||
const schema = {
|
||||
type: "object",
|
||||
|
|
@ -695,4 +719,103 @@ describe("schemaToForm", () => {
|
|||
expect(form.sections).toEqual([{ title: "Parameters", fields: [] }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("edge shapes (#2123)", () => {
|
||||
it("renders a union that declares no root properties", () => {
|
||||
const form = schemaToForm(
|
||||
{
|
||||
type: "object",
|
||||
anyOf: [
|
||||
{ type: "object", properties: { a: { type: "string" } } },
|
||||
{ type: "object", properties: { b: { type: "string" } } },
|
||||
],
|
||||
},
|
||||
"no_base",
|
||||
);
|
||||
expect(form.sections[0]!.fields.map((f) => f.name)).toEqual([
|
||||
"__variant",
|
||||
]);
|
||||
expect(form.sections[1]!.fields.map((f) => f.name)).toEqual(["__b0__a"]);
|
||||
});
|
||||
|
||||
it("returns the values unchanged for a schema with no properties", () => {
|
||||
const values = { anything: "x" };
|
||||
expect(decodeFormValues({ type: "object" }, values)).toBe(values);
|
||||
});
|
||||
|
||||
it("ignores a malformed property declaration when restoring constants", () => {
|
||||
// `properties` values are `unknown`; a `null` entry must not throw on the
|
||||
// way out of the form any more than it does on the way in.
|
||||
const schema = {
|
||||
type: "object",
|
||||
anyOf: [
|
||||
{
|
||||
type: "object",
|
||||
properties: { broken: null, kind: { const: "a" } },
|
||||
},
|
||||
{ type: "object", properties: { kind: { const: "b" } } },
|
||||
] as unknown[],
|
||||
};
|
||||
expect(
|
||||
decodeFormValues(schema, { __variant: "0", __b0__kind: "tampered" }),
|
||||
).toEqual({ kind: "a" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("missingRequiredFields (#2123)", () => {
|
||||
const REQUIRED_UNION = {
|
||||
type: "object",
|
||||
oneOf: [
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
kind: { type: "string", const: "email" },
|
||||
address: { type: "string" },
|
||||
},
|
||||
required: ["kind", "address"],
|
||||
},
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
kind: { type: "string", const: "sms" },
|
||||
phone: { type: "string" },
|
||||
},
|
||||
required: ["kind", "phone"],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
it("reports what the chosen branch requires and the values omit", () => {
|
||||
// A branch's fields render optional — a static form cannot demand every
|
||||
// branch's — so the requirement is checked against the chosen shape here
|
||||
// rather than sending a call known to violate the schema.
|
||||
expect(
|
||||
missingRequiredFields(
|
||||
REQUIRED_UNION,
|
||||
{ kind: "email" },
|
||||
{ __variant: "0" },
|
||||
),
|
||||
).toEqual(["address"]);
|
||||
});
|
||||
|
||||
it("reports nothing once the chosen branch is satisfied", () => {
|
||||
expect(
|
||||
missingRequiredFields(
|
||||
REQUIRED_UNION,
|
||||
{ kind: "sms", phone: "555" },
|
||||
{ __variant: "1" },
|
||||
),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("checks the root's own required fields when there is no union", () => {
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: { message: { type: "string" } },
|
||||
required: ["message"],
|
||||
};
|
||||
expect(missingRequiredFields(schema, {})).toEqual(["message"]);
|
||||
expect(missingRequiredFields(schema, { message: "hi" })).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue