@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,626 +0,0 @@
1
- import type { Attribute, AttributeProperty } from "@featurevisor/types";
2
- import { z } from "zod";
3
-
4
- import { valueZodSchema, refineArrayItems, refineMinimumMaximum } from "./schema";
5
-
6
- const attributeTypeEnum = z.enum([
7
- "boolean",
8
- "string",
9
- "integer",
10
- "double",
11
- "date",
12
- "semver",
13
- "object",
14
- "array",
15
- ]);
16
-
17
- type AttributeSchemaLike = {
18
- type?: Attribute["type"];
19
- enum?: unknown[];
20
- const?: unknown;
21
- minimum?: number;
22
- maximum?: number;
23
- minLength?: number;
24
- maxLength?: number;
25
- pattern?: string;
26
- items?: unknown;
27
- properties?: Record<string, unknown>;
28
- additionalProperties?: unknown;
29
- required?: string[];
30
- oneOf?: unknown[];
31
- };
32
-
33
- function isArrayOfStrings(value: unknown): boolean {
34
- return Array.isArray(value) && value.every((entry) => typeof entry === "string");
35
- }
36
-
37
- function isStringLikeAttributeType(type?: Attribute["type"]): boolean {
38
- return type === "string" || type === "date" || type === "semver";
39
- }
40
-
41
- function valueMatchesAttributeType(value: unknown, type: Attribute["type"]): boolean {
42
- if (type === "date" || type === "semver") {
43
- return typeof value === "string";
44
- }
45
-
46
- switch (type) {
47
- case "string":
48
- return typeof value === "string";
49
- case "boolean":
50
- return typeof value === "boolean";
51
- case "integer":
52
- return typeof value === "number" && Number.isInteger(value);
53
- case "double":
54
- return typeof value === "number";
55
- case "object":
56
- return typeof value === "object" && value !== null && !Array.isArray(value);
57
- case "array":
58
- return isArrayOfStrings(value);
59
- default:
60
- return true;
61
- }
62
- }
63
-
64
- function refineAttributeEnumMatchesType(
65
- schema: AttributeSchemaLike,
66
- pathPrefix: (string | number)[],
67
- ctx: z.RefinementCtx,
68
- ): void {
69
- if (!schema || typeof schema !== "object") return;
70
-
71
- if (schema.type && Array.isArray(schema.enum) && schema.enum.length > 0) {
72
- for (let i = 0; i < schema.enum.length; i++) {
73
- const value = schema.enum[i];
74
- if (!valueMatchesAttributeType(value, schema.type)) {
75
- ctx.addIssue({
76
- code: z.ZodIssueCode.custom,
77
- message: `Enum value at index ${i} (${JSON.stringify(value)}) does not match type "${schema.type}". All enum values must be of the same type as \`type\`.`,
78
- path: [...pathPrefix, "enum", i],
79
- });
80
- }
81
- }
82
- }
83
-
84
- if (schema.items && typeof schema.items === "object") {
85
- refineAttributeEnumMatchesType(
86
- schema.items as AttributeSchemaLike,
87
- [...pathPrefix, "items"],
88
- ctx,
89
- );
90
- }
91
-
92
- if (schema.properties && typeof schema.properties === "object") {
93
- for (const key of Object.keys(schema.properties)) {
94
- refineAttributeEnumMatchesType(
95
- schema.properties[key] as AttributeSchemaLike,
96
- [...pathPrefix, "properties", key],
97
- ctx,
98
- );
99
- }
100
- }
101
-
102
- if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
103
- refineAttributeEnumMatchesType(
104
- schema.additionalProperties as AttributeSchemaLike,
105
- [...pathPrefix, "additionalProperties"],
106
- ctx,
107
- );
108
- }
109
-
110
- if (schema.oneOf && Array.isArray(schema.oneOf)) {
111
- schema.oneOf.forEach((branch, index) => {
112
- if (branch && typeof branch === "object") {
113
- refineAttributeEnumMatchesType(
114
- branch as AttributeSchemaLike,
115
- [...pathPrefix, "oneOf", index],
116
- ctx,
117
- );
118
- }
119
- });
120
- }
121
- }
122
-
123
- function refineAttributeStringLengthPattern(
124
- schema: AttributeSchemaLike,
125
- pathPrefix: (string | number)[],
126
- ctx: z.RefinementCtx,
127
- ): void {
128
- if (!schema || typeof schema !== "object") return;
129
-
130
- const minLength = schema.minLength;
131
- const maxLength = schema.maxLength;
132
- const pattern = schema.pattern;
133
- const isString = isStringLikeAttributeType(schema.type);
134
-
135
- if (isString && minLength !== undefined && maxLength !== undefined && minLength > maxLength) {
136
- ctx.addIssue({
137
- code: z.ZodIssueCode.custom,
138
- message: `When \`type\` is "${schema.type}", \`minLength\` (${minLength}) must be less than or equal to \`maxLength\` (${maxLength}).`,
139
- path: [...pathPrefix, "minLength"],
140
- });
141
- }
142
-
143
- if (isString && pattern !== undefined) {
144
- try {
145
- new RegExp(pattern);
146
- } catch {
147
- ctx.addIssue({
148
- code: z.ZodIssueCode.custom,
149
- message: `\`pattern\` must be a valid ECMA-262 regular expression; "${pattern}" is invalid.`,
150
- path: [...pathPrefix, "pattern"],
151
- });
152
- }
153
- }
154
-
155
- const testString = (value: string) => {
156
- const minOk = minLength === undefined || value.length >= minLength;
157
- const maxOk = maxLength === undefined || value.length <= maxLength;
158
- let patternOk = true;
159
-
160
- if (pattern !== undefined) {
161
- try {
162
- patternOk = new RegExp(pattern).test(value);
163
- } catch {
164
- patternOk = true;
165
- }
166
- }
167
-
168
- return { minOk, maxOk, patternOk };
169
- };
170
-
171
- if (isString && typeof schema.const === "string") {
172
- const result = testString(schema.const);
173
- if (!result.minOk) {
174
- ctx.addIssue({
175
- code: z.ZodIssueCode.custom,
176
- message: `\`const\` value length (${schema.const.length}) is less than \`minLength\` (${minLength}).`,
177
- path: [...pathPrefix, "const"],
178
- });
179
- }
180
- if (!result.maxOk) {
181
- ctx.addIssue({
182
- code: z.ZodIssueCode.custom,
183
- message: `\`const\` value length (${schema.const.length}) is greater than \`maxLength\` (${maxLength}).`,
184
- path: [...pathPrefix, "const"],
185
- });
186
- }
187
- if (!result.patternOk) {
188
- ctx.addIssue({
189
- code: z.ZodIssueCode.custom,
190
- message: `\`const\` value does not match \`pattern\`.`,
191
- path: [...pathPrefix, "const"],
192
- });
193
- }
194
- }
195
-
196
- if (isString && Array.isArray(schema.enum)) {
197
- schema.enum.forEach((value, index) => {
198
- if (typeof value !== "string") return;
199
- const result = testString(value);
200
- if (!result.minOk) {
201
- ctx.addIssue({
202
- code: z.ZodIssueCode.custom,
203
- message: `Enum value at index ${index} length (${value.length}) is less than \`minLength\` (${minLength}).`,
204
- path: [...pathPrefix, "enum", index],
205
- });
206
- }
207
- if (!result.maxOk) {
208
- ctx.addIssue({
209
- code: z.ZodIssueCode.custom,
210
- message: `Enum value at index ${index} length (${value.length}) is greater than \`maxLength\` (${maxLength}).`,
211
- path: [...pathPrefix, "enum", index],
212
- });
213
- }
214
- if (!result.patternOk) {
215
- ctx.addIssue({
216
- code: z.ZodIssueCode.custom,
217
- message: `Enum value at index ${index} does not match \`pattern\`.`,
218
- path: [...pathPrefix, "enum", index],
219
- });
220
- }
221
- });
222
- }
223
-
224
- if (schema.items && typeof schema.items === "object") {
225
- refineAttributeStringLengthPattern(
226
- schema.items as AttributeSchemaLike,
227
- [...pathPrefix, "items"],
228
- ctx,
229
- );
230
- }
231
-
232
- if (schema.properties && typeof schema.properties === "object") {
233
- for (const key of Object.keys(schema.properties)) {
234
- refineAttributeStringLengthPattern(
235
- schema.properties[key] as AttributeSchemaLike,
236
- [...pathPrefix, "properties", key],
237
- ctx,
238
- );
239
- }
240
- }
241
-
242
- if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
243
- refineAttributeStringLengthPattern(
244
- schema.additionalProperties as AttributeSchemaLike,
245
- [...pathPrefix, "additionalProperties"],
246
- ctx,
247
- );
248
- }
249
-
250
- if (schema.oneOf && Array.isArray(schema.oneOf)) {
251
- schema.oneOf.forEach((branch, index) => {
252
- if (branch && typeof branch === "object") {
253
- refineAttributeStringLengthPattern(
254
- branch as AttributeSchemaLike,
255
- [...pathPrefix, "oneOf", index],
256
- ctx,
257
- );
258
- }
259
- });
260
- }
261
- }
262
-
263
- function refineRequiredKeysInAttributeSchema(
264
- schema: AttributeSchemaLike,
265
- pathPrefix: (string | number)[],
266
- ctx: z.RefinementCtx,
267
- ): void {
268
- if (!schema || typeof schema !== "object") return;
269
-
270
- if (
271
- schema.type === "object" &&
272
- Array.isArray(schema.required) &&
273
- schema.properties &&
274
- typeof schema.properties === "object"
275
- ) {
276
- const allowedKeys = Object.keys(schema.properties);
277
- schema.required.forEach((key, index) => {
278
- if (!allowedKeys.includes(key)) {
279
- ctx.addIssue({
280
- code: z.ZodIssueCode.custom,
281
- message: `Unknown required field "${key}". \`required\` must only contain property names defined in \`properties\`. Allowed: ${allowedKeys.length ? allowedKeys.join(", ") : "(none)"}.`,
282
- path: [...pathPrefix, "required", index],
283
- });
284
- }
285
- });
286
- }
287
-
288
- if (schema.properties && typeof schema.properties === "object") {
289
- for (const key of Object.keys(schema.properties)) {
290
- refineRequiredKeysInAttributeSchema(
291
- schema.properties[key] as AttributeSchemaLike,
292
- [...pathPrefix, "properties", key],
293
- ctx,
294
- );
295
- }
296
- }
297
-
298
- if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
299
- refineRequiredKeysInAttributeSchema(
300
- schema.additionalProperties as AttributeSchemaLike,
301
- [...pathPrefix, "additionalProperties"],
302
- ctx,
303
- );
304
- }
305
-
306
- if (schema.items && typeof schema.items === "object") {
307
- refineRequiredKeysInAttributeSchema(
308
- schema.items as AttributeSchemaLike,
309
- [...pathPrefix, "items"],
310
- ctx,
311
- );
312
- }
313
-
314
- if (schema.oneOf && Array.isArray(schema.oneOf)) {
315
- schema.oneOf.forEach((branch, index) => {
316
- if (branch && typeof branch === "object") {
317
- refineRequiredKeysInAttributeSchema(
318
- branch as AttributeSchemaLike,
319
- [...pathPrefix, "oneOf", index],
320
- ctx,
321
- );
322
- }
323
- });
324
- }
325
- }
326
-
327
- function refineNoNestedObjectProperties(
328
- schema: AttributeSchemaLike,
329
- pathPrefix: (string | number)[],
330
- ctx: z.RefinementCtx,
331
- ): void {
332
- if (!schema || typeof schema !== "object") return;
333
-
334
- const schemaAllowsObject = (candidate: AttributeSchemaLike | undefined): boolean => {
335
- if (!candidate || typeof candidate !== "object") {
336
- return false;
337
- }
338
-
339
- if (candidate.type === "object") {
340
- return true;
341
- }
342
-
343
- if (candidate.oneOf && Array.isArray(candidate.oneOf)) {
344
- return candidate.oneOf.some((branch) =>
345
- schemaAllowsObject(branch as AttributeSchemaLike | undefined),
346
- );
347
- }
348
-
349
- return false;
350
- };
351
-
352
- if (schema.type === "object" && schema.properties && typeof schema.properties === "object") {
353
- for (const key of Object.keys(schema.properties)) {
354
- const property = schema.properties[key] as AttributeSchemaLike;
355
- if (schemaAllowsObject(property)) {
356
- ctx.addIssue({
357
- code: z.ZodIssueCode.custom,
358
- message: `Object attributes must stay flat. Property "${key}" cannot be of type "object".`,
359
- path: [...pathPrefix, "properties", key, "type"],
360
- });
361
- }
362
- }
363
- }
364
-
365
- if (
366
- schema.type === "object" &&
367
- schema.additionalProperties &&
368
- schemaAllowsObject(schema.additionalProperties as AttributeSchemaLike)
369
- ) {
370
- ctx.addIssue({
371
- code: z.ZodIssueCode.custom,
372
- message:
373
- 'Object attributes must stay flat. `additionalProperties` cannot allow values of type "object".',
374
- path: [...pathPrefix, "additionalProperties", "type"],
375
- });
376
- }
377
-
378
- if (schema.items && typeof schema.items === "object") {
379
- refineNoNestedObjectProperties(
380
- schema.items as AttributeSchemaLike,
381
- [...pathPrefix, "items"],
382
- ctx,
383
- );
384
- }
385
-
386
- if (schema.properties && typeof schema.properties === "object") {
387
- for (const key of Object.keys(schema.properties)) {
388
- refineNoNestedObjectProperties(
389
- schema.properties[key] as AttributeSchemaLike,
390
- [...pathPrefix, "properties", key],
391
- ctx,
392
- );
393
- }
394
- }
395
-
396
- if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
397
- refineNoNestedObjectProperties(
398
- schema.additionalProperties as AttributeSchemaLike,
399
- [...pathPrefix, "additionalProperties"],
400
- ctx,
401
- );
402
- }
403
-
404
- if (schema.oneOf && Array.isArray(schema.oneOf)) {
405
- schema.oneOf.forEach((branch, index) => {
406
- if (branch && typeof branch === "object") {
407
- refineNoNestedObjectProperties(
408
- branch as AttributeSchemaLike,
409
- [...pathPrefix, "oneOf", index],
410
- ctx,
411
- );
412
- }
413
- });
414
- }
415
- }
416
-
417
- function refineArrayAttributesAreStringArrays(
418
- schema: AttributeSchemaLike,
419
- pathPrefix: (string | number)[],
420
- ctx: z.RefinementCtx,
421
- ): void {
422
- if (!schema || typeof schema !== "object") return;
423
-
424
- if (schema.type === "array") {
425
- if (schema.items && typeof schema.items === "object") {
426
- const itemSchema = schema.items as AttributeSchemaLike;
427
- if (itemSchema.type !== "string") {
428
- ctx.addIssue({
429
- code: z.ZodIssueCode.custom,
430
- message: `Attribute arrays must contain strings only. \`items.type\` must be "string".`,
431
- path: [...pathPrefix, "items", "type"],
432
- });
433
- }
434
- }
435
-
436
- if (schema.const !== undefined && !isArrayOfStrings(schema.const)) {
437
- ctx.addIssue({
438
- code: z.ZodIssueCode.custom,
439
- message: `Attribute arrays must contain strings only. \`const\` must be an array of strings.`,
440
- path: [...pathPrefix, "const"],
441
- });
442
- }
443
-
444
- if (Array.isArray(schema.enum)) {
445
- schema.enum.forEach((entry, index) => {
446
- if (!isArrayOfStrings(entry)) {
447
- ctx.addIssue({
448
- code: z.ZodIssueCode.custom,
449
- message: `Attribute arrays must contain strings only. Enum value at index ${index} must be an array of strings.`,
450
- path: [...pathPrefix, "enum", index],
451
- });
452
- }
453
- });
454
- }
455
- }
456
-
457
- if (schema.items && typeof schema.items === "object") {
458
- refineArrayAttributesAreStringArrays(
459
- schema.items as AttributeSchemaLike,
460
- [...pathPrefix, "items"],
461
- ctx,
462
- );
463
- }
464
-
465
- if (schema.properties && typeof schema.properties === "object") {
466
- for (const key of Object.keys(schema.properties)) {
467
- refineArrayAttributesAreStringArrays(
468
- schema.properties[key] as AttributeSchemaLike,
469
- [...pathPrefix, "properties", key],
470
- ctx,
471
- );
472
- }
473
- }
474
-
475
- if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
476
- refineArrayAttributesAreStringArrays(
477
- schema.additionalProperties as AttributeSchemaLike,
478
- [...pathPrefix, "additionalProperties"],
479
- ctx,
480
- );
481
- }
482
-
483
- if (schema.oneOf && Array.isArray(schema.oneOf)) {
484
- schema.oneOf.forEach((branch, index) => {
485
- if (branch && typeof branch === "object") {
486
- refineArrayAttributesAreStringArrays(
487
- branch as AttributeSchemaLike,
488
- [...pathPrefix, "oneOf", index],
489
- ctx,
490
- );
491
- }
492
- });
493
- }
494
- }
495
-
496
- export function getAttributeZodSchema() {
497
- const attributePropertySchema: z.ZodType<AttributeProperty> = z.lazy(() =>
498
- z
499
- .object({
500
- description: z.string().optional(),
501
- type: attributeTypeEnum.optional(),
502
- enum: z.array(valueZodSchema).optional(),
503
- const: valueZodSchema.optional(),
504
- minimum: z.number().optional(),
505
- maximum: z.number().optional(),
506
- minLength: z.number().optional(),
507
- maxLength: z.number().optional(),
508
- pattern: z.string().optional(),
509
- items: attributePropertySchema.optional(),
510
- minItems: z.number().optional(),
511
- maxItems: z.number().optional(),
512
- uniqueItems: z.boolean().optional(),
513
- required: z.array(z.string()).optional(),
514
- properties: z.record(z.string(), attributePropertySchema).optional(),
515
- additionalProperties: attributePropertySchema.optional(),
516
- schema: z.string().optional(),
517
- oneOf: z.array(attributePropertySchema).min(2).optional(),
518
- })
519
- .strict()
520
- .superRefine((data, ctx) => {
521
- const hasStructuredArrayDetails =
522
- data.items !== undefined ||
523
- data.minItems !== undefined ||
524
- data.maxItems !== undefined ||
525
- data.uniqueItems !== undefined ||
526
- data.oneOf !== undefined ||
527
- data.enum !== undefined ||
528
- data.const !== undefined;
529
-
530
- if (data.type === "array" && hasStructuredArrayDetails && data.items === undefined) {
531
- ctx.addIssue({
532
- code: z.ZodIssueCode.custom,
533
- message:
534
- 'When `type` is "array" and additional schema properties are provided, `items` is required.',
535
- path: ["items"],
536
- });
537
- }
538
- })
539
- .superRefine((data, ctx) => refineAttributeEnumMatchesType(data, [], ctx))
540
- .superRefine((data, ctx) =>
541
- refineMinimumMaximum(data as Parameters<typeof refineMinimumMaximum>[0], [], ctx),
542
- )
543
- .superRefine((data, ctx) => refineAttributeStringLengthPattern(data, [], ctx))
544
- .superRefine((data, ctx) =>
545
- refineArrayItems(data as Parameters<typeof refineArrayItems>[0], [], ctx),
546
- )
547
- .superRefine((data, ctx) => refineRequiredKeysInAttributeSchema(data, [], ctx))
548
- .superRefine((data, ctx) => refineNoNestedObjectProperties(data, [], ctx))
549
- .superRefine((data, ctx) => refineArrayAttributesAreStringArrays(data, [], ctx)),
550
- );
551
-
552
- const attributeZodSchema = z
553
- .object({
554
- archived: z.boolean().optional(),
555
- promotable: z.boolean().optional(),
556
- type: attributeTypeEnum.optional(),
557
- description: z.string(),
558
- enum: z.array(valueZodSchema).optional(),
559
- const: valueZodSchema.optional(),
560
- minimum: z.number().optional(),
561
- maximum: z.number().optional(),
562
- minLength: z.number().optional(),
563
- maxLength: z.number().optional(),
564
- pattern: z.string().optional(),
565
- items: attributePropertySchema.optional(),
566
- minItems: z.number().optional(),
567
- maxItems: z.number().optional(),
568
- uniqueItems: z.boolean().optional(),
569
- required: z.array(z.string()).optional(),
570
- properties: z.record(z.string(), attributePropertySchema).optional(),
571
- additionalProperties: attributePropertySchema.optional(),
572
- oneOf: z.array(attributePropertySchema).min(2).optional(),
573
- })
574
- .strict()
575
- .superRefine((data, ctx) => {
576
- const hasOneOf = data.oneOf !== undefined && data.oneOf.length > 0;
577
-
578
- if (!data.type && !hasOneOf) {
579
- ctx.addIssue({
580
- code: z.ZodIssueCode.custom,
581
- message: "Attribute must have either `type` or `oneOf` at the top level.",
582
- path: ["type"],
583
- });
584
- }
585
-
586
- if (data.type && hasOneOf) {
587
- ctx.addIssue({
588
- code: z.ZodIssueCode.custom,
589
- message:
590
- "Attribute cannot have both `type` and `oneOf` at the top level. Use one or the other.",
591
- path: ["type"],
592
- });
593
- }
594
-
595
- const hasStructuredArrayDetails =
596
- data.items !== undefined ||
597
- data.minItems !== undefined ||
598
- data.maxItems !== undefined ||
599
- data.uniqueItems !== undefined ||
600
- data.oneOf !== undefined ||
601
- data.enum !== undefined ||
602
- data.const !== undefined;
603
-
604
- if (data.type === "array" && hasStructuredArrayDetails && data.items === undefined) {
605
- ctx.addIssue({
606
- code: z.ZodIssueCode.custom,
607
- message:
608
- 'When `type` is "array" and additional schema properties are provided, `items` is required.',
609
- path: ["items"],
610
- });
611
- }
612
- })
613
- .superRefine((data, ctx) => refineAttributeEnumMatchesType(data, [], ctx))
614
- .superRefine((data, ctx) =>
615
- refineMinimumMaximum(data as Parameters<typeof refineMinimumMaximum>[0], [], ctx),
616
- )
617
- .superRefine((data, ctx) => refineAttributeStringLengthPattern(data, [], ctx))
618
- .superRefine((data, ctx) =>
619
- refineArrayItems(data as Parameters<typeof refineArrayItems>[0], [], ctx),
620
- )
621
- .superRefine((data, ctx) => refineRequiredKeysInAttributeSchema(data, [], ctx))
622
- .superRefine((data, ctx) => refineNoNestedObjectProperties(data, [], ctx))
623
- .superRefine((data, ctx) => refineArrayAttributesAreStringArrays(data, [], ctx));
624
-
625
- return attributeZodSchema;
626
- }
@@ -1,45 +0,0 @@
1
- import type { FeatureKey, Required } from "@featurevisor/types";
2
-
3
- import { Datasource } from "../datasource";
4
-
5
- export async function checkForCircularDependencyInRequired(
6
- datasource: Datasource,
7
- featureKey: FeatureKey,
8
- required?: Required[],
9
- chain: FeatureKey[] = [],
10
- ) {
11
- if (!required) {
12
- return;
13
- }
14
-
15
- const requiredKeys = required.map((r) => (typeof r === "string" ? r : r.key));
16
-
17
- if (requiredKeys.length === 0) {
18
- return;
19
- }
20
-
21
- for (const requiredKey of requiredKeys) {
22
- chain.push(requiredKey);
23
-
24
- if (chain.indexOf(featureKey) > -1) {
25
- throw new Error(`circular dependency found: ${chain.join(" -> ")}`);
26
- }
27
-
28
- const requiredFeatureExists = await datasource.featureExists(featureKey);
29
-
30
- if (!requiredFeatureExists) {
31
- throw new Error(`required feature "${requiredKey}" not found`);
32
- }
33
-
34
- const requiredParsedFeature = await datasource.readFeature(requiredKey);
35
-
36
- if (requiredParsedFeature.required) {
37
- await checkForCircularDependencyInRequired(
38
- datasource,
39
- featureKey,
40
- requiredParsedFeature.required,
41
- chain,
42
- );
43
- }
44
- }
45
- }