@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,713 +0,0 @@
1
- import type { Attribute, AttributeProperty, Schema } from "@featurevisor/types";
2
- import { z } from "zod";
3
-
4
- import { ProjectConfig } from "../config";
5
- import { refineWithMessage } from "./zodHelpers";
6
-
7
- const commonOperators: [string, ...string[]] = ["equals", "notEquals"];
8
- const numericOperators = ["greaterThan", "greaterThanOrEquals", "lessThan", "lessThanOrEquals"];
9
- const stringOperators = [
10
- "contains",
11
- "notContains",
12
- "startsWith",
13
- "endsWith",
14
- "matches",
15
- "notMatches",
16
- ];
17
- const semverOperators = [
18
- "semverEquals",
19
- "semverNotEquals",
20
- "semverGreaterThan",
21
- "semverGreaterThanOrEquals",
22
- "semverLessThan",
23
- "semverLessThanOrEquals",
24
- ];
25
- const dateOperators = ["before", "after"];
26
- const arrayMembershipOperators = ["in", "notIn"];
27
- const arrayOperators = ["includes", "notIncludes"];
28
- const operatorsWithoutValue = ["exists", "notExists"];
29
-
30
- const isoDateRegex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|([+\-]\d{2}:\d{2})))?$/;
31
-
32
- type SchemaNode = Attribute | AttributeProperty | Schema;
33
- type ResolvedLeaf =
34
- | { kind: "schema"; schema: SchemaNode }
35
- | { kind: "flat-object-property" }
36
- | { kind: "flat-array-item" };
37
-
38
- function isIsoDateString(value: string): boolean {
39
- return isoDateRegex.test(value);
40
- }
41
-
42
- function isPrimitiveAttributeValue(value: unknown): boolean {
43
- return (
44
- value === null ||
45
- typeof value === "string" ||
46
- typeof value === "number" ||
47
- typeof value === "boolean"
48
- );
49
- }
50
-
51
- function isStringArray(value: unknown): value is string[] {
52
- return Array.isArray(value) && value.every((entry) => typeof entry === "string");
53
- }
54
-
55
- function valueDeepEqual(a: unknown, b: unknown): boolean {
56
- if (a === b) return true;
57
- if (typeof a !== typeof b) return false;
58
- if (a === null || b === null) return a === b;
59
-
60
- if (typeof a === "object" && typeof b === "object") {
61
- if (Array.isArray(a) !== Array.isArray(b)) return false;
62
- if (Array.isArray(a) && Array.isArray(b)) {
63
- if (a.length !== b.length) return false;
64
- return a.every((value, index) => valueDeepEqual(value, b[index]));
65
- }
66
-
67
- const keysA = Object.keys(a as object).sort();
68
- const keysB = Object.keys(b as object).sort();
69
- if (keysA.length !== keysB.length || keysA.some((key, index) => key !== keysB[index])) {
70
- return false;
71
- }
72
-
73
- return keysA.every((key) =>
74
- valueDeepEqual((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key]),
75
- );
76
- }
77
-
78
- return false;
79
- }
80
-
81
- function resolveSchemaRefs(schema: SchemaNode, schemasByKey: Record<string, Schema>): SchemaNode {
82
- if ("schema" in schema && schema.schema && schemasByKey[schema.schema]) {
83
- return resolveSchemaRefs(schemasByKey[schema.schema], schemasByKey);
84
- }
85
-
86
- return schema;
87
- }
88
-
89
- function getLeafType(resolvedLeaf: ResolvedLeaf): string | undefined {
90
- if (resolvedLeaf.kind === "flat-object-property" || resolvedLeaf.kind === "flat-array-item") {
91
- return "primitive";
92
- }
93
-
94
- return "type" in resolvedLeaf.schema ? resolvedLeaf.schema.type : undefined;
95
- }
96
-
97
- function getLeafTypes(resolvedLeaf: ResolvedLeaf, schemasByKey: Record<string, Schema>): string[] {
98
- if (resolvedLeaf.kind === "flat-object-property" || resolvedLeaf.kind === "flat-array-item") {
99
- return ["primitive"];
100
- }
101
-
102
- const resolved = resolveSchemaRefs(resolvedLeaf.schema, schemasByKey) as SchemaNode & {
103
- type?: string;
104
- oneOf?: unknown[];
105
- };
106
-
107
- if (resolved.oneOf && Array.isArray(resolved.oneOf) && resolved.oneOf.length > 0) {
108
- return Array.from(
109
- new Set(
110
- resolved.oneOf.flatMap((branch) =>
111
- getLeafTypes({ kind: "schema", schema: branch as SchemaNode }, schemasByKey),
112
- ),
113
- ),
114
- );
115
- }
116
-
117
- return resolved.type ? [resolved.type] : [];
118
- }
119
-
120
- function resolveAttributePath(
121
- attributePath: string,
122
- attributesByKey: Record<string, Attribute>,
123
- schemasByKey: Record<string, Schema>,
124
- ): ResolvedLeaf | null {
125
- const [rootKey, ...rest] = attributePath.split(".");
126
- const rootAttribute = attributesByKey[rootKey];
127
-
128
- if (!rootAttribute) {
129
- return null;
130
- }
131
-
132
- let current: SchemaNode = rootAttribute;
133
-
134
- if (rest.length === 0) {
135
- return { kind: "schema", schema: resolveSchemaRefs(current, schemasByKey) };
136
- }
137
-
138
- for (let index = 0; index < rest.length; index++) {
139
- const segment = rest[index];
140
- current = resolveSchemaRefs(current, schemasByKey);
141
- const type = "type" in current ? current.type : undefined;
142
-
143
- if (type !== "object") {
144
- return null;
145
- }
146
-
147
- if ("oneOf" in current && Array.isArray(current.oneOf) && current.oneOf.length > 0) {
148
- return null;
149
- }
150
-
151
- const properties = "properties" in current ? current.properties : undefined;
152
- const additionalProperties =
153
- "additionalProperties" in current ? current.additionalProperties : undefined;
154
-
155
- if (properties && typeof properties === "object" && properties[segment]) {
156
- current = properties[segment];
157
- continue;
158
- }
159
-
160
- if (additionalProperties && typeof additionalProperties === "object") {
161
- current = additionalProperties;
162
- continue;
163
- }
164
-
165
- if (!properties && !additionalProperties) {
166
- return index === rest.length - 1 ? { kind: "flat-object-property" } : null;
167
- }
168
-
169
- return null;
170
- }
171
-
172
- return { kind: "schema", schema: resolveSchemaRefs(current, schemasByKey) };
173
- }
174
-
175
- function matchesSchemaValue(
176
- schema: SchemaNode,
177
- value: unknown,
178
- schemasByKey: Record<string, Schema>,
179
- ): boolean {
180
- const resolved = resolveSchemaRefs(schema, schemasByKey) as SchemaNode & {
181
- type?: string;
182
- enum?: unknown[];
183
- const?: unknown;
184
- oneOf?: unknown[];
185
- properties?: Record<string, unknown>;
186
- additionalProperties?: unknown;
187
- required?: string[];
188
- items?: unknown;
189
- minLength?: number;
190
- maxLength?: number;
191
- pattern?: string;
192
- minimum?: number;
193
- maximum?: number;
194
- minItems?: number;
195
- maxItems?: number;
196
- uniqueItems?: boolean;
197
- };
198
-
199
- if (resolved.oneOf && Array.isArray(resolved.oneOf) && resolved.oneOf.length > 0) {
200
- const matches = resolved.oneOf.filter((branch) =>
201
- matchesSchemaValue(branch as SchemaNode, value, schemasByKey),
202
- ).length;
203
- return matches === 1;
204
- }
205
-
206
- if (resolved.const !== undefined) {
207
- return valueDeepEqual(value, resolved.const);
208
- }
209
-
210
- if (Array.isArray(resolved.enum) && resolved.enum.length > 0) {
211
- return resolved.enum.some((entry) => valueDeepEqual(entry, value));
212
- }
213
-
214
- const type = resolved.type;
215
- if (!type) {
216
- return true;
217
- }
218
-
219
- if (type === "boolean") {
220
- return typeof value === "boolean";
221
- }
222
-
223
- if (type === "string" || type === "semver" || type === "date") {
224
- if (typeof value !== "string") {
225
- return false;
226
- }
227
-
228
- if (resolved.minLength !== undefined && value.length < resolved.minLength) return false;
229
- if (resolved.maxLength !== undefined && value.length > resolved.maxLength) return false;
230
-
231
- if (resolved.pattern !== undefined) {
232
- try {
233
- if (!new RegExp(resolved.pattern).test(value)) return false;
234
- } catch {
235
- return true;
236
- }
237
- }
238
-
239
- if (type === "date" && !isIsoDateString(value)) {
240
- return false;
241
- }
242
-
243
- return true;
244
- }
245
-
246
- if (type === "integer" || type === "double") {
247
- if (typeof value !== "number") {
248
- return false;
249
- }
250
-
251
- if (type === "integer" && !Number.isInteger(value)) {
252
- return false;
253
- }
254
-
255
- if (resolved.minimum !== undefined && value < resolved.minimum) return false;
256
- if (resolved.maximum !== undefined && value > resolved.maximum) return false;
257
- return true;
258
- }
259
-
260
- if (type === "object") {
261
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
262
- return false;
263
- }
264
-
265
- const objectValue = value as Record<string, unknown>;
266
- const properties = resolved.properties;
267
- const additionalProperties = resolved.additionalProperties;
268
-
269
- if ((!properties || typeof properties !== "object") && !additionalProperties) {
270
- return Object.values(objectValue).every((entry) => isPrimitiveAttributeValue(entry));
271
- }
272
-
273
- if (properties && typeof properties === "object") {
274
- const required = new Set(resolved.required || []);
275
- for (const key of required) {
276
- if (!Object.prototype.hasOwnProperty.call(objectValue, key)) {
277
- return false;
278
- }
279
- }
280
-
281
- for (const key of Object.keys(objectValue)) {
282
- if (properties[key]) {
283
- if (!matchesSchemaValue(properties[key] as SchemaNode, objectValue[key], schemasByKey)) {
284
- return false;
285
- }
286
- continue;
287
- }
288
-
289
- if (additionalProperties && typeof additionalProperties === "object") {
290
- if (
291
- !matchesSchemaValue(additionalProperties as SchemaNode, objectValue[key], schemasByKey)
292
- ) {
293
- return false;
294
- }
295
- continue;
296
- }
297
-
298
- return false;
299
- }
300
-
301
- return true;
302
- }
303
-
304
- if (additionalProperties && typeof additionalProperties === "object") {
305
- return Object.values(objectValue).every((entry) =>
306
- matchesSchemaValue(additionalProperties as SchemaNode, entry, schemasByKey),
307
- );
308
- }
309
-
310
- return true;
311
- }
312
-
313
- if (type === "array") {
314
- if (!Array.isArray(value)) {
315
- return false;
316
- }
317
-
318
- if (resolved.minItems !== undefined && value.length < resolved.minItems) return false;
319
- if (resolved.maxItems !== undefined && value.length > resolved.maxItems) return false;
320
-
321
- if (resolved.uniqueItems) {
322
- for (let i = 0; i < value.length; i++) {
323
- for (let j = i + 1; j < value.length; j++) {
324
- if (valueDeepEqual(value[i], value[j])) return false;
325
- }
326
- }
327
- }
328
-
329
- if (!resolved.items || typeof resolved.items !== "object") {
330
- return value.every((entry) => typeof entry === "string");
331
- }
332
-
333
- return value.every((entry) =>
334
- matchesSchemaValue(resolved.items as SchemaNode, entry, schemasByKey),
335
- );
336
- }
337
-
338
- return true;
339
- }
340
-
341
- function matchesLeafValue(
342
- leaf: ResolvedLeaf,
343
- value: unknown,
344
- schemasByKey: Record<string, Schema>,
345
- ): boolean {
346
- if (leaf.kind === "flat-object-property" || leaf.kind === "flat-array-item") {
347
- return leaf.kind === "flat-array-item"
348
- ? typeof value === "string"
349
- : isPrimitiveAttributeValue(value);
350
- }
351
-
352
- return matchesSchemaValue(leaf.schema, value, schemasByKey);
353
- }
354
-
355
- function getArrayItemLeaf(
356
- leaf: ResolvedLeaf,
357
- schemasByKey: Record<string, Schema>,
358
- ): ResolvedLeaf | null {
359
- if (leaf.kind !== "schema") {
360
- return leaf.kind === "flat-array-item" ? leaf : null;
361
- }
362
-
363
- const resolved = resolveSchemaRefs(leaf.schema, schemasByKey);
364
- if ("oneOf" in resolved && Array.isArray(resolved.oneOf) && resolved.oneOf.length > 0) {
365
- const itemLeaves = resolved.oneOf
366
- .map((branch) =>
367
- getArrayItemLeaf({ kind: "schema", schema: branch as SchemaNode }, schemasByKey),
368
- )
369
- .filter((item): item is ResolvedLeaf => item !== null);
370
-
371
- if (itemLeaves.length === 0) {
372
- return null;
373
- }
374
-
375
- if (itemLeaves.every((item) => item.kind === "flat-array-item")) {
376
- return { kind: "flat-array-item" };
377
- }
378
-
379
- return {
380
- kind: "schema",
381
- schema: {
382
- oneOf: itemLeaves.map((item) =>
383
- item.kind === "schema" ? item.schema : ({ type: "string" } as AttributeProperty),
384
- ),
385
- } as AttributeProperty,
386
- };
387
- }
388
-
389
- if (!("type" in resolved) || resolved.type !== "array") {
390
- return null;
391
- }
392
-
393
- if ("items" in resolved && resolved.items && typeof resolved.items === "object") {
394
- return {
395
- kind: "schema",
396
- schema: resolveSchemaRefs(resolved.items as SchemaNode, schemasByKey),
397
- };
398
- }
399
-
400
- return { kind: "flat-array-item" };
401
- }
402
-
403
- function addIssue(
404
- context: z.RefinementCtx,
405
- message: string,
406
- path: (string | number)[] = ["value"],
407
- ): void {
408
- context.addIssue({
409
- code: z.ZodIssueCode.custom,
410
- message,
411
- path,
412
- });
413
- }
414
-
415
- function validateAttributeAwareCondition(
416
- data: {
417
- attribute: string;
418
- operator: string;
419
- value?: unknown;
420
- regexFlags?: string;
421
- },
422
- context: z.RefinementCtx,
423
- attributesByKey: Record<string, Attribute>,
424
- schemasByKey: Record<string, Schema>,
425
- ): void {
426
- const resolvedLeaf = resolveAttributePath(data.attribute, attributesByKey, schemasByKey);
427
- if (!resolvedLeaf) {
428
- return;
429
- }
430
-
431
- const leafType = getLeafType(resolvedLeaf);
432
- const leafTypes = getLeafTypes(resolvedLeaf, schemasByKey);
433
-
434
- if (operatorsWithoutValue.includes(data.operator)) {
435
- return;
436
- }
437
-
438
- if (resolvedLeaf.kind === "schema" && leafType === "object") {
439
- addIssue(
440
- context,
441
- `Attribute "${data.attribute}" resolves to an object. Use a nested attribute path or \`exists\`/\`notExists\` instead.`,
442
- );
443
- return;
444
- }
445
-
446
- if (
447
- numericOperators.includes(data.operator) &&
448
- !leafTypes.some((type) => ["integer", "double"].includes(type))
449
- ) {
450
- addIssue(
451
- context,
452
- `Operator "${data.operator}" can only be used with integer or double attributes.`,
453
- );
454
- return;
455
- }
456
-
457
- if (
458
- stringOperators.includes(data.operator) &&
459
- !leafTypes.some((type) => ["string", "semver", "date"].includes(type))
460
- ) {
461
- addIssue(context, `Operator "${data.operator}" can only be used with string-like attributes.`);
462
- return;
463
- }
464
-
465
- if (
466
- semverOperators.includes(data.operator) &&
467
- !leafTypes.some((type) => ["string", "semver"].includes(type))
468
- ) {
469
- addIssue(
470
- context,
471
- `Operator "${data.operator}" can only be used with string or semver attributes.`,
472
- );
473
- return;
474
- }
475
-
476
- if (
477
- dateOperators.includes(data.operator) &&
478
- !leafTypes.some((type) => ["string", "date"].includes(type))
479
- ) {
480
- addIssue(
481
- context,
482
- `Operator "${data.operator}" can only be used with string or date attributes.`,
483
- );
484
- return;
485
- }
486
-
487
- if (arrayMembershipOperators.includes(data.operator)) {
488
- if (
489
- !["primitive", "string", "semver", "date", "integer", "double", "boolean"].includes(
490
- leafType || "",
491
- )
492
- ) {
493
- addIssue(
494
- context,
495
- `Operator "${data.operator}" can only be used with primitive attribute values.`,
496
- );
497
- return;
498
- }
499
-
500
- if (!Array.isArray(data.value)) {
501
- return;
502
- }
503
-
504
- data.value.forEach((entry, index) => {
505
- if (!matchesLeafValue(resolvedLeaf, entry, schemasByKey)) {
506
- addIssue(
507
- context,
508
- `Value at index ${index} does not match the schema of attribute "${data.attribute}".`,
509
- ["value", index],
510
- );
511
- }
512
- });
513
-
514
- return;
515
- }
516
-
517
- if (arrayOperators.includes(data.operator)) {
518
- const itemLeaf = getArrayItemLeaf(resolvedLeaf, schemasByKey);
519
- if (!itemLeaf) {
520
- addIssue(context, `Operator "${data.operator}" can only be used with array attributes.`);
521
- return;
522
- }
523
-
524
- const itemType = getLeafType(itemLeaf);
525
- if (itemType === "object" || itemType === "array") {
526
- addIssue(context, `Operator "${data.operator}" only supports arrays of primitive values.`);
527
- return;
528
- }
529
-
530
- if (!matchesLeafValue(itemLeaf, data.value, schemasByKey)) {
531
- addIssue(context, `Value does not match the item schema of attribute "${data.attribute}".`);
532
- }
533
-
534
- return;
535
- }
536
-
537
- if (commonOperators.includes(data.operator)) {
538
- if (data.value === null) {
539
- return;
540
- }
541
-
542
- if (!matchesLeafValue(resolvedLeaf, data.value, schemasByKey)) {
543
- addIssue(context, `Value does not match the schema of attribute "${data.attribute}".`);
544
- }
545
- }
546
- }
547
-
548
- export function getConditionsZodSchema(
549
- projectConfig: ProjectConfig,
550
- attributesByKey: Record<string, Attribute>,
551
- schemasByKey: Record<string, Schema> = {},
552
- ) {
553
- const plainConditionZodSchema = z
554
- .object({
555
- attribute: refineWithMessage(
556
- z.string(),
557
- (value) => resolveAttributePath(value, attributesByKey, schemasByKey) !== null,
558
- (value) => `Unknown attribute "${value}"`,
559
- ),
560
- operator: z.enum([
561
- ...commonOperators,
562
- ...numericOperators,
563
- ...stringOperators,
564
- ...semverOperators,
565
- ...dateOperators,
566
- ...arrayMembershipOperators,
567
- ...arrayOperators,
568
- ...operatorsWithoutValue,
569
- ]),
570
- value: z
571
- .union([z.string(), z.array(z.string()), z.number(), z.boolean(), z.date(), z.null()])
572
- .optional(),
573
- regexFlags: z
574
- .string()
575
- .refine(
576
- (value) => {
577
- if (typeof value === "undefined") {
578
- return true;
579
- }
580
-
581
- return /^[gimsuy]+$/.test(value) && new Set(value).size === value.length;
582
- },
583
- {
584
- message: `regexFlags must contain unique characters from: g, i, m, s, u, y`,
585
- },
586
- )
587
- .optional(),
588
- })
589
- .superRefine((data, context) => {
590
- if (
591
- commonOperators.includes(data.operator) &&
592
- !(
593
- data.value === null ||
594
- typeof data.value === "string" ||
595
- typeof data.value === "number" ||
596
- typeof data.value === "boolean" ||
597
- data.value instanceof Date ||
598
- data.value === null
599
- )
600
- ) {
601
- addIssue(
602
- context,
603
- `when operator is "${data.operator}", value has to be either a string, number, boolean, date or null`,
604
- );
605
- }
606
-
607
- if (numericOperators.includes(data.operator) && typeof data.value !== "number") {
608
- addIssue(context, `when operator is "${data.operator}", value must be a number`);
609
- }
610
-
611
- if (
612
- [...stringOperators, ...semverOperators].includes(data.operator) &&
613
- typeof data.value !== "string"
614
- ) {
615
- addIssue(context, `when operator is "${data.operator}", value must be a string`);
616
- }
617
-
618
- if (dateOperators.includes(data.operator)) {
619
- if (typeof data.value === "undefined" || data.value === null || data.value === "") {
620
- addIssue(context, `when operator is "${data.operator}", value must be provided`);
621
- } else if (typeof data.value === "string") {
622
- if (!isIsoDateString(data.value)) {
623
- addIssue(
624
- context,
625
- `when operator is "${data.operator}", value must be a stringified date in ISO 8601 format`,
626
- );
627
- }
628
- } else if (data.value instanceof Date) {
629
- if (isNaN(data.value.getTime())) {
630
- addIssue(context, `when operator is "${data.operator}", value is a Date but invalid`);
631
- }
632
- } else {
633
- addIssue(
634
- context,
635
- `when operator is "${data.operator}", value must be a stringified date in ISO 8601 format or a valid Date object`,
636
- );
637
- }
638
- }
639
-
640
- if (arrayMembershipOperators.includes(data.operator) && !Array.isArray(data.value)) {
641
- addIssue(context, `when operator is "${data.operator}", value must be an array of strings`);
642
- }
643
-
644
- if (
645
- arrayMembershipOperators.includes(data.operator) &&
646
- Array.isArray(data.value) &&
647
- !isStringArray(data.value)
648
- ) {
649
- addIssue(context, `when operator is "${data.operator}", value must be an array of strings`);
650
- }
651
-
652
- if (!arrayMembershipOperators.includes(data.operator) && Array.isArray(data.value)) {
653
- if (!["in", "notIn"].includes(data.operator)) {
654
- addIssue(context, `when operator is "${data.operator}", value must not be an array`);
655
- }
656
- }
657
-
658
- if (regexFlagsRequired(data.operator)) {
659
- if (typeof data.value !== "string") {
660
- addIssue(context, `when operator is "${data.operator}", value must be a string`);
661
- }
662
- } else if (data.regexFlags) {
663
- addIssue(
664
- context,
665
- `when operator is nether "matches" nor "notMatches", regexFlags are not needed`,
666
- ["regexFlags"],
667
- );
668
- }
669
-
670
- if (operatorsWithoutValue.includes(data.operator) && data.value !== undefined) {
671
- addIssue(context, `when operator is "${data.operator}", value is not needed`);
672
- }
673
-
674
- validateAttributeAwareCondition(
675
- data as {
676
- attribute: string;
677
- operator: string;
678
- value?: unknown;
679
- regexFlags?: string;
680
- },
681
- context,
682
- attributesByKey,
683
- schemasByKey,
684
- );
685
- });
686
-
687
- const andOrNotConditionZodSchema = z.union([
688
- z
689
- .object({
690
- and: z.array(z.lazy(() => conditionZodSchema)).min(1),
691
- })
692
- .strict(),
693
- z
694
- .object({
695
- or: z.array(z.lazy(() => conditionZodSchema)).min(1),
696
- })
697
- .strict(),
698
- z
699
- .object({
700
- not: z.array(z.lazy(() => conditionZodSchema)).min(1),
701
- })
702
- .strict(),
703
- ]);
704
-
705
- const everyoneZodSchema = z.literal("*");
706
- const conditionZodSchema = z.union([andOrNotConditionZodSchema, plainConditionZodSchema]);
707
-
708
- return z.union([conditionZodSchema, z.array(conditionZodSchema), everyoneZodSchema]);
709
- }
710
-
711
- function regexFlagsRequired(operator: string): boolean {
712
- return operator === "matches" || operator === "notMatches";
713
- }