@featurevisor/core 3.1.0 → 3.2.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 (208) 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/tester/testFeature.js +17 -16
  13. package/lib/tester/testFeature.js.map +1 -1
  14. package/package.json +10 -7
  15. package/CHANGELOG.md +0 -2246
  16. package/coverage/clover.xml +0 -5456
  17. package/coverage/coverage-final.json +0 -64
  18. package/coverage/lcov-report/base.css +0 -224
  19. package/coverage/lcov-report/block-navigation.js +0 -87
  20. package/coverage/lcov-report/favicon.png +0 -0
  21. package/coverage/lcov-report/index.html +0 -371
  22. package/coverage/lcov-report/prettify.css +0 -1
  23. package/coverage/lcov-report/prettify.js +0 -2
  24. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  25. package/coverage/lcov-report/sorter.js +0 -210
  26. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  27. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  28. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  29. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  30. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  31. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  32. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  33. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  34. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  35. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  36. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  37. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  38. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  39. package/coverage/lcov-report/src/builder/index.html +0 -311
  40. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  41. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  42. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  43. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  44. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  45. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  46. package/coverage/lcov-report/src/cli/index.html +0 -131
  47. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  48. package/coverage/lcov-report/src/config/index.html +0 -131
  49. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  50. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  51. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  52. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  53. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  54. package/coverage/lcov-report/src/datasource/index.html +0 -161
  55. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  56. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  57. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  58. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  59. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  60. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  61. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  62. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  63. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  64. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  65. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  66. package/coverage/lcov-report/src/index.html +0 -131
  67. package/coverage/lcov-report/src/info/index.html +0 -116
  68. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  69. package/coverage/lcov-report/src/init/index.html +0 -116
  70. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  71. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  72. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  73. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  74. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  75. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  76. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  77. package/coverage/lcov-report/src/linter/index.html +0 -326
  78. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  79. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  80. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  81. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  82. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  83. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  84. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  85. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  86. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  87. package/coverage/lcov-report/src/list/index.html +0 -131
  88. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  89. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  90. package/coverage/lcov-report/src/promoter/index.html +0 -116
  91. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  92. package/coverage/lcov-report/src/sets.ts.html +0 -313
  93. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  94. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  95. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  96. package/coverage/lcov-report/src/tester/index.html +0 -206
  97. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  98. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  99. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  100. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  101. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  102. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  103. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  104. package/coverage/lcov-report/src/utils/index.html +0 -161
  105. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  106. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  107. package/coverage/lcov.info +0 -11615
  108. package/jest.config.js +0 -22
  109. package/json-schema/README.md +0 -5
  110. package/json-schema/attribute.json +0 -40
  111. package/json-schema/feature.json +0 -394
  112. package/json-schema/segment.json +0 -137
  113. package/src/assess-distribution/index.ts +0 -223
  114. package/src/benchmark/index.ts +0 -236
  115. package/src/builder/allocator.spec.ts +0 -105
  116. package/src/builder/allocator.ts +0 -45
  117. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  118. package/src/builder/applyContextToConditions.ts +0 -139
  119. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  120. package/src/builder/applyContextToDatafile.ts +0 -229
  121. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  122. package/src/builder/applyContextToSegments.ts +0 -165
  123. package/src/builder/buildDatafile.spec.ts +0 -445
  124. package/src/builder/buildDatafile.ts +0 -656
  125. package/src/builder/buildProject.ts +0 -344
  126. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  127. package/src/builder/getFeatureRanges.ts +0 -61
  128. package/src/builder/hashes.ts +0 -109
  129. package/src/builder/index.ts +0 -2
  130. package/src/builder/mutateVariables.spec.ts +0 -1179
  131. package/src/builder/mutateVariables.ts +0 -112
  132. package/src/builder/mutator.spec.ts +0 -413
  133. package/src/builder/mutator.ts +0 -237
  134. package/src/builder/revision.spec.ts +0 -22
  135. package/src/builder/revision.ts +0 -22
  136. package/src/builder/traffic.spec.ts +0 -946
  137. package/src/builder/traffic.ts +0 -192
  138. package/src/cli/cli.ts +0 -105
  139. package/src/cli/index.ts +0 -2
  140. package/src/cli/plugins.ts +0 -43
  141. package/src/config/index.ts +0 -1
  142. package/src/config/projectConfig.spec.ts +0 -194
  143. package/src/config/projectConfig.ts +0 -280
  144. package/src/datasource/adapter.ts +0 -56
  145. package/src/datasource/datasource.ts +0 -305
  146. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  147. package/src/datasource/filesystemAdapter.ts +0 -497
  148. package/src/datasource/index.ts +0 -3
  149. package/src/dependencies.ts +0 -13
  150. package/src/evaluate/index.ts +0 -243
  151. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  152. package/src/find-duplicate-segments/index.ts +0 -74
  153. package/src/find-usage/index.ts +0 -565
  154. package/src/generate-code/index.ts +0 -106
  155. package/src/generate-code/typescript.spec.ts +0 -214
  156. package/src/generate-code/typescript.ts +0 -608
  157. package/src/index.ts +0 -20
  158. package/src/info/index.ts +0 -129
  159. package/src/init/index.ts +0 -65
  160. package/src/linter/attributeSchema.spec.ts +0 -342
  161. package/src/linter/attributeSchema.ts +0 -626
  162. package/src/linter/checkCircularDependency.ts +0 -45
  163. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  164. package/src/linter/conditionSchema.spec.ts +0 -510
  165. package/src/linter/conditionSchema.ts +0 -713
  166. package/src/linter/featureSchema.spec.ts +0 -1985
  167. package/src/linter/featureSchema.ts +0 -1922
  168. package/src/linter/groupSchema.ts +0 -47
  169. package/src/linter/index.ts +0 -1
  170. package/src/linter/lintProject.spec.ts +0 -336
  171. package/src/linter/lintProject.ts +0 -873
  172. package/src/linter/mutationNotation.spec.ts +0 -704
  173. package/src/linter/mutationNotation.ts +0 -442
  174. package/src/linter/printError.ts +0 -68
  175. package/src/linter/schema.spec.ts +0 -690
  176. package/src/linter/schema.ts +0 -491
  177. package/src/linter/segmentSchema.spec.ts +0 -358
  178. package/src/linter/segmentSchema.ts +0 -18
  179. package/src/linter/targetSchema.spec.ts +0 -159
  180. package/src/linter/targetSchema.ts +0 -46
  181. package/src/linter/testSchema.spec.ts +0 -258
  182. package/src/linter/testSchema.ts +0 -148
  183. package/src/linter/zodHelpers.ts +0 -16
  184. package/src/list/index.spec.ts +0 -135
  185. package/src/list/index.ts +0 -660
  186. package/src/list/matrix.spec.ts +0 -49
  187. package/src/list/matrix.ts +0 -212
  188. package/src/promoter/index.spec.ts +0 -617
  189. package/src/promoter/index.ts +0 -1266
  190. package/src/sets.ts +0 -76
  191. package/src/targeting.spec.ts +0 -74
  192. package/src/targeting.ts +0 -106
  193. package/src/tester/cliFormat.ts +0 -16
  194. package/src/tester/helpers.spec.ts +0 -149
  195. package/src/tester/helpers.ts +0 -76
  196. package/src/tester/index.ts +0 -2
  197. package/src/tester/prettyDuration.ts +0 -34
  198. package/src/tester/printTestResult.ts +0 -84
  199. package/src/tester/testFeature.ts +0 -367
  200. package/src/tester/testProject.spec.ts +0 -50
  201. package/src/tester/testProject.ts +0 -311
  202. package/src/tester/testSegment.ts +0 -77
  203. package/src/utils/extractKeys.ts +0 -136
  204. package/src/utils/git.ts +0 -158
  205. package/src/utils/index.ts +0 -3
  206. package/src/utils/pretty.ts +0 -12
  207. package/tsconfig.cjs.json +0 -13
  208. package/tsconfig.typecheck.json +0 -9
