@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.
- package/lib/datasource/datasource.d.ts +1 -1
- package/lib/datasource/datasource.js +4 -14
- package/lib/datasource/datasource.js.map +1 -1
- package/lib/datasource/requiredFeatures.d.ts +7 -0
- package/lib/datasource/requiredFeatures.js +38 -0
- package/lib/datasource/requiredFeatures.js.map +1 -0
- package/lib/linter/checkCircularDependency.d.ts +1 -1
- package/lib/linter/checkCircularDependency.js +3 -22
- package/lib/linter/checkCircularDependency.js.map +1 -1
- package/lib/linter/conditionSchema.js +27 -3
- package/lib/linter/conditionSchema.js.map +1 -1
- package/lib/tester/testFeature.js +17 -16
- package/lib/tester/testFeature.js.map +1 -1
- package/package.json +10 -7
- package/CHANGELOG.md +0 -2246
- package/coverage/clover.xml +0 -5456
- package/coverage/coverage-final.json +0 -64
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -371
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -210
- package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
- package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
- package/coverage/lcov-report/src/benchmark/index.html +0 -116
- package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
- package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
- package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
- package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
- package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
- package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
- package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
- package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
- package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
- package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
- package/coverage/lcov-report/src/builder/index.html +0 -311
- package/coverage/lcov-report/src/builder/index.ts.html +0 -91
- package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
- package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
- package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
- package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
- package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
- package/coverage/lcov-report/src/cli/index.html +0 -131
- package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
- package/coverage/lcov-report/src/config/index.html +0 -131
- package/coverage/lcov-report/src/config/index.ts.html +0 -88
- package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
- package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
- package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
- package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
- package/coverage/lcov-report/src/datasource/index.html +0 -161
- package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
- package/coverage/lcov-report/src/evaluate/index.html +0 -116
- package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
- package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
- package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
- package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
- package/coverage/lcov-report/src/find-usage/index.html +0 -116
- package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
- package/coverage/lcov-report/src/generate-code/index.html +0 -131
- package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
- package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
- package/coverage/lcov-report/src/index.html +0 -131
- package/coverage/lcov-report/src/info/index.html +0 -116
- package/coverage/lcov-report/src/info/index.ts.html +0 -472
- package/coverage/lcov-report/src/init/index.html +0 -116
- package/coverage/lcov-report/src/init/index.ts.html +0 -280
- package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
- package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
- package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
- package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
- package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
- package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
- package/coverage/lcov-report/src/linter/index.html +0 -326
- package/coverage/lcov-report/src/linter/index.ts.html +0 -88
- package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
- package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
- package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
- package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
- package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
- package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
- package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
- package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
- package/coverage/lcov-report/src/list/index.html +0 -131
- package/coverage/lcov-report/src/list/index.ts.html +0 -2065
- package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
- package/coverage/lcov-report/src/promoter/index.html +0 -116
- package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
- package/coverage/lcov-report/src/sets.ts.html +0 -313
- package/coverage/lcov-report/src/targeting.ts.html +0 -403
- package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
- package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
- package/coverage/lcov-report/src/tester/index.html +0 -206
- package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
- package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
- package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
- package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
- package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
- package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
- package/coverage/lcov-report/src/utils/git.ts.html +0 -559
- package/coverage/lcov-report/src/utils/index.html +0 -161
- package/coverage/lcov-report/src/utils/index.ts.html +0 -94
- package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
- package/coverage/lcov.info +0 -11615
- package/jest.config.js +0 -22
- package/json-schema/README.md +0 -5
- package/json-schema/attribute.json +0 -40
- package/json-schema/feature.json +0 -394
- package/json-schema/segment.json +0 -137
- package/src/assess-distribution/index.ts +0 -223
- package/src/benchmark/index.ts +0 -236
- package/src/builder/allocator.spec.ts +0 -105
- package/src/builder/allocator.ts +0 -45
- package/src/builder/applyContextToConditions.spec.ts +0 -2677
- package/src/builder/applyContextToConditions.ts +0 -139
- package/src/builder/applyContextToDatafile.spec.ts +0 -2626
- package/src/builder/applyContextToDatafile.ts +0 -229
- package/src/builder/applyContextToSegments.spec.ts +0 -1593
- package/src/builder/applyContextToSegments.ts +0 -165
- package/src/builder/buildDatafile.spec.ts +0 -445
- package/src/builder/buildDatafile.ts +0 -656
- package/src/builder/buildProject.ts +0 -344
- package/src/builder/buildRuntimeDatafiles.ts +0 -57
- package/src/builder/getFeatureRanges.ts +0 -61
- package/src/builder/hashes.ts +0 -109
- package/src/builder/index.ts +0 -2
- package/src/builder/mutateVariables.spec.ts +0 -1179
- package/src/builder/mutateVariables.ts +0 -112
- package/src/builder/mutator.spec.ts +0 -413
- package/src/builder/mutator.ts +0 -237
- package/src/builder/revision.spec.ts +0 -22
- package/src/builder/revision.ts +0 -22
- package/src/builder/traffic.spec.ts +0 -946
- package/src/builder/traffic.ts +0 -192
- package/src/cli/cli.ts +0 -105
- package/src/cli/index.ts +0 -2
- package/src/cli/plugins.ts +0 -43
- package/src/config/index.ts +0 -1
- package/src/config/projectConfig.spec.ts +0 -194
- package/src/config/projectConfig.ts +0 -280
- package/src/datasource/adapter.ts +0 -56
- package/src/datasource/datasource.ts +0 -305
- package/src/datasource/filesystemAdapter.spec.ts +0 -212
- package/src/datasource/filesystemAdapter.ts +0 -497
- package/src/datasource/index.ts +0 -3
- package/src/dependencies.ts +0 -13
- package/src/evaluate/index.ts +0 -243
- package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
- package/src/find-duplicate-segments/index.ts +0 -74
- package/src/find-usage/index.ts +0 -565
- package/src/generate-code/index.ts +0 -106
- package/src/generate-code/typescript.spec.ts +0 -214
- package/src/generate-code/typescript.ts +0 -608
- package/src/index.ts +0 -20
- package/src/info/index.ts +0 -129
- package/src/init/index.ts +0 -65
- package/src/linter/attributeSchema.spec.ts +0 -342
- package/src/linter/attributeSchema.ts +0 -626
- package/src/linter/checkCircularDependency.ts +0 -45
- package/src/linter/checkPercentageExceedingSlot.ts +0 -61
- package/src/linter/conditionSchema.spec.ts +0 -510
- package/src/linter/conditionSchema.ts +0 -713
- package/src/linter/featureSchema.spec.ts +0 -1985
- package/src/linter/featureSchema.ts +0 -1922
- package/src/linter/groupSchema.ts +0 -47
- package/src/linter/index.ts +0 -1
- package/src/linter/lintProject.spec.ts +0 -336
- package/src/linter/lintProject.ts +0 -873
- package/src/linter/mutationNotation.spec.ts +0 -704
- package/src/linter/mutationNotation.ts +0 -442
- package/src/linter/printError.ts +0 -68
- package/src/linter/schema.spec.ts +0 -690
- package/src/linter/schema.ts +0 -491
- package/src/linter/segmentSchema.spec.ts +0 -358
- package/src/linter/segmentSchema.ts +0 -18
- package/src/linter/targetSchema.spec.ts +0 -159
- package/src/linter/targetSchema.ts +0 -46
- package/src/linter/testSchema.spec.ts +0 -258
- package/src/linter/testSchema.ts +0 -148
- package/src/linter/zodHelpers.ts +0 -16
- package/src/list/index.spec.ts +0 -135
- package/src/list/index.ts +0 -660
- package/src/list/matrix.spec.ts +0 -49
- package/src/list/matrix.ts +0 -212
- package/src/promoter/index.spec.ts +0 -617
- package/src/promoter/index.ts +0 -1266
- package/src/sets.ts +0 -76
- package/src/targeting.spec.ts +0 -74
- package/src/targeting.ts +0 -106
- package/src/tester/cliFormat.ts +0 -16
- package/src/tester/helpers.spec.ts +0 -149
- package/src/tester/helpers.ts +0 -76
- package/src/tester/index.ts +0 -2
- package/src/tester/prettyDuration.ts +0 -34
- package/src/tester/printTestResult.ts +0 -84
- package/src/tester/testFeature.ts +0 -367
- package/src/tester/testProject.spec.ts +0 -50
- package/src/tester/testProject.ts +0 -311
- package/src/tester/testSegment.ts +0 -77
- package/src/utils/extractKeys.ts +0 -136
- package/src/utils/git.ts +0 -158
- package/src/utils/index.ts +0 -3
- package/src/utils/pretty.ts +0 -12
- package/tsconfig.cjs.json +0 -13
- package/tsconfig.typecheck.json +0 -9
package/src/linter/schema.ts
DELETED
|
@@ -1,491 +0,0 @@
|
|
|
1
|
-
import type { Schema, SchemaKey, Value } from "@featurevisor/types";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
import { refineWithMessage } from "./zodHelpers";
|
|
5
|
-
|
|
6
|
-
/** Returns true if value matches the schema type. */
|
|
7
|
-
function valueMatchesType(v: unknown, type: string): boolean {
|
|
8
|
-
switch (type) {
|
|
9
|
-
case "string":
|
|
10
|
-
return typeof v === "string";
|
|
11
|
-
case "boolean":
|
|
12
|
-
return typeof v === "boolean";
|
|
13
|
-
case "integer":
|
|
14
|
-
return typeof v === "number" && Number.isInteger(v);
|
|
15
|
-
case "double":
|
|
16
|
-
return typeof v === "number";
|
|
17
|
-
case "object":
|
|
18
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
19
|
-
case "array":
|
|
20
|
-
return Array.isArray(v);
|
|
21
|
-
default:
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
type SchemaLike = {
|
|
27
|
-
type?: string;
|
|
28
|
-
enum?: unknown[];
|
|
29
|
-
const?: unknown;
|
|
30
|
-
minimum?: number;
|
|
31
|
-
maximum?: number;
|
|
32
|
-
minLength?: number;
|
|
33
|
-
maxLength?: number;
|
|
34
|
-
pattern?: string;
|
|
35
|
-
minItems?: number;
|
|
36
|
-
maxItems?: number;
|
|
37
|
-
uniqueItems?: boolean;
|
|
38
|
-
items?: unknown;
|
|
39
|
-
properties?: Record<string, unknown>;
|
|
40
|
-
additionalProperties?: unknown;
|
|
41
|
-
oneOf?: unknown[];
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Recursively validates that when a schema has both `type` and `enum`, every enum value matches the type.
|
|
46
|
-
* Also recurses into oneOf branches.
|
|
47
|
-
*/
|
|
48
|
-
export function refineEnumMatchesType(
|
|
49
|
-
schema: SchemaLike,
|
|
50
|
-
pathPrefix: (string | number)[],
|
|
51
|
-
ctx: z.RefinementCtx,
|
|
52
|
-
): void {
|
|
53
|
-
if (!schema || typeof schema !== "object") return;
|
|
54
|
-
const type = schema.type;
|
|
55
|
-
const enumArr = schema.enum;
|
|
56
|
-
if (type && Array.isArray(enumArr) && enumArr.length > 0) {
|
|
57
|
-
for (let i = 0; i < enumArr.length; i++) {
|
|
58
|
-
const v = enumArr[i];
|
|
59
|
-
if (!valueMatchesType(v, type)) {
|
|
60
|
-
ctx.addIssue({
|
|
61
|
-
code: z.ZodIssueCode.custom,
|
|
62
|
-
message: `Enum value at index ${i} (${JSON.stringify(v)}) does not match type "${type}". All enum values must be of the same type as \`type\`.`,
|
|
63
|
-
path: [...pathPrefix, "enum", i],
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
if (schema.items && typeof schema.items === "object") {
|
|
69
|
-
refineEnumMatchesType(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
|
|
70
|
-
}
|
|
71
|
-
if (schema.properties && typeof schema.properties === "object") {
|
|
72
|
-
for (const k of Object.keys(schema.properties)) {
|
|
73
|
-
refineEnumMatchesType(
|
|
74
|
-
schema.properties[k] as SchemaLike,
|
|
75
|
-
[...pathPrefix, "properties", k],
|
|
76
|
-
ctx,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
81
|
-
refineEnumMatchesType(
|
|
82
|
-
schema.additionalProperties as SchemaLike,
|
|
83
|
-
[...pathPrefix, "additionalProperties"],
|
|
84
|
-
ctx,
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
if (schema.oneOf && Array.isArray(schema.oneOf)) {
|
|
88
|
-
schema.oneOf.forEach((branch, i) => {
|
|
89
|
-
if (branch && typeof branch === "object") {
|
|
90
|
-
refineEnumMatchesType(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Validates that when a schema has type "integer" or "double", minimum <= maximum when both set,
|
|
98
|
-
* and const/enum values (if present) fall within the range.
|
|
99
|
-
*/
|
|
100
|
-
export function refineMinimumMaximum(
|
|
101
|
-
schema: SchemaLike,
|
|
102
|
-
pathPrefix: (string | number)[],
|
|
103
|
-
ctx: z.RefinementCtx,
|
|
104
|
-
): void {
|
|
105
|
-
if (!schema || typeof schema !== "object") return;
|
|
106
|
-
const type = schema.type;
|
|
107
|
-
const min = schema.minimum;
|
|
108
|
-
const max = schema.maximum;
|
|
109
|
-
const isNumeric = type === "integer" || type === "double";
|
|
110
|
-
if (isNumeric && min !== undefined && max !== undefined && min > max) {
|
|
111
|
-
ctx.addIssue({
|
|
112
|
-
code: z.ZodIssueCode.custom,
|
|
113
|
-
message: `When \`type\` is "${type}", \`minimum\` (${min}) must be less than or equal to \`maximum\` (${max}).`,
|
|
114
|
-
path: [...pathPrefix, "minimum"],
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
if (isNumeric && min !== undefined && schema.const !== undefined) {
|
|
118
|
-
const v = schema.const;
|
|
119
|
-
if (typeof v === "number" && v < min) {
|
|
120
|
-
ctx.addIssue({
|
|
121
|
-
code: z.ZodIssueCode.custom,
|
|
122
|
-
message: `\`const\` value ${v} is less than \`minimum\` (${min}).`,
|
|
123
|
-
path: [...pathPrefix, "const"],
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (isNumeric && max !== undefined && schema.const !== undefined) {
|
|
128
|
-
const v = schema.const;
|
|
129
|
-
if (typeof v === "number" && v > max) {
|
|
130
|
-
ctx.addIssue({
|
|
131
|
-
code: z.ZodIssueCode.custom,
|
|
132
|
-
message: `\`const\` value ${v} is greater than \`maximum\` (${max}).`,
|
|
133
|
-
path: [...pathPrefix, "const"],
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (isNumeric && Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
138
|
-
for (let i = 0; i < schema.enum.length; i++) {
|
|
139
|
-
const v = schema.enum[i];
|
|
140
|
-
if (typeof v === "number") {
|
|
141
|
-
if (min !== undefined && v < min) {
|
|
142
|
-
ctx.addIssue({
|
|
143
|
-
code: z.ZodIssueCode.custom,
|
|
144
|
-
message: `Enum value at index ${i} (${v}) is less than \`minimum\` (${min}).`,
|
|
145
|
-
path: [...pathPrefix, "enum", i],
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
if (max !== undefined && v > max) {
|
|
149
|
-
ctx.addIssue({
|
|
150
|
-
code: z.ZodIssueCode.custom,
|
|
151
|
-
message: `Enum value at index ${i} (${v}) is greater than \`maximum\` (${max}).`,
|
|
152
|
-
path: [...pathPrefix, "enum", i],
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
if (schema.items && typeof schema.items === "object") {
|
|
159
|
-
refineMinimumMaximum(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
|
|
160
|
-
}
|
|
161
|
-
if (schema.properties && typeof schema.properties === "object") {
|
|
162
|
-
for (const k of Object.keys(schema.properties)) {
|
|
163
|
-
refineMinimumMaximum(
|
|
164
|
-
schema.properties[k] as SchemaLike,
|
|
165
|
-
[...pathPrefix, "properties", k],
|
|
166
|
-
ctx,
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
171
|
-
refineMinimumMaximum(
|
|
172
|
-
schema.additionalProperties as SchemaLike,
|
|
173
|
-
[...pathPrefix, "additionalProperties"],
|
|
174
|
-
ctx,
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
if (schema.oneOf && Array.isArray(schema.oneOf)) {
|
|
178
|
-
schema.oneOf.forEach((branch, i) => {
|
|
179
|
-
if (branch && typeof branch === "object") {
|
|
180
|
-
refineMinimumMaximum(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Validates that when a schema has type "string", minLength <= maxLength when both set,
|
|
188
|
-
* pattern is a valid RegExp (if set), and const/enum string values satisfy length and pattern.
|
|
189
|
-
*/
|
|
190
|
-
export function refineStringLengthPattern(
|
|
191
|
-
schema: SchemaLike,
|
|
192
|
-
pathPrefix: (string | number)[],
|
|
193
|
-
ctx: z.RefinementCtx,
|
|
194
|
-
): void {
|
|
195
|
-
if (!schema || typeof schema !== "object") return;
|
|
196
|
-
const type = schema.type;
|
|
197
|
-
const minLen = schema.minLength;
|
|
198
|
-
const maxLen = schema.maxLength;
|
|
199
|
-
const patternStr = schema.pattern;
|
|
200
|
-
const isString = type === "string";
|
|
201
|
-
if (isString && minLen !== undefined && maxLen !== undefined && minLen > maxLen) {
|
|
202
|
-
ctx.addIssue({
|
|
203
|
-
code: z.ZodIssueCode.custom,
|
|
204
|
-
message: `When \`type\` is "string", \`minLength\` (${minLen}) must be less than or equal to \`maxLength\` (${maxLen}).`,
|
|
205
|
-
path: [...pathPrefix, "minLength"],
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
if (isString && patternStr !== undefined) {
|
|
209
|
-
try {
|
|
210
|
-
new RegExp(patternStr);
|
|
211
|
-
} catch {
|
|
212
|
-
ctx.addIssue({
|
|
213
|
-
code: z.ZodIssueCode.custom,
|
|
214
|
-
message: `\`pattern\` must be a valid ECMA-262 regular expression; "${patternStr}" is invalid.`,
|
|
215
|
-
path: [...pathPrefix, "pattern"],
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
const testString = (s: string): { minOk: boolean; maxOk: boolean; patternOk: boolean } => {
|
|
220
|
-
const minOk = minLen === undefined || s.length >= minLen;
|
|
221
|
-
const maxOk = maxLen === undefined || s.length <= maxLen;
|
|
222
|
-
let patternOk = true;
|
|
223
|
-
if (patternStr !== undefined) {
|
|
224
|
-
try {
|
|
225
|
-
patternOk = new RegExp(patternStr).test(s);
|
|
226
|
-
} catch {
|
|
227
|
-
patternOk = true;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return { minOk, maxOk, patternOk };
|
|
231
|
-
};
|
|
232
|
-
if (isString && schema.const !== undefined && typeof schema.const === "string") {
|
|
233
|
-
const { minOk, maxOk, patternOk } = testString(schema.const);
|
|
234
|
-
if (!minOk) {
|
|
235
|
-
ctx.addIssue({
|
|
236
|
-
code: z.ZodIssueCode.custom,
|
|
237
|
-
message: `\`const\` value length (${schema.const.length}) is less than \`minLength\` (${minLen}).`,
|
|
238
|
-
path: [...pathPrefix, "const"],
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
if (!maxOk) {
|
|
242
|
-
ctx.addIssue({
|
|
243
|
-
code: z.ZodIssueCode.custom,
|
|
244
|
-
message: `\`const\` value length (${schema.const.length}) is greater than \`maxLength\` (${maxLen}).`,
|
|
245
|
-
path: [...pathPrefix, "const"],
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
if (!patternOk) {
|
|
249
|
-
ctx.addIssue({
|
|
250
|
-
code: z.ZodIssueCode.custom,
|
|
251
|
-
message: `\`const\` value does not match \`pattern\`.`,
|
|
252
|
-
path: [...pathPrefix, "const"],
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
if (isString && Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
257
|
-
for (let i = 0; i < schema.enum.length; i++) {
|
|
258
|
-
const v = schema.enum[i];
|
|
259
|
-
if (typeof v === "string") {
|
|
260
|
-
const { minOk, maxOk, patternOk } = testString(v);
|
|
261
|
-
if (!minOk) {
|
|
262
|
-
ctx.addIssue({
|
|
263
|
-
code: z.ZodIssueCode.custom,
|
|
264
|
-
message: `Enum value at index ${i} length (${v.length}) is less than \`minLength\` (${minLen}).`,
|
|
265
|
-
path: [...pathPrefix, "enum", i],
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
if (!maxOk) {
|
|
269
|
-
ctx.addIssue({
|
|
270
|
-
code: z.ZodIssueCode.custom,
|
|
271
|
-
message: `Enum value at index ${i} length (${v.length}) is greater than \`maxLength\` (${maxLen}).`,
|
|
272
|
-
path: [...pathPrefix, "enum", i],
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
if (!patternOk) {
|
|
276
|
-
ctx.addIssue({
|
|
277
|
-
code: z.ZodIssueCode.custom,
|
|
278
|
-
message: `Enum value at index ${i} does not match \`pattern\`.`,
|
|
279
|
-
path: [...pathPrefix, "enum", i],
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
if (schema.items && typeof schema.items === "object") {
|
|
286
|
-
refineStringLengthPattern(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
|
|
287
|
-
}
|
|
288
|
-
if (schema.properties && typeof schema.properties === "object") {
|
|
289
|
-
for (const k of Object.keys(schema.properties)) {
|
|
290
|
-
refineStringLengthPattern(
|
|
291
|
-
schema.properties[k] as SchemaLike,
|
|
292
|
-
[...pathPrefix, "properties", k],
|
|
293
|
-
ctx,
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
298
|
-
refineStringLengthPattern(
|
|
299
|
-
schema.additionalProperties as SchemaLike,
|
|
300
|
-
[...pathPrefix, "additionalProperties"],
|
|
301
|
-
ctx,
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
|
-
if (schema.oneOf && Array.isArray(schema.oneOf)) {
|
|
305
|
-
schema.oneOf.forEach((branch, i) => {
|
|
306
|
-
if (branch && typeof branch === "object") {
|
|
307
|
-
refineStringLengthPattern(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/** Deep equality for primitive/array/object values (used for uniqueItems check). */
|
|
314
|
-
function valueDeepEqualForRefine(a: unknown, b: unknown): boolean {
|
|
315
|
-
if (a === b) return true;
|
|
316
|
-
if (typeof a !== typeof b) return false;
|
|
317
|
-
if (a === null || b === null) return a === b;
|
|
318
|
-
if (typeof a === "object" && typeof b === "object") {
|
|
319
|
-
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
320
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
321
|
-
if (a.length !== b.length) return false;
|
|
322
|
-
return a.every((v, i) => valueDeepEqualForRefine(v, b[i]));
|
|
323
|
-
}
|
|
324
|
-
const keysA = Object.keys(a as object).sort();
|
|
325
|
-
const keysB = Object.keys(b as object).sort();
|
|
326
|
-
if (keysA.length !== keysB.length || keysA.some((k, i) => k !== keysB[i])) return false;
|
|
327
|
-
return keysA.every((k) =>
|
|
328
|
-
valueDeepEqualForRefine((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k]),
|
|
329
|
-
);
|
|
330
|
-
}
|
|
331
|
-
return false;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Validates that when a schema has type "array", minItems <= maxItems when both set,
|
|
336
|
-
* and const/enum array values (if present) satisfy length and uniqueItems.
|
|
337
|
-
*/
|
|
338
|
-
export function refineArrayItems(
|
|
339
|
-
schema: SchemaLike,
|
|
340
|
-
pathPrefix: (string | number)[],
|
|
341
|
-
ctx: z.RefinementCtx,
|
|
342
|
-
): void {
|
|
343
|
-
if (!schema || typeof schema !== "object") return;
|
|
344
|
-
const type = schema.type;
|
|
345
|
-
const minItems = schema.minItems;
|
|
346
|
-
const maxItems = schema.maxItems;
|
|
347
|
-
const uniqueItems = schema.uniqueItems;
|
|
348
|
-
const isArray = type === "array";
|
|
349
|
-
if (isArray && minItems !== undefined && maxItems !== undefined && minItems > maxItems) {
|
|
350
|
-
ctx.addIssue({
|
|
351
|
-
code: z.ZodIssueCode.custom,
|
|
352
|
-
message: `When \`type\` is "array", \`minItems\` (${minItems}) must be less than or equal to \`maxItems\` (${maxItems}).`,
|
|
353
|
-
path: [...pathPrefix, "minItems"],
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
const checkArray = (arr: unknown[], pathSuffix: (string | number)[]) => {
|
|
357
|
-
if (minItems !== undefined && arr.length < minItems) {
|
|
358
|
-
ctx.addIssue({
|
|
359
|
-
code: z.ZodIssueCode.custom,
|
|
360
|
-
message: `Array length (${arr.length}) is less than \`minItems\` (${minItems}).`,
|
|
361
|
-
path: pathSuffix,
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
if (maxItems !== undefined && arr.length > maxItems) {
|
|
365
|
-
ctx.addIssue({
|
|
366
|
-
code: z.ZodIssueCode.custom,
|
|
367
|
-
message: `Array length (${arr.length}) is greater than \`maxItems\` (${maxItems}).`,
|
|
368
|
-
path: pathSuffix,
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
if (uniqueItems) {
|
|
372
|
-
for (let i = 0; i < arr.length; i++) {
|
|
373
|
-
for (let j = i + 1; j < arr.length; j++) {
|
|
374
|
-
if (valueDeepEqualForRefine(arr[i], arr[j])) {
|
|
375
|
-
ctx.addIssue({
|
|
376
|
-
code: z.ZodIssueCode.custom,
|
|
377
|
-
message: `Array has duplicate items at indices ${i} and ${j} but \`uniqueItems\` is true.`,
|
|
378
|
-
path: pathSuffix,
|
|
379
|
-
});
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
if (isArray && Array.isArray(schema.const)) {
|
|
387
|
-
checkArray(schema.const, [...pathPrefix, "const"]);
|
|
388
|
-
}
|
|
389
|
-
if (isArray && Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
390
|
-
for (let i = 0; i < schema.enum.length; i++) {
|
|
391
|
-
const v = schema.enum[i];
|
|
392
|
-
if (Array.isArray(v)) checkArray(v, [...pathPrefix, "enum", i]);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
if (schema.items && typeof schema.items === "object") {
|
|
396
|
-
refineArrayItems(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
|
|
397
|
-
}
|
|
398
|
-
if (schema.properties && typeof schema.properties === "object") {
|
|
399
|
-
for (const k of Object.keys(schema.properties)) {
|
|
400
|
-
refineArrayItems(schema.properties[k] as SchemaLike, [...pathPrefix, "properties", k], ctx);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
404
|
-
refineArrayItems(
|
|
405
|
-
schema.additionalProperties as SchemaLike,
|
|
406
|
-
[...pathPrefix, "additionalProperties"],
|
|
407
|
-
ctx,
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
if (schema.oneOf && Array.isArray(schema.oneOf)) {
|
|
411
|
-
schema.oneOf.forEach((branch, i) => {
|
|
412
|
-
if (branch && typeof branch === "object") {
|
|
413
|
-
refineArrayItems(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
|
|
414
|
-
}
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// Recursive schema for Value: boolean | string | number | ObjectValue | Value[]
|
|
420
|
-
export const valueZodSchema: z.ZodType<Value> = z.lazy(() =>
|
|
421
|
-
z.union([
|
|
422
|
-
z.boolean(),
|
|
423
|
-
z.string(),
|
|
424
|
-
z.number(),
|
|
425
|
-
// | Date // @TODO: support in future
|
|
426
|
-
z.record(z.string(), valueZodSchema),
|
|
427
|
-
z.array(valueZodSchema),
|
|
428
|
-
]),
|
|
429
|
-
);
|
|
430
|
-
|
|
431
|
-
// @TODO: support "date" in future
|
|
432
|
-
// @TODO: consider "semver" in future
|
|
433
|
-
// @TODO: consider "url" in future
|
|
434
|
-
export const propertyTypeEnum = z.enum([
|
|
435
|
-
"boolean",
|
|
436
|
-
"string",
|
|
437
|
-
"integer",
|
|
438
|
-
"double",
|
|
439
|
-
"object",
|
|
440
|
-
"array",
|
|
441
|
-
]);
|
|
442
|
-
|
|
443
|
-
export function getSchemaZodSchema(schemaKeys: SchemaKey[] = []) {
|
|
444
|
-
const schemaZodSchema: z.ZodType<Schema> = z.lazy(() =>
|
|
445
|
-
z
|
|
446
|
-
.object({
|
|
447
|
-
description: z.string().optional(),
|
|
448
|
-
promotable: z.boolean().optional(),
|
|
449
|
-
type: propertyTypeEnum.optional(),
|
|
450
|
-
enum: z.array(valueZodSchema).optional(),
|
|
451
|
-
const: valueZodSchema.optional(),
|
|
452
|
-
minimum: z.number().optional(),
|
|
453
|
-
maximum: z.number().optional(),
|
|
454
|
-
minLength: z.number().optional(),
|
|
455
|
-
maxLength: z.number().optional(),
|
|
456
|
-
pattern: z.string().optional(),
|
|
457
|
-
items: schemaZodSchema.optional(),
|
|
458
|
-
minItems: z.number().optional(),
|
|
459
|
-
maxItems: z.number().optional(),
|
|
460
|
-
uniqueItems: z.boolean().optional(),
|
|
461
|
-
required: z.array(z.string()).optional(),
|
|
462
|
-
properties: z.record(z.string(), schemaZodSchema).optional(),
|
|
463
|
-
additionalProperties: schemaZodSchema.optional(),
|
|
464
|
-
// Annotations: default?: Value; examples?: Value[];
|
|
465
|
-
|
|
466
|
-
schema: refineWithMessage(
|
|
467
|
-
z.string(),
|
|
468
|
-
(value) => schemaKeys.includes(value),
|
|
469
|
-
(value) => `Unknown schema "${value}"`,
|
|
470
|
-
).optional(),
|
|
471
|
-
oneOf: z.array(schemaZodSchema).min(2).optional(),
|
|
472
|
-
})
|
|
473
|
-
.strict()
|
|
474
|
-
.superRefine((data, ctx) => {
|
|
475
|
-
if (data.type === "array" && data.items === undefined) {
|
|
476
|
-
ctx.addIssue({
|
|
477
|
-
code: z.ZodIssueCode.custom,
|
|
478
|
-
message:
|
|
479
|
-
'When `type` is "array", `items` is required. Define the schema for array elements.',
|
|
480
|
-
path: ["items"],
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
})
|
|
484
|
-
.superRefine((data, ctx) => refineEnumMatchesType(data, [], ctx))
|
|
485
|
-
.superRefine((data, ctx) => refineMinimumMaximum(data, [], ctx))
|
|
486
|
-
.superRefine((data, ctx) => refineStringLengthPattern(data, [], ctx))
|
|
487
|
-
.superRefine((data, ctx) => refineArrayItems(data, [], ctx)),
|
|
488
|
-
);
|
|
489
|
-
|
|
490
|
-
return schemaZodSchema;
|
|
491
|
-
}
|