json-schema.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { describe, expect, it } from "vitest";
  2. import { modelInputMessageJsonSchema } from "@zcode/contracts";
  3. import { validateJsonSchemaValue } from "../src/tool/json-schema.js";
  4. describe("tool JSON schema validator", () => {
  5. it("accepts base64-only video message blocks and rejects URL fields", () => {
  6. const message = {
  7. role: "user",
  8. content: [
  9. {
  10. type: "video",
  11. mediaType: "video/mp4",
  12. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  13. source: { id: "video-1", kind: "inline" },
  14. },
  15. ],
  16. };
  17. expect(validateJsonSchemaValue(message, modelInputMessageJsonSchema).valid).toBe(true);
  18. expect(
  19. validateJsonSchemaValue(
  20. {
  21. ...message,
  22. content: [{ ...message.content[0], url: "https://example.test/video.mp4" }],
  23. },
  24. modelInputMessageJsonSchema,
  25. ).valid,
  26. ).toBe(false);
  27. });
  28. it("accepts blockIndex on assistant text and reasoning blocks and still rejects unknown fields", () => {
  29. const message = {
  30. role: "assistant",
  31. content: [
  32. { type: "reasoning", text: "think", providerOptions: {}, blockIndex: 0 },
  33. { type: "text", text: "answer", blockIndex: 1 },
  34. ],
  35. toolCalls: [{ id: "call-1", name: "Read", input: {}, blockIndex: 2 }],
  36. };
  37. expect(validateJsonSchemaValue(message, modelInputMessageJsonSchema).valid).toBe(true);
  38. for (const block of [
  39. { type: "text", text: "answer", blockIndex: -1 },
  40. { type: "text", text: "answer", blockIndex: 1.5 },
  41. { type: "text", text: "answer", order: 1 },
  42. { type: "reasoning", text: "think", order: 1 },
  43. ]) {
  44. expect(
  45. validateJsonSchemaValue({ ...message, content: [block] }, modelInputMessageJsonSchema)
  46. .valid,
  47. ).toBe(false);
  48. }
  49. });
  50. it("validates object properties, required fields, and additionalProperties", () => {
  51. const schema = {
  52. type: "object",
  53. properties: {
  54. status: { enum: ["ok", "failed"] },
  55. count: { type: "integer", minimum: 0 },
  56. },
  57. required: ["status", "count"],
  58. additionalProperties: false,
  59. };
  60. expect(validateJsonSchemaValue({ status: "ok", count: 2 }, schema).valid).toBe(true);
  61. const invalid = validateJsonSchemaValue({ status: "maybe", extra: true }, schema);
  62. expect(invalid.valid).toBe(false);
  63. expect(invalid.errors).toEqual(
  64. expect.arrayContaining([
  65. "$.count is required",
  66. '$.status must be one of "ok", "failed"',
  67. "$.extra is not allowed",
  68. ]),
  69. );
  70. expect(invalid.issues).toEqual([
  71. {
  72. code: "invalid_value",
  73. values: ["ok", "failed"],
  74. path: ["status"],
  75. message: 'Invalid option: expected one of "ok"|"failed"',
  76. },
  77. {
  78. expected: "number",
  79. code: "invalid_type",
  80. path: ["count"],
  81. message: "Invalid input: expected number, received undefined",
  82. },
  83. {
  84. code: "unrecognized_keys",
  85. keys: ["extra"],
  86. path: [],
  87. message: 'Unrecognized key: "extra"',
  88. },
  89. ]);
  90. });
  91. it("validates oneOf variants and array item schemas", () => {
  92. const schema = {
  93. oneOf: [
  94. { type: "string" },
  95. {
  96. type: "object",
  97. properties: {
  98. values: {
  99. type: "array",
  100. items: { type: "integer" },
  101. },
  102. },
  103. required: ["values"],
  104. additionalProperties: false,
  105. },
  106. ],
  107. };
  108. expect(validateJsonSchemaValue("plain", schema).valid).toBe(true);
  109. expect(validateJsonSchemaValue({ values: [1, 2, 3] }, schema).valid).toBe(true);
  110. const invalid = validateJsonSchemaValue({ values: [1, "bad"] }, schema);
  111. expect(invalid.errors).toContain("$ must match exactly one oneOf schema, matched 0");
  112. expect(invalid.issues).toEqual([
  113. {
  114. code: "invalid_union",
  115. errors: [
  116. [
  117. {
  118. expected: "string",
  119. code: "invalid_type",
  120. path: [],
  121. message: "Invalid input: expected string, received object",
  122. },
  123. ],
  124. [
  125. {
  126. expected: "number",
  127. code: "invalid_type",
  128. path: ["values", 1],
  129. message: "Invalid input: expected number, received string",
  130. },
  131. ],
  132. ],
  133. path: [],
  134. message: "Invalid input",
  135. },
  136. ]);
  137. });
  138. it("emits source-compatible integer and multi-type issues", () => {
  139. expect(validateJsonSchemaValue(1.5, { type: "integer" }).issues).toEqual([
  140. {
  141. expected: "int",
  142. format: "safeint",
  143. code: "invalid_type",
  144. path: [],
  145. message: "Invalid input: expected int, received number",
  146. },
  147. ]);
  148. expect(validateJsonSchemaValue(true, { type: ["string", "number"] }).issues).toEqual([
  149. {
  150. code: "invalid_union",
  151. errors: [
  152. [
  153. {
  154. expected: "string",
  155. code: "invalid_type",
  156. path: [],
  157. message: "Invalid input: expected string, received boolean",
  158. },
  159. ],
  160. [
  161. {
  162. expected: "number",
  163. code: "invalid_type",
  164. path: [],
  165. message: "Invalid input: expected number, received boolean",
  166. },
  167. ],
  168. ],
  169. path: [],
  170. message: "Invalid input",
  171. },
  172. ]);
  173. });
  174. it("does not duplicate type issues after enum or const value constraints fail", () => {
  175. const invalid = validateJsonSchemaValue(
  176. {
  177. mode: 7,
  178. },
  179. {
  180. type: "object",
  181. properties: {
  182. mode: { type: "string", enum: ["fast", "safe"] },
  183. literal: { type: "string", const: "fixed" },
  184. },
  185. required: ["mode", "literal"],
  186. additionalProperties: false,
  187. },
  188. );
  189. expect(invalid.errors).toEqual([
  190. "$.literal is required",
  191. '$.mode must be one of "fast", "safe"',
  192. "$.mode must be string",
  193. ]);
  194. expect(invalid.issues).toEqual([
  195. {
  196. code: "invalid_value",
  197. values: ["fast", "safe"],
  198. path: ["mode"],
  199. message: 'Invalid option: expected one of "fast"|"safe"',
  200. },
  201. {
  202. code: "invalid_value",
  203. values: ["fixed"],
  204. path: ["literal"],
  205. message: 'Invalid input: expected "fixed"',
  206. },
  207. ]);
  208. });
  209. it("emits source-compatible upper-bound issues for every supported value origin", () => {
  210. const invalid = validateJsonSchemaValue(
  211. {
  212. count: 3,
  213. label: "long",
  214. values: [1, 2],
  215. },
  216. {
  217. type: "object",
  218. properties: {
  219. count: { type: "number", maximum: 2 },
  220. label: { type: "string", maxLength: 3 },
  221. values: { type: "array", maxItems: 1 },
  222. },
  223. required: ["count", "label", "values"],
  224. additionalProperties: false,
  225. },
  226. );
  227. expect(invalid.issues).toEqual([
  228. {
  229. origin: "number",
  230. code: "too_big",
  231. maximum: 2,
  232. inclusive: true,
  233. path: ["count"],
  234. message: "Too big: expected number to be <=2",
  235. },
  236. {
  237. origin: "string",
  238. code: "too_big",
  239. maximum: 3,
  240. inclusive: true,
  241. path: ["label"],
  242. message: "Too big: expected string to have <=3 characters",
  243. },
  244. {
  245. origin: "array",
  246. code: "too_big",
  247. maximum: 1,
  248. inclusive: true,
  249. path: ["values"],
  250. message: "Too big: expected array to have <=1 items",
  251. },
  252. ]);
  253. });
  254. });