@@ -1,442 +0,0 @@
1
- import type { Schema } from "@featurevisor/types";
2
-
3
- import { parseNotation, type MutationOperation, type PathPart } from "../builder/mutator";
4
-
5
- export type { MutationOperation, PathPart };
6
- export type PathSegment = PathPart;
7
-
8
- const OPERATION_SUFFIX = /:((?:append|prepend|after|before|remove))$/;
9
-
10
- export interface ParsedMutationKey {
11
- rootKey: string;
12
- pathSegments: PathSegment[];
13
- allSegments: PathSegment[];
14
- operation: MutationOperation;
15
- }
16
-
17
- /**
18
- * Returns true if the key looks like mutation notation (contains path or operation).
19
- */
20
- export function isMutationKey(key: string): boolean {
21
- const k = key.trim();
22
- return k.includes(".") || k.includes("[") || OPERATION_SUFFIX.test(k);
23
- }
24
-
25
- /**
26
- * Parse a full variable key (e.g. "config.width", "items[1].name", "tags:append")
27
- * into root variable name, path segments within the variable, and operation.
28
- * Uses the shared parseNotation from the mutator.
29
- */
30
- export function parseMutationKey(key: string): ParsedMutationKey | null {
31
- const rest = key.trim();
32
- if (!rest) return null;
33
-
34
- const { segments, operation } = parseNotation(rest);
35
- const rootKey = segments.length > 0 && "key" in segments[0] ? segments[0].key : "";
36
- const first = segments[0];
37
- const firstPathPart: PathSegment[] =
38
- first && ("index" in first || "selector" in first)
39
- ? [
40
- "index" in first
41
- ? { key: "", index: first.index }
42
- : { key: "", selector: first.selector! },
43
- ]
44
- : [];
45
- const pathWithinVariable = firstPathPart.concat(segments.slice(1));
46
- return { rootKey, pathSegments: pathWithinVariable, allSegments: segments, operation };
47
- }
48
-
49
- /**
50
- * Resolve a schema reference (schema.schema -> schemasByKey[name]).
51
- * Follows one level of reference; the resolved schema may itself have oneOf or another ref.
52
- */
53
- function resolveSchemaRef(
54
- schema: Schema | null,
55
- schemasByKey: Record<string, Schema> | undefined,
56
- ): Schema | null {
57
- if (!schema || typeof schema !== "object") return null;
58
- if (schema.schema && schemasByKey?.[schema.schema]) {
59
- return resolveSchemaRef(schemasByKey[schema.schema], schemasByKey);
60
- }
61
- return schema;
62
- }
63
-
64
- /**
65
- * Return true if the schema is a oneOf (multiple possible shapes); path resolution cannot descend through oneOf.
66
- */
67
- function isOneOfSchema(schema: Schema): boolean {
68
- return Array.isArray(schema.oneOf) && schema.oneOf.length > 0;
69
- }
70
-
71
- /**
72
- * Resolve the schema at a path within a variable schema.
73
- * pathSegments are the path *within* the variable (e.g. for variable "config", path [ {key:"width"} ];
74
- * for variable "items", path [ {index:0}, {key:"name"} ]).
75
- * Returns the schema at that path, or null if the path is invalid.
76
- * Does not descend through oneOf (path through oneOf is considered invalid for mutation targets).
77
- */
78
- export function resolveSchemaAtPath(
79
- variableSchema: Schema | null,
80
- pathSegments: PathSegment[],
81
- schemasByKey?: Record<string, Schema>,
82
- ): Schema | null {
83
- let current: Schema | null = resolveSchemaRef(variableSchema, schemasByKey);
84
- if (!current) return null;
85
-
86
- for (const seg of pathSegments) {
87
- if (isOneOfSchema(current)) return null;
88
- if (seg.key) {
89
- if (current.type !== "object") return null;
90
- const props = current.properties;
91
- const additional = current.additionalProperties;
92
- const next = props && typeof props === "object" ? props[seg.key] : undefined;
93
- if (next === undefined) {
94
- if (!additional || typeof additional !== "object") return null;
95
- current = resolveSchemaRef(additional, schemasByKey);
96
- if (!current) return null;
97
- continue;
98
- }
99
- current = resolveSchemaRef(next, schemasByKey);
100
- if (!current) return null;
101
- }
102
- if ("index" in seg || "selector" in seg) {
103
- if (current.type !== "array") return null;
104
- const itemSchema = current.items;
105
- if (!itemSchema || typeof itemSchema !== "object") return null;
106
- current = resolveSchemaRef(itemSchema, schemasByKey);
107
- if (!current) return null;
108
- }
109
- }
110
- return current;
111
- }
112
-
113
- /**
114
- * Return the schema of the container at the end of path (object or array) and the last segment.
115
- * Used to check if we can do append/prepend (must be array) or remove (object key or array element).
116
- * Does not descend through oneOf.
117
- */
118
- function getContainerSchemaAtPath(
119
- variableSchema: Schema | null,
120
- pathSegments: PathSegment[],
121
- schemasByKey?: Record<string, Schema>,
122
- ): { containerSchema: Schema; lastSegment: PathSegment; parentSchema: Schema } | null {
123
- if (pathSegments.length === 0) {
124
- const resolved = variableSchema ? resolveSchemaRef(variableSchema, schemasByKey) : null;
125
- return resolved
126
- ? { containerSchema: resolved, lastSegment: { key: "" }, parentSchema: resolved }
127
- : null;
128
- }
129
- let current: Schema | null = resolveSchemaRef(variableSchema, schemasByKey);
130
- if (!current) return null;
131
- const pathWithoutLast = pathSegments.slice(0, -1);
132
- const lastSegment = pathSegments[pathSegments.length - 1];
133
- for (const seg of pathWithoutLast) {
134
- if (isOneOfSchema(current)) return null;
135
- if (seg.key) {
136
- if (current.type !== "object") return null;
137
- const props = current.properties;
138
- const additional = current.additionalProperties;
139
- const next = props && typeof props === "object" ? props[seg.key] : undefined;
140
- if (next === undefined) {
141
- if (!additional || typeof additional !== "object") return null;
142
- current = resolveSchemaRef(additional, schemasByKey);
143
- if (!current) return null;
144
- continue;
145
- }
146
- current = resolveSchemaRef(next, schemasByKey);
147
- }
148
- if ("index" in seg || "selector" in seg) {
149
- if (current?.type !== "array") return null;
150
- const itemSchema = current.items;
151
- if (!itemSchema || typeof itemSchema !== "object") return null;
152
- current = resolveSchemaRef(itemSchema, schemasByKey);
153
- }
154
- if (!current) return null;
155
- }
156
- if (!current) return null;
157
- const parentSchema = current;
158
- if ("index" in lastSegment || "selector" in lastSegment) {
159
- let arraySchema: Schema | null = parentSchema;
160
- if (lastSegment.key) {
161
- if (arraySchema.type !== "object") return null;
162
- const props = arraySchema.properties;
163
- const additional = arraySchema.additionalProperties;
164
- const next = props && typeof props === "object" ? props[lastSegment.key] : undefined;
165
- if (next === undefined) {
166
- if (!additional || typeof additional !== "object") return null;
167
- arraySchema = resolveSchemaRef(additional, schemasByKey);
168
- } else {
169
- arraySchema = resolveSchemaRef(next, schemasByKey);
170
- }
171
- if (!arraySchema) return null;
172
- }
173
- const normalizedLastSegment =
174
- "index" in lastSegment
175
- ? ({ key: "", index: lastSegment.index } as PathSegment)
176
- : ({ key: "", selector: lastSegment.selector } as PathSegment);
177
- return {
178
- containerSchema: arraySchema,
179
- lastSegment: normalizedLastSegment,
180
- parentSchema: arraySchema,
181
- };
182
- }
183
- const propSchema =
184
- parentSchema.properties?.[lastSegment.key] ?? parentSchema.additionalProperties;
185
- const resolvedProp =
186
- propSchema && typeof propSchema === "object"
187
- ? resolveSchemaRef(propSchema, schemasByKey)
188
- : null;
189
- return resolvedProp ? { containerSchema: resolvedProp, lastSegment, parentSchema } : null;
190
- }
191
-
192
- export interface MutationValidationResult {
193
- valid: boolean;
194
- rootKey: string;
195
- pathSegments: PathSegment[];
196
- operation: MutationOperation;
197
- valueSchema: Schema | null;
198
- error?: string;
199
- }
200
-
201
- /**
202
- * Validate mutation key against variable schema: root exists, path valid, operation allowed.
203
- * Returns valueSchema to validate the value against (for set: schema at path; for append/prepend/after/before: item schema).
204
- * Uses Schema from @featurevisor/types; validates required (no :remove on required props) and does not allow path through oneOf.
205
- */
206
- export function validateMutationKey(
207
- key: string,
208
- variableSchemaByKey: Record<string, Schema>,
209
- schemasByKey?: Record<string, Schema>,
210
- ): MutationValidationResult {
211
- const parsed = parseMutationKey(key);
212
- if (!parsed) {
213
- return {
214
- valid: false,
215
- rootKey: "",
216
- pathSegments: [],
217
- operation: "set",
218
- valueSchema: null,
219
- error: `Invalid mutation notation: "${key}"`,
220
- };
221
- }
222
- const { rootKey, pathSegments, operation } = parsed;
223
- if (!rootKey) {
224
- return {
225
- valid: false,
226
- rootKey: "",
227
- pathSegments: [],
228
- operation,
229
- valueSchema: null,
230
- error: `Mutation key must start with a variable name: "${key}"`,
231
- };
232
- }
233
- const variableSchema = variableSchemaByKey[rootKey];
234
- if (!variableSchema) {
235
- return {
236
- valid: false,
237
- rootKey,
238
- pathSegments,
239
- operation,
240
- valueSchema: null,
241
- error: `Variable "${rootKey}" is not defined in \`variablesSchema\`.`,
242
- };
243
- }
244
- const resolvedRoot = resolveSchemaRef(variableSchema, schemasByKey);
245
- if (!resolvedRoot) {
246
- const refName =
247
- variableSchema && typeof variableSchema === "object" && "schema" in variableSchema
248
- ? (variableSchema as { schema?: string }).schema
249
- : undefined;
250
- return {
251
- valid: false,
252
- rootKey,
253
- pathSegments,
254
- operation,
255
- valueSchema: null,
256
- error:
257
- refName != null
258
- ? `Schema "${refName}" could not be loaded for variable "${rootKey}".`
259
- : `Could not resolve schema for variable "${rootKey}".`,
260
- };
261
- }
262
- if (pathSegments.length > 0 && isOneOfSchema(resolvedRoot)) {
263
- return {
264
- valid: false,
265
- rootKey,
266
- pathSegments,
267
- operation,
268
- valueSchema: null,
269
- error: `Cannot mutate path into variable "${rootKey}" (root schema is \`oneOf\`; path resolution not defined).`,
270
- };
271
- }
272
-
273
- const container = getContainerSchemaAtPath(variableSchema, pathSegments, schemasByKey);
274
- const valueSchemaAtPath = resolveSchemaAtPath(variableSchema, pathSegments, schemasByKey);
275
-
276
- switch (operation) {
277
- case "append":
278
- case "prepend": {
279
- if (!container) {
280
- return {
281
- valid: false,
282
- rootKey,
283
- pathSegments,
284
- operation,
285
- valueSchema: null,
286
- error: `Path "${key}" is invalid for variable "${rootKey}" (path does not exist in schema).`,
287
- };
288
- }
289
- const arrResolved = container.containerSchema;
290
- if (!arrResolved || arrResolved.type !== "array") {
291
- return {
292
- valid: false,
293
- rootKey,
294
- pathSegments,
295
- operation,
296
- valueSchema: null,
297
- error: `Operation ":${operation}" is only allowed on array variables or object properties of type array; path "${key}" does not point to an array.`,
298
- };
299
- }
300
- if (isOneOfSchema(arrResolved)) {
301
- return {
302
- valid: false,
303
- rootKey,
304
- pathSegments,
305
- operation,
306
- valueSchema: null,
307
- error: `Operation ":${operation}" is not allowed when array \`items\` is \`oneOf\` (path "${key}").`,
308
- };
309
- }
310
- const itemSchema =
311
- arrResolved.items && typeof arrResolved.items === "object"
312
- ? resolveSchemaRef(arrResolved.items, schemasByKey)
313
- : null;
314
- return {
315
- valid: true,
316
- rootKey,
317
- pathSegments,
318
- operation,
319
- valueSchema: itemSchema,
320
- };
321
- }
322
- case "after":
323
- case "before":
324
- case "remove": {
325
- if (!container) {
326
- return {
327
- valid: false,
328
- rootKey,
329
- pathSegments,
330
- operation,
331
- valueSchema: null,
332
- error: `Path "${key}" is invalid for variable "${rootKey}" (path does not exist in schema).`,
333
- };
334
- }
335
- const last = container.lastSegment;
336
- if ("index" in last || "selector" in last) {
337
- if (container.parentSchema.type !== "array") {
338
- return {
339
- valid: false,
340
- rootKey,
341
- pathSegments,
342
- operation,
343
- valueSchema: null,
344
- error: `Operation ":${operation}" with array index/selector is only allowed on arrays; path "${key}" does not point to an array element.`,
345
- };
346
- }
347
- if (operation === "after" || operation === "before") {
348
- const parentItems = container.parentSchema.items;
349
- if (parentItems && typeof parentItems === "object" && isOneOfSchema(parentItems)) {
350
- return {
351
- valid: false,
352
- rootKey,
353
- pathSegments,
354
- operation,
355
- valueSchema: null,
356
- error: `Operation ":${operation}" is not allowed when array \`items\` is \`oneOf\` (path "${key}").`,
357
- };
358
- }
359
- const itemSchema =
360
- parentItems && typeof parentItems === "object"
361
- ? resolveSchemaRef(parentItems, schemasByKey)
362
- : null;
363
- return { valid: true, rootKey, pathSegments, operation, valueSchema: itemSchema };
364
- }
365
- return { valid: true, rootKey, pathSegments, operation, valueSchema: null };
366
- }
367
- if (container.parentSchema.type !== "object") {
368
- return {
369
- valid: false,
370
- rootKey,
371
- pathSegments,
372
- operation,
373
- valueSchema: null,
374
- error: `Operation ":${operation}" on a property is only allowed on objects; path "${key}" does not point to an object property.`,
375
- };
376
- }
377
- const requiredKeys = container.parentSchema.required;
378
- if (
379
- operation === "remove" &&
380
- Array.isArray(requiredKeys) &&
381
- requiredKeys.includes(last.key)
382
- ) {
383
- return {
384
- valid: false,
385
- rootKey,
386
- pathSegments,
387
- operation,
388
- valueSchema: null,
389
- error: `Cannot remove required property "${last.key}" from variable "${rootKey}" (listed in schema \`required\`).`,
390
- };
391
- }
392
- return { valid: true, rootKey, pathSegments, operation, valueSchema: null };
393
- }
394
- case "set": {
395
- if (valueSchemaAtPath === null && pathSegments.length > 0) {
396
- return {
397
- valid: false,
398
- rootKey,
399
- pathSegments,
400
- operation,
401
- valueSchema: null,
402
- error: `Path "${key}" is invalid for variable "${rootKey}" (path does not exist in schema).`,
403
- };
404
- }
405
- if (valueSchemaAtPath && isOneOfSchema(valueSchemaAtPath)) {
406
- return {
407
- valid: false,
408
- rootKey,
409
- pathSegments,
410
- operation,
411
- valueSchema: null,
412
- error: `Cannot set value at path "${key}" (target schema is \`oneOf\`; mutation target must be a single schema).`,
413
- };
414
- }
415
- return {
416
- valid: true,
417
- rootKey,
418
- pathSegments,
419
- operation,
420
- valueSchema: valueSchemaAtPath,
421
- };
422
- }
423
- default:
424
- return {
425
- valid: true,
426
- rootKey,
427
- pathSegments,
428
- operation,
429
- valueSchema: valueSchemaAtPath,
430
- };
431
- }
432
- }
433
-
434
- /**
435
- * Parse a path-map key (relative to a variable) into path segments for resolveSchemaAtPath.
436
- * e.g. "display.fontSize" -> [{key:"display"},{key:"fontSize"}], "[0].name" -> [{key:"",index:0},{key:"name"}].
437
- */
438
- export function parsePathMapKey(relativePath: string): PathSegment[] | null {
439
- const parsed = parseMutationKey(relativePath);
440
- if (!parsed) return null;
441
- return parsed.allSegments.length > 0 ? parsed.allSegments : null;
442
- }
@@ -1,68 +0,0 @@
1
- import { ZodError, ZodIssue } from "zod";
2
-
3
- import { CLI_FORMAT_RED } from "../tester/cliFormat";
4
-
5
- export interface LintIssueFromZod {
6
- message: string;
7
- path: (string | number)[];
8
- code?: string;
9
- value?: unknown;
10
- }
11
-
12
- function normalizePath(path: PropertyKey[]): (string | number)[] {
13
- return path.map((segment) => (typeof segment === "symbol" ? String(segment) : segment));
14
- }
15
-
16
- function getInvalidUnionIssue(issue: ZodIssue): ZodIssue | undefined {
17
- if (issue.code !== "invalid_union" || issue.path.length > 0) {
18
- return undefined;
19
- }
20
-
21
- const unionErrors = (issue as any).unionErrors as ZodError[] | undefined;
22
- if (Array.isArray(unionErrors) && unionErrors.length > 0) {
23
- return unionErrors[unionErrors.length - 1].issues[0];
24
- }
25
-
26
- const errors = (issue as any).errors as ZodIssue[][] | undefined;
27
- if (Array.isArray(errors) && errors.length > 0) {
28
- return errors[errors.length - 1][0];
29
- }
30
-
31
- return undefined;
32
- }
33
-
34
- export function getLintIssuesFromZodError(e: ZodError): LintIssueFromZod[] {
35
- return e.issues
36
- .map((issue) => {
37
- const nestedIssue = getInvalidUnionIssue(issue);
38
-
39
- if (nestedIssue) {
40
- return {
41
- message: nestedIssue.message,
42
- path: normalizePath(nestedIssue.path),
43
- code: nestedIssue.code,
44
- value: (nestedIssue as any).received,
45
- };
46
- }
47
-
48
- return {
49
- message: issue.message,
50
- path: normalizePath(issue.path),
51
- code: issue.code,
52
- value: (issue as any).received,
53
- };
54
- })
55
- .filter(Boolean);
56
- }
57
-
58
- export function printZodError(e: ZodError) {
59
- const issues = getLintIssuesFromZodError(e);
60
- issues.forEach((issue) => {
61
- console.error(CLI_FORMAT_RED, ` => Error: ${issue.message}`);
62
- console.error(" Path:", issue.path.join("."));
63
- if (typeof issue.value !== "undefined" && issue.value !== "undefined") {
64
- console.error(" Value:", issue.value);
65
- }
66
- console.error("");
67
- });
68
- }