@featurevisor/core 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/linter/testSchema.d.ts +4 -0
  13. package/lib/linter/testSchema.js +46 -4
  14. package/lib/linter/testSchema.js.map +1 -1
  15. package/lib/promoter/index.js +62 -1
  16. package/lib/promoter/index.js.map +1 -1
  17. package/lib/tester/testFeature.js +17 -16
  18. package/lib/tester/testFeature.js.map +1 -1
  19. package/package.json +10 -7
  20. package/CHANGELOG.md +0 -2246
  21. package/coverage/clover.xml +0 -5456
  22. package/coverage/coverage-final.json +0 -64
  23. package/coverage/lcov-report/base.css +0 -224
  24. package/coverage/lcov-report/block-navigation.js +0 -87
  25. package/coverage/lcov-report/favicon.png +0 -0
  26. package/coverage/lcov-report/index.html +0 -371
  27. package/coverage/lcov-report/prettify.css +0 -1
  28. package/coverage/lcov-report/prettify.js +0 -2
  29. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  30. package/coverage/lcov-report/sorter.js +0 -210
  31. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  32. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  33. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  34. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  35. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  36. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  37. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  38. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  39. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  40. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  41. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  42. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  43. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  44. package/coverage/lcov-report/src/builder/index.html +0 -311
  45. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  46. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  47. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  48. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  49. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  50. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  51. package/coverage/lcov-report/src/cli/index.html +0 -131
  52. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  53. package/coverage/lcov-report/src/config/index.html +0 -131
  54. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  55. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  56. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  57. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  58. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  59. package/coverage/lcov-report/src/datasource/index.html +0 -161
  60. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  61. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  62. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  63. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  64. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  65. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  66. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  67. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  68. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  69. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  70. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  71. package/coverage/lcov-report/src/index.html +0 -131
  72. package/coverage/lcov-report/src/info/index.html +0 -116
  73. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  74. package/coverage/lcov-report/src/init/index.html +0 -116
  75. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  76. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  77. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  78. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  79. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  80. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  81. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  82. package/coverage/lcov-report/src/linter/index.html +0 -326
  83. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  84. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  85. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  86. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  87. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  88. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  89. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  90. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  91. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  92. package/coverage/lcov-report/src/list/index.html +0 -131
  93. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  94. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  95. package/coverage/lcov-report/src/promoter/index.html +0 -116
  96. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  97. package/coverage/lcov-report/src/sets.ts.html +0 -313
  98. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  99. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  100. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  101. package/coverage/lcov-report/src/tester/index.html +0 -206
  102. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  103. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  104. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  105. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  106. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  107. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  108. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  109. package/coverage/lcov-report/src/utils/index.html +0 -161
  110. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  111. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  112. package/coverage/lcov.info +0 -11615
  113. package/jest.config.js +0 -22
  114. package/json-schema/README.md +0 -5
  115. package/json-schema/attribute.json +0 -40
  116. package/json-schema/feature.json +0 -394
  117. package/json-schema/segment.json +0 -137
  118. package/src/assess-distribution/index.ts +0 -223
  119. package/src/benchmark/index.ts +0 -236
  120. package/src/builder/allocator.spec.ts +0 -105
  121. package/src/builder/allocator.ts +0 -45
  122. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  123. package/src/builder/applyContextToConditions.ts +0 -139
  124. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  125. package/src/builder/applyContextToDatafile.ts +0 -229
  126. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  127. package/src/builder/applyContextToSegments.ts +0 -165
  128. package/src/builder/buildDatafile.spec.ts +0 -445
  129. package/src/builder/buildDatafile.ts +0 -656
  130. package/src/builder/buildProject.ts +0 -344
  131. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  132. package/src/builder/getFeatureRanges.ts +0 -61
  133. package/src/builder/hashes.ts +0 -109
  134. package/src/builder/index.ts +0 -2
  135. package/src/builder/mutateVariables.spec.ts +0 -1179
  136. package/src/builder/mutateVariables.ts +0 -112
  137. package/src/builder/mutator.spec.ts +0 -413
  138. package/src/builder/mutator.ts +0 -237
  139. package/src/builder/revision.spec.ts +0 -22
  140. package/src/builder/revision.ts +0 -22
  141. package/src/builder/traffic.spec.ts +0 -946
  142. package/src/builder/traffic.ts +0 -192
  143. package/src/cli/cli.ts +0 -105
  144. package/src/cli/index.ts +0 -2
  145. package/src/cli/plugins.ts +0 -43
  146. package/src/config/index.ts +0 -1
  147. package/src/config/projectConfig.spec.ts +0 -194
  148. package/src/config/projectConfig.ts +0 -280
  149. package/src/datasource/adapter.ts +0 -56
  150. package/src/datasource/datasource.ts +0 -305
  151. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  152. package/src/datasource/filesystemAdapter.ts +0 -497
  153. package/src/datasource/index.ts +0 -3
  154. package/src/dependencies.ts +0 -13
  155. package/src/evaluate/index.ts +0 -243
  156. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  157. package/src/find-duplicate-segments/index.ts +0 -74
  158. package/src/find-usage/index.ts +0 -565
  159. package/src/generate-code/index.ts +0 -106
  160. package/src/generate-code/typescript.spec.ts +0 -214
  161. package/src/generate-code/typescript.ts +0 -608
  162. package/src/index.ts +0 -20
  163. package/src/info/index.ts +0 -129
  164. package/src/init/index.ts +0 -65
  165. package/src/linter/attributeSchema.spec.ts +0 -342
  166. package/src/linter/attributeSchema.ts +0 -626
  167. package/src/linter/checkCircularDependency.ts +0 -45
  168. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  169. package/src/linter/conditionSchema.spec.ts +0 -510
  170. package/src/linter/conditionSchema.ts +0 -713
  171. package/src/linter/featureSchema.spec.ts +0 -1985
  172. package/src/linter/featureSchema.ts +0 -1922
  173. package/src/linter/groupSchema.ts +0 -47
  174. package/src/linter/index.ts +0 -1
  175. package/src/linter/lintProject.spec.ts +0 -336
  176. package/src/linter/lintProject.ts +0 -873
  177. package/src/linter/mutationNotation.spec.ts +0 -704
  178. package/src/linter/mutationNotation.ts +0 -442
  179. package/src/linter/printError.ts +0 -68
  180. package/src/linter/schema.spec.ts +0 -690
  181. package/src/linter/schema.ts +0 -491
  182. package/src/linter/segmentSchema.spec.ts +0 -358
  183. package/src/linter/segmentSchema.ts +0 -18
  184. package/src/linter/targetSchema.spec.ts +0 -159
  185. package/src/linter/targetSchema.ts +0 -46
  186. package/src/linter/testSchema.spec.ts +0 -258
  187. package/src/linter/testSchema.ts +0 -148
  188. package/src/linter/zodHelpers.ts +0 -16
  189. package/src/list/index.spec.ts +0 -135
  190. package/src/list/index.ts +0 -660
  191. package/src/list/matrix.spec.ts +0 -49
  192. package/src/list/matrix.ts +0 -212
  193. package/src/promoter/index.spec.ts +0 -617
  194. package/src/promoter/index.ts +0 -1266
  195. package/src/sets.ts +0 -76
  196. package/src/targeting.spec.ts +0 -74
  197. package/src/targeting.ts +0 -106
  198. package/src/tester/cliFormat.ts +0 -16
  199. package/src/tester/helpers.spec.ts +0 -149
  200. package/src/tester/helpers.ts +0 -76
  201. package/src/tester/index.ts +0 -2
  202. package/src/tester/prettyDuration.ts +0 -34
  203. package/src/tester/printTestResult.ts +0 -84
  204. package/src/tester/testFeature.ts +0 -367
  205. package/src/tester/testProject.spec.ts +0 -50
  206. package/src/tester/testProject.ts +0 -311
  207. package/src/tester/testSegment.ts +0 -77
  208. package/src/utils/extractKeys.ts +0 -136
  209. package/src/utils/git.ts +0 -158
  210. package/src/utils/index.ts +0 -3
  211. package/src/utils/pretty.ts +0 -12
  212. package/tsconfig.cjs.json +0 -13
  213. package/tsconfig.typecheck.json +0 -9
