json-schema.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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("validates object properties, required fields, and additionalProperties", () => {
  29. const schema = {
  30. type: "object",
  31. properties: {
  32. status: { enum: ["ok", "failed"] },
  33. count: { type: "integer", minimum: 0 },
  34. },
  35. required: ["status", "count"],
  36. additionalProperties: false,
  37. };
  38. expect(validateJsonSchemaValue({ status: "ok", count: 2 }, schema).valid).toBe(true);
  39. const invalid = validateJsonSchemaValue({ status: "maybe", extra: true }, schema);
  40. expect(invalid.valid).toBe(false);
  41. expect(invalid.errors).toEqual(
  42. expect.arrayContaining([
  43. "$.count is required",
  44. '$.status must be one of "ok", "failed"',
  45. "$.extra is not allowed",
  46. ]),
  47. );
  48. expect(invalid.issues).toEqual([
  49. {
  50. code: "invalid_value",
  51. values: ["ok", "failed"],
  52. path: ["status"],
  53. message: 'Invalid option: expected one of "ok"|"failed"',
  54. },
  55. {
  56. expected: "number",
  57. code: "invalid_type",
  58. path: ["count"],
  59. message: "Invalid input: expected number, received undefined",
  60. },
  61. {
  62. code: "unrecognized_keys",
  63. keys: ["extra"],
  64. path: [],
  65. message: 'Unrecognized key: "extra"',
  66. },
  67. ]);
  68. });
  69. it("validates oneOf variants and array item schemas", () => {
  70. const schema = {
  71. oneOf: [
  72. { type: "string" },
  73. {
  74. type: "object",
  75. properties: {
  76. values: {
  77. type: "array",
  78. items: { type: "integer" },
  79. },
  80. },
  81. required: ["values"],
  82. additionalProperties: false,
  83. },
  84. ],
  85. };
  86. expect(validateJsonSchemaValue("plain", schema).valid).toBe(true);
  87. expect(validateJsonSchemaValue({ values: [1, 2, 3] }, schema).valid).toBe(true);
  88. const invalid = validateJsonSchemaValue({ values: [1, "bad"] }, schema);
  89. expect(invalid.errors).toContain("$ must match exactly one oneOf schema, matched 0");
  90. expect(invalid.issues).toEqual([
  91. {
  92. code: "invalid_union",
  93. errors: [
  94. [
  95. {
  96. expected: "string",
  97. code: "invalid_type",
  98. path: [],
  99. message: "Invalid input: expected string, received object",
  100. },
  101. ],
  102. [
  103. {
  104. expected: "number",
  105. code: "invalid_type",
  106. path: ["values", 1],
  107. message: "Invalid input: expected number, received string",
  108. },
  109. ],
  110. ],
  111. path: [],
  112. message: "Invalid input",
  113. },
  114. ]);
  115. });
  116. it("emits source-compatible integer and multi-type issues", () => {
  117. expect(validateJsonSchemaValue(1.5, { type: "integer" }).issues).toEqual([
  118. {
  119. expected: "int",
  120. format: "safeint",
  121. code: "invalid_type",
  122. path: [],
  123. message: "Invalid input: expected int, received number",
  124. },
  125. ]);
  126. expect(validateJsonSchemaValue(true, { type: ["string", "number"] }).issues).toEqual([
  127. {
  128. code: "invalid_union",
  129. errors: [
  130. [
  131. {
  132. expected: "string",
  133. code: "invalid_type",
  134. path: [],
  135. message: "Invalid input: expected string, received boolean",
  136. },
  137. ],
  138. [
  139. {
  140. expected: "number",
  141. code: "invalid_type",
  142. path: [],
  143. message: "Invalid input: expected number, received boolean",
  144. },
  145. ],
  146. ],
  147. path: [],
  148. message: "Invalid input",
  149. },
  150. ]);
  151. });
  152. it("does not duplicate type issues after enum or const value constraints fail", () => {
  153. const invalid = validateJsonSchemaValue(
  154. {
  155. mode: 7,
  156. },
  157. {
  158. type: "object",
  159. properties: {
  160. mode: { type: "string", enum: ["fast", "safe"] },
  161. literal: { type: "string", const: "fixed" },
  162. },
  163. required: ["mode", "literal"],
  164. additionalProperties: false,
  165. },
  166. );
  167. expect(invalid.errors).toEqual([
  168. "$.literal is required",
  169. '$.mode must be one of "fast", "safe"',
  170. "$.mode must be string",
  171. ]);
  172. expect(invalid.issues).toEqual([
  173. {
  174. code: "invalid_value",
  175. values: ["fast", "safe"],
  176. path: ["mode"],
  177. message: 'Invalid option: expected one of "fast"|"safe"',
  178. },
  179. {
  180. code: "invalid_value",
  181. values: ["fixed"],
  182. path: ["literal"],
  183. message: 'Invalid input: expected "fixed"',
  184. },
  185. ]);
  186. });
  187. it("emits source-compatible upper-bound issues for every supported value origin", () => {
  188. const invalid = validateJsonSchemaValue(
  189. {
  190. count: 3,
  191. label: "long",
  192. values: [1, 2],
  193. },
  194. {
  195. type: "object",
  196. properties: {
  197. count: { type: "number", maximum: 2 },
  198. label: { type: "string", maxLength: 3 },
  199. values: { type: "array", maxItems: 1 },
  200. },
  201. required: ["count", "label", "values"],
  202. additionalProperties: false,
  203. },
  204. );
  205. expect(invalid.issues).toEqual([
  206. {
  207. origin: "number",
  208. code: "too_big",
  209. maximum: 2,
  210. inclusive: true,
  211. path: ["count"],
  212. message: "Too big: expected number to be <=2",
  213. },
  214. {
  215. origin: "string",
  216. code: "too_big",
  217. maximum: 3,
  218. inclusive: true,
  219. path: ["label"],
  220. message: "Too big: expected string to have <=3 characters",
  221. },
  222. {
  223. origin: "array",
  224. code: "too_big",
  225. maximum: 1,
  226. inclusive: true,
  227. path: ["values"],
  228. message: "Too big: expected array to have <=1 items",
  229. },
  230. ]);
  231. });
  232. });