| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import { describe, expect, it } from "vitest";
- import { modelInputMessageJsonSchema } from "@zcode/contracts";
- import { validateJsonSchemaValue } from "../src/tool/json-schema.js";
- describe("tool JSON schema validator", () => {
- it("accepts base64-only video message blocks and rejects URL fields", () => {
- const message = {
- role: "user",
- content: [
- {
- type: "video",
- mediaType: "video/mp4",
- dataUrl: "data:video/mp4;base64,dmlkZW8=",
- source: { id: "video-1", kind: "inline" },
- },
- ],
- };
- expect(validateJsonSchemaValue(message, modelInputMessageJsonSchema).valid).toBe(true);
- expect(
- validateJsonSchemaValue(
- {
- ...message,
- content: [{ ...message.content[0], url: "https://example.test/video.mp4" }],
- },
- modelInputMessageJsonSchema,
- ).valid,
- ).toBe(false);
- });
- it("validates object properties, required fields, and additionalProperties", () => {
- const schema = {
- type: "object",
- properties: {
- status: { enum: ["ok", "failed"] },
- count: { type: "integer", minimum: 0 },
- },
- required: ["status", "count"],
- additionalProperties: false,
- };
- expect(validateJsonSchemaValue({ status: "ok", count: 2 }, schema).valid).toBe(true);
- const invalid = validateJsonSchemaValue({ status: "maybe", extra: true }, schema);
- expect(invalid.valid).toBe(false);
- expect(invalid.errors).toEqual(
- expect.arrayContaining([
- "$.count is required",
- '$.status must be one of "ok", "failed"',
- "$.extra is not allowed",
- ]),
- );
- expect(invalid.issues).toEqual([
- {
- code: "invalid_value",
- values: ["ok", "failed"],
- path: ["status"],
- message: 'Invalid option: expected one of "ok"|"failed"',
- },
- {
- expected: "number",
- code: "invalid_type",
- path: ["count"],
- message: "Invalid input: expected number, received undefined",
- },
- {
- code: "unrecognized_keys",
- keys: ["extra"],
- path: [],
- message: 'Unrecognized key: "extra"',
- },
- ]);
- });
- it("validates oneOf variants and array item schemas", () => {
- const schema = {
- oneOf: [
- { type: "string" },
- {
- type: "object",
- properties: {
- values: {
- type: "array",
- items: { type: "integer" },
- },
- },
- required: ["values"],
- additionalProperties: false,
- },
- ],
- };
- expect(validateJsonSchemaValue("plain", schema).valid).toBe(true);
- expect(validateJsonSchemaValue({ values: [1, 2, 3] }, schema).valid).toBe(true);
- const invalid = validateJsonSchemaValue({ values: [1, "bad"] }, schema);
- expect(invalid.errors).toContain("$ must match exactly one oneOf schema, matched 0");
- expect(invalid.issues).toEqual([
- {
- code: "invalid_union",
- errors: [
- [
- {
- expected: "string",
- code: "invalid_type",
- path: [],
- message: "Invalid input: expected string, received object",
- },
- ],
- [
- {
- expected: "number",
- code: "invalid_type",
- path: ["values", 1],
- message: "Invalid input: expected number, received string",
- },
- ],
- ],
- path: [],
- message: "Invalid input",
- },
- ]);
- });
- it("emits source-compatible integer and multi-type issues", () => {
- expect(validateJsonSchemaValue(1.5, { type: "integer" }).issues).toEqual([
- {
- expected: "int",
- format: "safeint",
- code: "invalid_type",
- path: [],
- message: "Invalid input: expected int, received number",
- },
- ]);
- expect(validateJsonSchemaValue(true, { type: ["string", "number"] }).issues).toEqual([
- {
- code: "invalid_union",
- errors: [
- [
- {
- expected: "string",
- code: "invalid_type",
- path: [],
- message: "Invalid input: expected string, received boolean",
- },
- ],
- [
- {
- expected: "number",
- code: "invalid_type",
- path: [],
- message: "Invalid input: expected number, received boolean",
- },
- ],
- ],
- path: [],
- message: "Invalid input",
- },
- ]);
- });
- it("does not duplicate type issues after enum or const value constraints fail", () => {
- const invalid = validateJsonSchemaValue(
- {
- mode: 7,
- },
- {
- type: "object",
- properties: {
- mode: { type: "string", enum: ["fast", "safe"] },
- literal: { type: "string", const: "fixed" },
- },
- required: ["mode", "literal"],
- additionalProperties: false,
- },
- );
- expect(invalid.errors).toEqual([
- "$.literal is required",
- '$.mode must be one of "fast", "safe"',
- "$.mode must be string",
- ]);
- expect(invalid.issues).toEqual([
- {
- code: "invalid_value",
- values: ["fast", "safe"],
- path: ["mode"],
- message: 'Invalid option: expected one of "fast"|"safe"',
- },
- {
- code: "invalid_value",
- values: ["fixed"],
- path: ["literal"],
- message: 'Invalid input: expected "fixed"',
- },
- ]);
- });
- it("emits source-compatible upper-bound issues for every supported value origin", () => {
- const invalid = validateJsonSchemaValue(
- {
- count: 3,
- label: "long",
- values: [1, 2],
- },
- {
- type: "object",
- properties: {
- count: { type: "number", maximum: 2 },
- label: { type: "string", maxLength: 3 },
- values: { type: "array", maxItems: 1 },
- },
- required: ["count", "label", "values"],
- additionalProperties: false,
- },
- );
- expect(invalid.issues).toEqual([
- {
- origin: "number",
- code: "too_big",
- maximum: 2,
- inclusive: true,
- path: ["count"],
- message: "Too big: expected number to be <=2",
- },
- {
- origin: "string",
- code: "too_big",
- maximum: 3,
- inclusive: true,
- path: ["label"],
- message: "Too big: expected string to have <=3 characters",
- },
- {
- origin: "array",
- code: "too_big",
- maximum: 1,
- inclusive: true,
- path: ["values"],
- message: "Too big: expected array to have <=1 items",
- },
- ]);
- });
- });
|