SPB Git

Repository Public

schemaForm.test.ts 4.4 KB 76 lines typescript

1import { describe, expect, it } from "vitest";2import { initialValues, schemaToFields, validateForm } from "./schemaForm";34const schema = {5  type: "object",6  required: ["profile", "lines"],7  properties: {8    profile: { type: "string", enum: ["basic", "network", "disk"], title: "Profile", description: "What to collect" },9    lines: { type: "integer", minimum: 1, maximum: 500, default: 200 },10    ratio: { type: "number", minimum: 0, maximum: 1 },11    graceful: { type: "boolean", default: true },12    cache: { type: "string", minLength: 2, maxLength: 16, pattern: "^[a-z][a-z0-9_-]*$" },13    notes: { type: "string", maxLength: 2000 },14    tags: { type: "array", items: { type: "string" } },15    modes: { type: "array", items: { type: "string", enum: ["a", "b"] } },16    extra: { type: "object" },17    nullable: { type: ["string", "null"] },18  },19};2021describe("schemaToFields", () => {22  const fields = schemaToFields(schema);23  const by = (n: string) => fields.find((f) => f.name === n);24  it("maps JSON Schema properties to field kinds", () => {25    expect(fields.map((f) => [f.name, f.kind])).toEqual([26      ["profile", "enum"], ["lines", "integer"], ["ratio", "number"], ["graceful", "boolean"], ["cache", "string"], ["notes", "text"],27      ["tags", "string-list"], ["modes", "enum-list"], ["extra", "json"], ["nullable", "string"],28    ]);29  });30  it("keeps titles, descriptions, constraints, defaults and required flags", () => {31    expect(by("profile")).toMatchObject({ label: "Profile", description: "What to collect", required: true, options: ["basic", "network", "disk"] });32    expect(by("lines")).toMatchObject({ min: 1, max: 500, defaultValue: 200, required: true, label: "lines" });33    expect(by("cache")).toMatchObject({ minLength: 2, maxLength: 16, pattern: "^[a-z][a-z0-9_-]*$", required: false });34  });35  it("returns no field for schemas without properties", () => {36    expect(schemaToFields({ type: "object" })).toEqual([]);37    expect(schemaToFields(null)).toEqual([]);38    expect(schemaToFields("x")).toEqual([]);39  });40  it("builds initial values from defaults", () => {41    expect(initialValues(fields)).toEqual({ profile: "", lines: "200", ratio: "", graceful: true, cache: "", notes: "", tags: [], modes: [], extra: "", nullable: "" });42  });43});4445describe("validateForm", () => {46  const fields = schemaToFields(schema);47  it("coerces valid input and omits empty optional fields", () => {48    const r = validateForm(fields, { profile: "disk", lines: "50", ratio: "0,5", graceful: false, cache: "", notes: "", tags: "a, b\nc", modes: ["a"], extra: '{"k":1}', nullable: "" });49    expect(r.errors).toEqual([]);50    expect(r.params).toEqual({ profile: "disk", lines: 50, ratio: 0.5, graceful: false, tags: ["a", "b", "c"], modes: ["a"], extra: { k: 1 } });51  });52  it("reports required, range, type, pattern, enum and JSON errors", () => {53    const r = validateForm(fields, { profile: "", lines: "501", ratio: "abc", graceful: true, cache: "X", notes: "", tags: [], modes: ["z"], extra: "{oops", nullable: "" });54    expect(r.errors).toEqual([55      { field: "profile", code: "required" },56      { field: "lines", code: "max", limit: 500 },57      { field: "ratio", code: "number" },58      { field: "cache", code: "minLength", limit: 2 },59      { field: "modes", code: "enum" },60      { field: "extra", code: "json" },61    ]);62  });63  it("checks integers, minimum, max length and pattern", () => {64    const base = { profile: "basic", graceful: false };65    expect(validateForm(fields, { ...base, lines: "1.5" }).errors).toEqual([{ field: "lines", code: "integer" }]);66    expect(validateForm(fields, { ...base, lines: "0" }).errors).toEqual([{ field: "lines", code: "min", limit: 1 }]);67    expect(validateForm(fields, { ...base, lines: "5", cache: "a".repeat(17) }).errors).toEqual([{ field: "cache", code: "maxLength", limit: 16 }]);68    expect(validateForm(fields, { ...base, lines: "5", cache: "9abc" }).errors).toEqual([{ field: "cache", code: "pattern", limit: "^[a-z][a-z0-9_-]*$" }]);69    expect(validateForm(fields, { ...base, lines: "5", profile: "root" }).errors).toEqual([{ field: "profile", code: "enum" }]);70  });71  it("always sends booleans and leaves uncompilable patterns to the server", () => {72    const f = schemaToFields({ type: "object", properties: { on: { type: "boolean" }, odd: { type: "string", pattern: "(?<!x" } } });73    expect(validateForm(f, { on: undefined, odd: "anything" })).toEqual({ errors: [], params: { on: false, odd: "anything" } });74  });75});76
Files main
Menu

Theme