@@ -1,690 +0,0 @@
1
- /**
2
- * Unit tests for standalone schema validation (schemas/*.yml).
3
- * Covers getSchemaZodSchema and the refinement helpers: refineEnumMatchesType,
4
- * refineMinimumMaximum, refineStringLengthPattern, refineArrayItems.
5
- */
6
- import { z } from "zod";
7
-
8
- import {
9
- refineEnumMatchesType,
10
- refineMinimumMaximum,
11
- refineStringLengthPattern,
12
- refineArrayItems,
13
- getSchemaZodSchema,
14
- valueZodSchema,
15
- propertyTypeEnum,
16
- } from "./schema";
17
-
18
- /** Build a refinement context that collects issues for assertion. */
19
- function createRefinementCtx(): { issues: z.ZodIssue[]; ctx: z.RefinementCtx } {
20
- const issues: z.ZodIssue[] = [];
21
- const ctx = {
22
- addIssue(issue: Parameters<z.RefinementCtx["addIssue"]>[0]) {
23
- if (typeof issue !== "string") {
24
- issues.push(issue as z.ZodIssue);
25
- }
26
- },
27
- } as z.RefinementCtx;
28
- return { issues, ctx };
29
- }
30
-
31
- /** Return human-readable message from a Zod issue (custom or default). */
32
- function issueMessage(issue: z.ZodIssue): string {
33
- if (issue.code === z.ZodIssueCode.custom && typeof issue.message === "string") {
34
- return issue.message;
35
- }
36
- return issue.message ?? "";
37
- }
38
-
39
- describe("schema.ts :: valueZodSchema and propertyTypeEnum", () => {
40
- describe("valueZodSchema", () => {
41
- it("accepts boolean, string, number", () => {
42
- expect(valueZodSchema.safeParse(true).success).toBe(true);
43
- expect(valueZodSchema.safeParse("x").success).toBe(true);
44
- expect(valueZodSchema.safeParse(1).success).toBe(true);
45
- });
46
-
47
- it("accepts plain objects and arrays of values", () => {
48
- expect(valueZodSchema.safeParse({ a: 1 }).success).toBe(true);
49
- expect(valueZodSchema.safeParse([1, "b"]).success).toBe(true);
50
- });
51
-
52
- it("rejects null at top level", () => {
53
- const r = valueZodSchema.safeParse(null);
54
- expect(r.success).toBe(false);
55
- });
56
- });
57
-
58
- describe("propertyTypeEnum", () => {
59
- it("accepts allowed types", () => {
60
- expect(propertyTypeEnum.safeParse("string").success).toBe(true);
61
- expect(propertyTypeEnum.safeParse("array").success).toBe(true);
62
- expect(propertyTypeEnum.safeParse("object").success).toBe(true);
63
- });
64
-
65
- it("rejects invalid type", () => {
66
- expect(propertyTypeEnum.safeParse("date").success).toBe(false);
67
- });
68
- });
69
- });
70
-
71
- describe("schema.ts :: refineEnumMatchesType", () => {
72
- it("adds no issue when enum values match type string", () => {
73
- const { issues, ctx } = createRefinementCtx();
74
- refineEnumMatchesType({ type: "string", enum: ["a", "b"] }, [], ctx);
75
- expect(issues).toHaveLength(0);
76
- });
77
-
78
- it("adds no issue when enum values match type integer", () => {
79
- const { issues, ctx } = createRefinementCtx();
80
- refineEnumMatchesType({ type: "integer", enum: [1, 2, 3] }, [], ctx);
81
- expect(issues).toHaveLength(0);
82
- });
83
-
84
- it("adds issue when an enum value does not match type", () => {
85
- const { issues, ctx } = createRefinementCtx();
86
- refineEnumMatchesType({ type: "string", enum: ["a", 2, "c"] }, [], ctx);
87
- expect(issues).toHaveLength(1);
88
- expect(issueMessage(issues[0])).toContain("index 1");
89
- expect(issueMessage(issues[0])).toContain('does not match type "string"');
90
- expect(issues[0].path).toEqual(["enum", 1]);
91
- });
92
-
93
- it("adds issue for each enum value that does not match type", () => {
94
- const { issues, ctx } = createRefinementCtx();
95
- refineEnumMatchesType({ type: "boolean", enum: [true, "yes", false] }, [], ctx);
96
- expect(issues.length).toBeGreaterThanOrEqual(1);
97
- expect(issues.some((i) => issueMessage(i).includes("index 1"))).toBe(true);
98
- });
99
-
100
- it("recurses into items and adds issue for enum mismatch in items", () => {
101
- const { issues, ctx } = createRefinementCtx();
102
- refineEnumMatchesType(
103
- {
104
- type: "array",
105
- items: { type: "integer", enum: [1, "two", 3] },
106
- },
107
- [],
108
- ctx,
109
- );
110
- expect(issues).toHaveLength(1);
111
- expect(issues[0].path).toEqual(["items", "enum", 1]);
112
- });
113
-
114
- it("recurses into properties and adds issue for enum mismatch in property", () => {
115
- const { issues, ctx } = createRefinementCtx();
116
- refineEnumMatchesType(
117
- {
118
- type: "object",
119
- properties: {
120
- level: { type: "integer", enum: [1, 2, 3.5] },
121
- },
122
- },
123
- [],
124
- ctx,
125
- );
126
- expect(issues).toHaveLength(1);
127
- expect(issues[0].path).toEqual(["properties", "level", "enum", 2]);
128
- });
129
-
130
- it("recurses into oneOf branches", () => {
131
- const { issues, ctx } = createRefinementCtx();
132
- refineEnumMatchesType(
133
- {
134
- oneOf: [
135
- { type: "string", enum: ["a", "b"] },
136
- { type: "integer", enum: [1, "two"] },
137
- ],
138
- },
139
- [],
140
- ctx,
141
- );
142
- expect(issues).toHaveLength(1);
143
- expect(issues[0].path).toEqual(["oneOf", 1, "enum", 1]);
144
- });
145
-
146
- it("recurses into additionalProperties", () => {
147
- const { issues, ctx } = createRefinementCtx();
148
- refineEnumMatchesType(
149
- {
150
- type: "object",
151
- additionalProperties: { type: "string", enum: ["a", 1] },
152
- },
153
- [],
154
- ctx,
155
- );
156
- expect(issues).toHaveLength(1);
157
- expect(issues[0].path).toEqual(["additionalProperties", "enum", 1]);
158
- });
159
-
160
- it("does nothing when schema is null or not an object", () => {
161
- const { issues: i1, ctx: c1 } = createRefinementCtx();
162
- refineEnumMatchesType(null as any, [], c1);
163
- expect(i1).toHaveLength(0);
164
- const { issues: i2, ctx: c2 } = createRefinementCtx();
165
- refineEnumMatchesType({ type: "string" }, [], c2);
166
- expect(i2).toHaveLength(0);
167
- });
168
- });
169
-
170
- describe("schema.ts :: refineMinimumMaximum", () => {
171
- it("adds no issue when minimum <= maximum", () => {
172
- const { issues, ctx } = createRefinementCtx();
173
- refineMinimumMaximum({ type: "integer", minimum: 0, maximum: 10 }, [], ctx);
174
- expect(issues).toHaveLength(0);
175
- });
176
-
177
- it("adds issue when minimum > maximum for integer", () => {
178
- const { issues, ctx } = createRefinementCtx();
179
- refineMinimumMaximum({ type: "integer", minimum: 10, maximum: 5 }, [], ctx);
180
- expect(issues).toHaveLength(1);
181
- expect(issueMessage(issues[0])).toContain("minimum");
182
- expect(issueMessage(issues[0])).toContain("maximum");
183
- expect(issues[0].path).toEqual(["minimum"]);
184
- });
185
-
186
- it("adds issue when minimum > maximum for double", () => {
187
- const { issues, ctx } = createRefinementCtx();
188
- refineMinimumMaximum({ type: "double", minimum: 1.5, maximum: 1.0 }, [], ctx);
189
- expect(issues).toHaveLength(1);
190
- });
191
-
192
- it("adds issue when const is less than minimum", () => {
193
- const { issues, ctx } = createRefinementCtx();
194
- refineMinimumMaximum({ type: "integer", minimum: 10, const: 5 }, [], ctx);
195
- expect(issues).toHaveLength(1);
196
- expect(issueMessage(issues[0])).toContain("const");
197
- expect(issueMessage(issues[0])).toContain("minimum");
198
- });
199
-
200
- it("adds issue when const is greater than maximum", () => {
201
- const { issues, ctx } = createRefinementCtx();
202
- refineMinimumMaximum({ type: "integer", maximum: 10, const: 15 }, [], ctx);
203
- expect(issues).toHaveLength(1);
204
- });
205
-
206
- it("adds issue when an enum value is out of range", () => {
207
- const { issues, ctx } = createRefinementCtx();
208
- refineMinimumMaximum({ type: "integer", minimum: 1, maximum: 5, enum: [1, 3, 10] }, [], ctx);
209
- expect(issues).toHaveLength(1);
210
- expect(issueMessage(issues[0])).toContain("index 2");
211
- expect(issueMessage(issues[0])).toContain("maximum");
212
- });
213
-
214
- it("does nothing for non-numeric types", () => {
215
- const { issues, ctx } = createRefinementCtx();
216
- refineMinimumMaximum({ type: "string", minimum: 0, maximum: 10 }, [], ctx);
217
- expect(issues).toHaveLength(0);
218
- });
219
-
220
- it("recurses into items", () => {
221
- const { issues, ctx } = createRefinementCtx();
222
- refineMinimumMaximum(
223
- {
224
- type: "array",
225
- items: { type: "integer", minimum: 20, maximum: 10 },
226
- },
227
- [],
228
- ctx,
229
- );
230
- expect(issues).toHaveLength(1);
231
- expect(issues[0].path).toEqual(["items", "minimum"]);
232
- });
233
-
234
- it("recurses into additionalProperties", () => {
235
- const { issues, ctx } = createRefinementCtx();
236
- refineMinimumMaximum(
237
- {
238
- type: "object",
239
- additionalProperties: { type: "integer", minimum: 10, maximum: 5 },
240
- },
241
- [],
242
- ctx,
243
- );
244
- expect(issues).toHaveLength(1);
245
- expect(issues[0].path).toEqual(["additionalProperties", "minimum"]);
246
- });
247
- });
248
-
249
- describe("schema.ts :: refineStringLengthPattern", () => {
250
- it("adds no issue when minLength <= maxLength", () => {
251
- const { issues, ctx } = createRefinementCtx();
252
- refineStringLengthPattern({ type: "string", minLength: 1, maxLength: 10 }, [], ctx);
253
- expect(issues).toHaveLength(0);
254
- });
255
-
256
- it("adds issue when minLength > maxLength", () => {
257
- const { issues, ctx } = createRefinementCtx();
258
- refineStringLengthPattern({ type: "string", minLength: 10, maxLength: 5 }, [], ctx);
259
- expect(issues).toHaveLength(1);
260
- expect(issueMessage(issues[0])).toContain("minLength");
261
- expect(issues[0].path).toEqual(["minLength"]);
262
- });
263
-
264
- it("adds issue when pattern is invalid regex", () => {
265
- const { issues, ctx } = createRefinementCtx();
266
- refineStringLengthPattern({ type: "string", pattern: "[" }, [], ctx);
267
- expect(issues).toHaveLength(1);
268
- expect(issueMessage(issues[0])).toContain("pattern");
269
- expect(issueMessage(issues[0])).toContain("invalid");
270
- });
271
-
272
- it("adds no issue when pattern is valid", () => {
273
- const { issues, ctx } = createRefinementCtx();
274
- refineStringLengthPattern({ type: "string", pattern: "^[a-z]+$" }, [], ctx);
275
- expect(issues).toHaveLength(0);
276
- });
277
-
278
- it("adds issue when const string is shorter than minLength", () => {
279
- const { issues, ctx } = createRefinementCtx();
280
- refineStringLengthPattern({ type: "string", minLength: 5, const: "ab" }, [], ctx);
281
- expect(issues).toHaveLength(1);
282
- expect(issueMessage(issues[0])).toContain("const");
283
- });
284
-
285
- it("adds issue when const string is longer than maxLength", () => {
286
- const { issues, ctx } = createRefinementCtx();
287
- refineStringLengthPattern({ type: "string", maxLength: 2, const: "hello" }, [], ctx);
288
- expect(issues).toHaveLength(1);
289
- });
290
-
291
- it("adds issue when const string does not match pattern", () => {
292
- const { issues, ctx } = createRefinementCtx();
293
- refineStringLengthPattern({ type: "string", pattern: "^[0-9]+$", const: "abc" }, [], ctx);
294
- expect(issues).toHaveLength(1);
295
- expect(issueMessage(issues[0])).toContain("pattern");
296
- });
297
-
298
- it("adds issue when an enum string value violates length or pattern", () => {
299
- const { issues, ctx } = createRefinementCtx();
300
- refineStringLengthPattern({ type: "string", maxLength: 2, enum: ["a", "abc", "b"] }, [], ctx);
301
- expect(issues).toHaveLength(1);
302
- expect(issueMessage(issues[0])).toContain("index 1");
303
- });
304
-
305
- it("recurses into items and properties", () => {
306
- const { issues, ctx } = createRefinementCtx();
307
- refineStringLengthPattern(
308
- {
309
- type: "object",
310
- properties: {
311
- code: { type: "string", minLength: 10, maxLength: 5 },
312
- },
313
- },
314
- [],
315
- ctx,
316
- );
317
- expect(issues).toHaveLength(1);
318
- expect(issues[0].path).toEqual(["properties", "code", "minLength"]);
319
- });
320
-
321
- it("recurses into additionalProperties", () => {
322
- const { issues, ctx } = createRefinementCtx();
323
- refineStringLengthPattern(
324
- {
325
- type: "object",
326
- additionalProperties: { type: "string", minLength: 10, maxLength: 5 },
327
- },
328
- [],
329
- ctx,
330
- );
331
- expect(issues).toHaveLength(1);
332
- expect(issues[0].path).toEqual(["additionalProperties", "minLength"]);
333
- });
334
- });
335
-
336
- describe("schema.ts :: refineArrayItems", () => {
337
- it("adds no issue when minItems <= maxItems", () => {
338
- const { issues, ctx } = createRefinementCtx();
339
- refineArrayItems({ type: "array", minItems: 0, maxItems: 10 }, [], ctx);
340
- expect(issues).toHaveLength(0);
341
- });
342
-
343
- it("adds issue when minItems > maxItems", () => {
344
- const { issues, ctx } = createRefinementCtx();
345
- refineArrayItems({ type: "array", minItems: 10, maxItems: 5 }, [], ctx);
346
- expect(issues).toHaveLength(1);
347
- expect(issueMessage(issues[0])).toContain("minItems");
348
- expect(issues[0].path).toEqual(["minItems"]);
349
- });
350
-
351
- it("adds issue when const array length is less than minItems", () => {
352
- const { issues, ctx } = createRefinementCtx();
353
- refineArrayItems({ type: "array", minItems: 3, const: [1, 2] }, [], ctx);
354
- expect(issues).toHaveLength(1);
355
- expect(issueMessage(issues[0])).toContain("minItems");
356
- });
357
-
358
- it("adds issue when const array length is greater than maxItems", () => {
359
- const { issues, ctx } = createRefinementCtx();
360
- refineArrayItems({ type: "array", maxItems: 2, const: [1, 2, 3] }, [], ctx);
361
- expect(issues).toHaveLength(1);
362
- });
363
-
364
- it("adds issue when uniqueItems is true and const array has duplicates", () => {
365
- const { issues, ctx } = createRefinementCtx();
366
- refineArrayItems({ type: "array", uniqueItems: true, const: [1, 2, 1] }, [], ctx);
367
- expect(issues).toHaveLength(1);
368
- expect(issueMessage(issues[0])).toContain("duplicate");
369
- });
370
-
371
- it("adds issue when uniqueItems is true and enum array value has duplicates", () => {
372
- const { issues, ctx } = createRefinementCtx();
373
- refineArrayItems(
374
- {
375
- type: "array",
376
- uniqueItems: true,
377
- enum: [
378
- [1, 2],
379
- [3, 3],
380
- [4, 5],
381
- ],
382
- },
383
- [],
384
- ctx,
385
- );
386
- expect(issues).toHaveLength(1);
387
- expect(issues[0].path).toEqual(["enum", 1]);
388
- });
389
-
390
- it("recurses into items", () => {
391
- const { issues, ctx } = createRefinementCtx();
392
- refineArrayItems(
393
- {
394
- type: "array",
395
- items: { type: "array", minItems: 5, maxItems: 2 },
396
- },
397
- [],
398
- ctx,
399
- );
400
- expect(issues).toHaveLength(1);
401
- expect(issues[0].path).toEqual(["items", "minItems"]);
402
- });
403
-
404
- it("recurses into additionalProperties", () => {
405
- const { issues, ctx } = createRefinementCtx();
406
- refineArrayItems(
407
- {
408
- type: "object",
409
- additionalProperties: {
410
- type: "array",
411
- minItems: 5,
412
- maxItems: 2,
413
- items: { type: "string" },
414
- },
415
- },
416
- [],
417
- ctx,
418
- );
419
- expect(issues).toHaveLength(1);
420
- expect(issues[0].path).toEqual(["additionalProperties", "minItems"]);
421
- });
422
- });
423
-
424
- describe("schema.ts :: getSchemaZodSchema", () => {
425
- it("accepts a minimal valid schema with type and items for array", () => {
426
- const Schema = getSchemaZodSchema([]);
427
- const result = Schema.safeParse({
428
- type: "array",
429
- items: { type: "string" },
430
- });
431
- expect(result.success).toBe(true);
432
- });
433
-
434
- it("accepts a schema with description only (no type)", () => {
435
- const Schema = getSchemaZodSchema([]);
436
- const result = Schema.safeParse({ description: "A schema" });
437
- expect(result.success).toBe(true);
438
- });
439
-
440
- it("rejects unknown schema reference when key not in schemaKeys", () => {
441
- const Schema = getSchemaZodSchema(["link", "color"]);
442
- const result = Schema.safeParse({ schema: "nonexistent" });
443
- expect(result.success).toBe(false);
444
- const err = !result.success ? (result as z.ZodSafeParseError<unknown>).error : null;
445
- expect(err?.issues.some((i) => i.message === 'Unknown schema "nonexistent"')).toBe(true);
446
- });
447
-
448
- it("accepts schema reference when key is in schemaKeys", () => {
449
- const Schema = getSchemaZodSchema(["link"]);
450
- const result = Schema.safeParse({ schema: "link" });
451
- expect(result.success).toBe(true);
452
- });
453
-
454
- it("rejects oneOf with only one branch (requires min 2)", () => {
455
- const Schema = getSchemaZodSchema([]);
456
- const result = Schema.safeParse({
457
- oneOf: [{ type: "string" }],
458
- });
459
- expect(result.success).toBe(false);
460
- const err = !result.success ? (result as z.ZodSafeParseError<unknown>).error : null;
461
- const msg =
462
- err?.issues.map((i) => (typeof i.message === "string" ? i.message : "")).join(" ") ?? "";
463
- expect(msg).toMatch(/array|length|minimum|oneOf|element/i);
464
- });
465
-
466
- it("accepts oneOf with at least two branches", () => {
467
- const Schema = getSchemaZodSchema(["link"]);
468
- const result = Schema.safeParse({
469
- oneOf: [{ type: "string" }, { schema: "link" }],
470
- });
471
- expect(result.success).toBe(true);
472
- });
473
-
474
- it("rejects type array without items", () => {
475
- const Schema = getSchemaZodSchema([]);
476
- const result = Schema.safeParse({ type: "array" });
477
- expect(result.success).toBe(false);
478
- const err = !result.success ? (result as z.ZodSafeParseError<unknown>).error : null;
479
- expect(
480
- err?.issues.some(
481
- (i) =>
482
- typeof i.message === "string" &&
483
- i.message.includes("array") &&
484
- i.message.includes("items"),
485
- ),
486
- ).toBe(true);
487
- });
488
-
489
- it("rejects extra keys (strict)", () => {
490
- const Schema = getSchemaZodSchema([]);
491
- const result = Schema.safeParse({
492
- type: "string",
493
- unknownKey: true,
494
- });
495
- expect(result.success).toBe(false);
496
- });
497
-
498
- it("rejects enum value that does not match type (via refinement)", () => {
499
- const Schema = getSchemaZodSchema([]);
500
- const result = Schema.safeParse({
501
- type: "string",
502
- enum: ["a", 42, "c"],
503
- });
504
- expect(result.success).toBe(false);
505
- const err = !result.success ? (result as z.ZodSafeParseError<unknown>).error : null;
506
- expect(
507
- err?.issues.some(
508
- (i) => typeof i.message === "string" && i.message.includes("does not match type"),
509
- ),
510
- ).toBe(true);
511
- });
512
-
513
- it("rejects minimum > maximum (via refinement)", () => {
514
- const Schema = getSchemaZodSchema([]);
515
- const result = Schema.safeParse({
516
- type: "integer",
517
- minimum: 10,
518
- maximum: 5,
519
- });
520
- expect(result.success).toBe(false);
521
- });
522
-
523
- it("rejects invalid pattern (via refinement)", () => {
524
- const Schema = getSchemaZodSchema([]);
525
- const result = Schema.safeParse({
526
- type: "string",
527
- pattern: "[unclosed",
528
- });
529
- expect(result.success).toBe(false);
530
- });
531
-
532
- it("rejects minItems > maxItems (via refinement)", () => {
533
- const Schema = getSchemaZodSchema([]);
534
- const result = Schema.safeParse({
535
- type: "array",
536
- items: { type: "string" },
537
- minItems: 10,
538
- maxItems: 2,
539
- });
540
- expect(result.success).toBe(false);
541
- });
542
-
543
- it("accepts object schema with properties and required", () => {
544
- const Schema = getSchemaZodSchema([]);
545
- const result = Schema.safeParse({
546
- type: "object",
547
- required: ["id"],
548
- properties: {
549
- id: { type: "string" },
550
- name: { type: "string" },
551
- },
552
- });
553
- expect(result.success).toBe(true);
554
- });
555
-
556
- it("accepts object schema with additionalProperties", () => {
557
- const Schema = getSchemaZodSchema([]);
558
- const result = Schema.safeParse({
559
- type: "object",
560
- additionalProperties: {
561
- type: "string",
562
- },
563
- });
564
- expect(result.success).toBe(true);
565
- });
566
-
567
- it("accepts array schema with items that reference another schema", () => {
568
- const Schema = getSchemaZodSchema(["link"]);
569
- const result = Schema.safeParse({
570
- type: "array",
571
- items: { schema: "link" },
572
- });
573
- expect(result.success).toBe(true);
574
- });
575
-
576
- it("accepts const schema with valid value", () => {
577
- const Schema = getSchemaZodSchema([]);
578
- const result = Schema.safeParse({
579
- type: "string",
580
- const: "draft",
581
- });
582
- expect(result.success).toBe(true);
583
- });
584
-
585
- it("accepts boolean type", () => {
586
- const Schema = getSchemaZodSchema([]);
587
- const result = Schema.safeParse({ type: "boolean" });
588
- expect(result.success).toBe(true);
589
- });
590
-
591
- it("accepts double type with minimum and maximum", () => {
592
- const Schema = getSchemaZodSchema([]);
593
- const result = Schema.safeParse({
594
- type: "double",
595
- minimum: 0,
596
- maximum: 1,
597
- });
598
- expect(result.success).toBe(true);
599
- });
600
-
601
- it("accepts object schema with property that references another schema (e.g. productSummary)", () => {
602
- const Schema = getSchemaZodSchema(["money", "image"]);
603
- const result = Schema.safeParse({
604
- type: "object",
605
- description: "Product summary",
606
- properties: {
607
- id: { type: "string" },
608
- name: { type: "string" },
609
- price: { schema: "money" },
610
- image: { schema: "image" },
611
- },
612
- required: ["id", "name", "price"],
613
- });
614
- expect(result.success).toBe(true);
615
- });
616
-
617
- describe("errors surface properly: intentional mistakes produce correct path and message", () => {
618
- function expectSchemaErrorSurfaces(
619
- schemaKeys: string[],
620
- invalidSchema: unknown,
621
- opts: { pathContains: string[]; messageContains: string },
622
- ): void {
623
- const Schema = getSchemaZodSchema(schemaKeys);
624
- const result = Schema.safeParse(invalidSchema);
625
- expect(result.success).toBe(false);
626
- const err = !result.success ? (result as z.ZodSafeParseError<unknown>).error : null;
627
- expect(err).not.toBeNull();
628
- const messages = (err?.issues ?? [])
629
- .map((i) => (typeof i.message === "string" ? i.message : ""))
630
- .join(" ");
631
- expect(messages).toContain(opts.messageContains);
632
- const pathStrings = (err?.issues ?? []).map((i) => i.path.join("."));
633
- const hasMatchingPath = pathStrings.some((p) =>
634
- opts.pathContains.every((seg) => p.includes(seg)),
635
- );
636
- expect(hasMatchingPath).toBe(true);
637
- }
638
-
639
- it("unknown schema ref: error path includes schema, message says Unknown schema", () => {
640
- expectSchemaErrorSurfaces(
641
- ["link"],
642
- { schema: "nonexistent" },
643
- { pathContains: ["schema"], messageContains: "Unknown schema" },
644
- );
645
- });
646
-
647
- it("array without items: error path points to items, message mentions array and items", () => {
648
- expectSchemaErrorSurfaces(
649
- [],
650
- { type: "array" },
651
- { pathContains: ["items"], messageContains: "array" },
652
- );
653
- });
654
-
655
- it("minimum > maximum: error path points to minimum, message mentions minimum and maximum", () => {
656
- expectSchemaErrorSurfaces(
657
- [],
658
- { type: "integer", minimum: 20, maximum: 10 },
659
- { pathContains: ["minimum"], messageContains: "minimum" },
660
- );
661
- });
662
-
663
- it("invalid pattern: error path points to pattern, message mentions pattern", () => {
664
- expectSchemaErrorSurfaces(
665
- [],
666
- { type: "string", pattern: "[" },
667
- { pathContains: ["pattern"], messageContains: "pattern" },
668
- );
669
- });
670
-
671
- it("enum value does not match type: error path points to enum index, message says match type", () => {
672
- expectSchemaErrorSurfaces(
673
- [],
674
- { type: "string", enum: ["a", 42, "c"] },
675
- { pathContains: ["enum"], messageContains: "match type" },
676
- );
677
- });
678
-
679
- it("extra key: parse fails and message mentions unrecognized key", () => {
680
- const Schema = getSchemaZodSchema([]);
681
- const result = Schema.safeParse({ type: "string", invalidKey: true });
682
- expect(result.success).toBe(false);
683
- const err = !result.success ? (result as z.ZodSafeParseError<unknown>).error : null;
684
- const messages = (err?.issues ?? [])
685
- .map((i) => (typeof i.message === "string" ? i.message : ""))
686
- .join(" ");
687
- expect(messages).toMatch(/unrecognized|invalidKey/i);
688
- });
689
- });
690
- });