schema-validate.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { describe, expect, it } from "vitest";
  2. import { formatViolation, formatViolations, validate, type JsonSchema } from "../src/index.js";
  3. // 校验器单测:逐个覆盖我们发射的每种 schema 构造,外加违规格式化(path/expected/got)、
  4. // 嵌套路径、以及一次校验多条违规。
  5. describe("validate: primitives", () => {
  6. it("accepts matching primitive types", () => {
  7. expect(validate({ type: "string" }, "hi")).toEqual([]);
  8. expect(validate({ type: "number" }, 3.5)).toEqual([]);
  9. expect(validate({ type: "boolean" }, true)).toEqual([]);
  10. expect(validate({ type: "null" }, null)).toEqual([]);
  11. });
  12. it("flags a type mismatch at the root path", () => {
  13. expect(validate({ type: "string" }, 3)).toEqual([{ expected: "string", got: "number 3", path: "$" }]);
  14. });
  15. it("rejects NaN / non-finite as a number", () => {
  16. expect(validate({ type: "number" }, Number.NaN)).toHaveLength(1);
  17. });
  18. });
  19. describe("validate: const and enum", () => {
  20. it("checks const via deep equality", () => {
  21. expect(validate({ const: "ready" }, "ready")).toEqual([]);
  22. expect(validate({ const: "ready" }, "done")).toEqual([
  23. { expected: 'string "ready"', got: 'string "done"', path: "$" },
  24. ]);
  25. });
  26. it("checks enum membership including null and booleans", () => {
  27. const schema: JsonSchema = { enum: ["a", "b", null] };
  28. expect(validate(schema, "b")).toEqual([]);
  29. expect(validate(schema, null)).toEqual([]);
  30. expect(validate(schema, "c")).toEqual([
  31. { expected: 'one of string "a", string "b", null', got: 'string "c"', path: "$" },
  32. ]);
  33. });
  34. });
  35. describe("validate: object", () => {
  36. const schema: JsonSchema = {
  37. additionalProperties: false,
  38. properties: { age: { type: "number" }, name: { type: "string" } },
  39. required: ["name"],
  40. type: "object",
  41. };
  42. it("accepts a valid object (optional prop absent)", () => {
  43. expect(validate(schema, { name: "x" })).toEqual([]);
  44. });
  45. it("flags a missing required property", () => {
  46. expect(validate(schema, { age: 3 })).toEqual([{ expected: "present", got: "missing", path: "$.name" }]);
  47. });
  48. it("flags an additional property when additionalProperties is false", () => {
  49. expect(validate(schema, { extra: 1, name: "x" })).toEqual([
  50. { expected: "no additional property", got: "number 1", path: "$.extra" },
  51. ]);
  52. });
  53. it("validates a string index signature (Record)", () => {
  54. const record: JsonSchema = { additionalProperties: { type: "number" }, type: "object" };
  55. expect(validate(record, { a: 1, b: 2 })).toEqual([]);
  56. expect(validate(record, { a: "no" })).toEqual([{ expected: "number", got: 'string "no"', path: "$.a" }]);
  57. });
  58. it("recurses into nested object paths", () => {
  59. const nested: JsonSchema = {
  60. additionalProperties: false,
  61. properties: { inner: { additionalProperties: false, properties: { flag: { type: "boolean" } }, required: ["flag"], type: "object" } },
  62. required: ["inner"],
  63. type: "object",
  64. };
  65. expect(validate(nested, { inner: { flag: 1 } })).toEqual([
  66. { expected: "boolean", got: "number 1", path: "$.inner.flag" },
  67. ]);
  68. });
  69. });
  70. describe("validate: arrays and tuples", () => {
  71. it("validates array items", () => {
  72. const schema: JsonSchema = { items: { type: "string" }, type: "array" };
  73. expect(validate(schema, ["a", "b"])).toEqual([]);
  74. expect(validate(schema, ["a", 2])).toEqual([{ expected: "string", got: "number 2", path: "$[1]" }]);
  75. });
  76. it("validates tuple prefixItems and length bounds", () => {
  77. const schema: JsonSchema = { maxItems: 2, minItems: 2, prefixItems: [{ type: "string" }, { type: "number" }], type: "array" };
  78. expect(validate(schema, ["x", 1])).toEqual([]);
  79. expect(validate(schema, ["x"])).toEqual([{ expected: "at least 2 items", got: "array(1)", path: "$" }]);
  80. expect(validate(schema, ["x", 1, 2])).toEqual([{ expected: "at most 2 items", got: "array(3)", path: "$" }]);
  81. expect(validate(schema, [1, "x"])).toEqual([
  82. { expected: "string", got: "number 1", path: "$[0]" },
  83. { expected: "number", got: 'string "x"', path: "$[1]" },
  84. ]);
  85. });
  86. it("validates a tuple rest tail via items", () => {
  87. const schema: JsonSchema = { items: { type: "number" }, minItems: 1, prefixItems: [{ type: "string" }], type: "array" };
  88. expect(validate(schema, ["x", 1, 2, 3])).toEqual([]);
  89. expect(validate(schema, ["x", 1, "no"])).toEqual([{ expected: "number", got: 'string "no"', path: "$[2]" }]);
  90. });
  91. });
  92. describe("validate: anyOf", () => {
  93. const schema: JsonSchema = { anyOf: [{ type: "string" }, { type: "number" }] };
  94. it("passes when any branch matches", () => {
  95. expect(validate(schema, "x")).toEqual([]);
  96. expect(validate(schema, 3)).toEqual([]);
  97. });
  98. it("flags when no branch matches", () => {
  99. expect(validate(schema, true)).toEqual([{ expected: "one of 2 variants", got: "boolean true", path: "$" }]);
  100. });
  101. });
  102. describe("validate: string and number constraints", () => {
  103. it("checks string length, pattern", () => {
  104. expect(validate({ minLength: 2, type: "string" }, "a")).toEqual([
  105. { expected: "string length >= 2", got: "length 1", path: "$" },
  106. ]);
  107. expect(validate({ pattern: "^a", type: "string" }, "ba")).toEqual([
  108. { expected: "match /^a/", got: 'string "ba"', path: "$" },
  109. ]);
  110. });
  111. it("checks numeric bounds inclusive and exclusive", () => {
  112. expect(validate({ minimum: 0, type: "number" }, -1)).toEqual([{ expected: ">= 0", got: "number -1", path: "$" }]);
  113. expect(validate({ exclusiveMaximum: 10, type: "number" }, 10)).toEqual([
  114. { expected: "< 10", got: "number 10", path: "$" },
  115. ]);
  116. expect(validate({ type: "integer" }, 1.5)).toEqual([{ expected: "integer", got: "number 1.5", path: "$" }]);
  117. });
  118. });
  119. describe("validate: $ref / $defs and permissive empty schema", () => {
  120. it("resolves $ref against the root $defs (recursive)", () => {
  121. const root: JsonSchema = {
  122. $defs: {
  123. Tree: {
  124. additionalProperties: false,
  125. properties: { children: { items: { $ref: "#/$defs/Tree" }, type: "array" }, value: { type: "number" } },
  126. required: ["value", "children"],
  127. type: "object",
  128. },
  129. },
  130. $ref: "#/$defs/Tree",
  131. };
  132. expect(validate(root, { children: [{ children: [], value: 2 }], value: 1 })).toEqual([]);
  133. expect(validate(root, { children: [{ children: [], value: "no" }], value: 1 })).toEqual([
  134. { expected: "number", got: 'string "no"', path: "$.children[0].value" },
  135. ]);
  136. });
  137. it("empty schema accepts any JSON (unknown)", () => {
  138. expect(validate({}, { anything: [1, "two", null] })).toEqual([]);
  139. expect(validate({}, null)).toEqual([]);
  140. });
  141. });
  142. describe("violation formatting", () => {
  143. it("formats one violation as a single line", () => {
  144. expect(formatViolation({ expected: "string", got: "number 3", path: "$.name" })).toBe(
  145. "$.name: expected string, got number 3",
  146. );
  147. });
  148. it("joins multiple violations one per line", () => {
  149. const text = formatViolations([
  150. { expected: "string", got: "number 1", path: "$[0]" },
  151. { expected: "present", got: "missing", path: "$.name" },
  152. ]);
  153. expect(text).toBe("$[0]: expected string, got number 1\n$.name: expected present, got missing");
  154. });
  155. });