@featurevisor/core 4.13.0 → 4.14.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/builder/applyContextToDatafile.js +21 -1
- package/lib/builder/applyContextToDatafile.js.map +1 -1
- package/lib/builder/buildDatafile.d.ts +15 -1
- package/lib/builder/buildDatafile.js +48 -13
- package/lib/builder/buildDatafile.js.map +1 -1
- package/lib/builder/buildProject.js +9 -0
- package/lib/builder/buildProject.js.map +1 -1
- package/lib/builder/environments.d.ts +1 -2
- package/lib/builder/hashes.d.ts +9 -0
- package/lib/builder/hashes.js +16 -0
- package/lib/builder/hashes.js.map +1 -1
- package/lib/builder/schemas.d.ts +76 -0
- package/lib/builder/schemas.js +406 -0
- package/lib/builder/schemas.js.map +1 -0
- package/lib/catalog.d.ts +2 -0
- package/lib/catalog.js +3 -0
- package/lib/catalog.js.map +1 -1
- package/lib/cli/options.js +31 -4
- package/lib/cli/options.js.map +1 -1
- package/lib/config/allowedConditions.d.ts +6 -3
- package/lib/config/allowedConditions.js +9 -2
- package/lib/config/allowedConditions.js.map +1 -1
- package/lib/convert/check.js +5 -1
- package/lib/convert/check.js.map +1 -1
- package/lib/convert/plan.js +8 -2
- package/lib/convert/plan.js.map +1 -1
- package/lib/datasource/conditionLocations.d.ts +1 -1
- package/lib/datasource/conditionLocations.js +26 -0
- package/lib/datasource/conditionLocations.js.map +1 -1
- package/lib/datasource/datasource.d.ts +2 -0
- package/lib/datasource/datasource.js +5 -3
- package/lib/datasource/datasource.js.map +1 -1
- package/lib/datasource/expressions.d.ts +1 -1
- package/lib/datasource/testSpecs.js +3 -3
- package/lib/datasource/testSpecs.js.map +1 -1
- package/lib/diff/conditions.js +1 -1
- package/lib/diff/conditions.js.map +1 -1
- package/lib/diff/index.js +68 -1
- package/lib/diff/index.js.map +1 -1
- package/lib/diff/schemaWidening.d.ts +15 -0
- package/lib/diff/schemaWidening.js +275 -0
- package/lib/diff/schemaWidening.js.map +1 -0
- package/lib/find-usage/index.d.ts +16 -2
- package/lib/find-usage/index.js +114 -10
- package/lib/find-usage/index.js.map +1 -1
- package/lib/formatter/expressions.js +5 -2
- package/lib/formatter/expressions.js.map +1 -1
- package/lib/formatter/layout.d.ts +3 -2
- package/lib/formatter/layout.js +23 -4
- package/lib/formatter/layout.js.map +1 -1
- package/lib/formatter/style.d.ts +20 -2
- package/lib/formatter/style.js +49 -4
- package/lib/formatter/style.js.map +1 -1
- package/lib/generate-code/index.d.ts +4 -0
- package/lib/generate-code/index.js +10 -0
- package/lib/generate-code/index.js.map +1 -1
- package/lib/generate-code/typescript.d.ts +5 -0
- package/lib/generate-code/typescript.js +265 -25
- package/lib/generate-code/typescript.js.map +1 -1
- package/lib/generate-code/widenMutations.d.ts +9 -0
- package/lib/generate-code/widenMutations.js +134 -0
- package/lib/generate-code/widenMutations.js.map +1 -0
- package/lib/linter/composedOverrideSchema.js +3 -0
- package/lib/linter/composedOverrideSchema.js.map +1 -1
- package/lib/linter/conditionKinds.d.ts +1 -1
- package/lib/linter/conditionKinds.js +4 -0
- package/lib/linter/conditionKinds.js.map +1 -1
- package/lib/linter/conditionSchema.d.ts +21 -1
- package/lib/linter/conditionSchema.js +261 -135
- package/lib/linter/conditionSchema.js.map +1 -1
- package/lib/linter/contextWarnings.js +2 -0
- package/lib/linter/contextWarnings.js.map +1 -1
- package/lib/linter/diagnostics.d.ts +10 -3
- package/lib/linter/diagnostics.js +16 -2
- package/lib/linter/diagnostics.js.map +1 -1
- package/lib/linter/expressionDiagnostics.js +2 -2
- package/lib/linter/expressionDiagnostics.js.map +1 -1
- package/lib/linter/featureSchema.d.ts +10 -9
- package/lib/linter/featureSchema.js +60 -158
- package/lib/linter/featureSchema.js.map +1 -1
- package/lib/linter/lintProject.js +229 -2
- package/lib/linter/lintProject.js.map +1 -1
- package/lib/linter/messageWarnings.d.ts +16 -0
- package/lib/linter/messageWarnings.js +181 -0
- package/lib/linter/messageWarnings.js.map +1 -0
- package/lib/linter/regexSafety.d.ts +17 -2
- package/lib/linter/regexSafety.js +257 -13
- package/lib/linter/regexSafety.js.map +1 -1
- package/lib/linter/reservedNames.d.ts +21 -0
- package/lib/linter/reservedNames.js +79 -0
- package/lib/linter/reservedNames.js.map +1 -1
- package/lib/linter/schema.d.ts +45 -3
- package/lib/linter/schema.js +341 -35
- package/lib/linter/schema.js.map +1 -1
- package/lib/linter/schemaAnnotations.d.ts +34 -0
- package/lib/linter/schemaAnnotations.js +409 -0
- package/lib/linter/schemaAnnotations.js.map +1 -0
- package/lib/linter/schemaEngine.d.ts +7 -0
- package/lib/linter/schemaEngine.js +80 -0
- package/lib/linter/schemaEngine.js.map +1 -0
- package/lib/linter/schemaFormats.d.ts +6 -0
- package/lib/linter/schemaFormats.js +20 -0
- package/lib/linter/schemaFormats.js.map +1 -1
- package/lib/linter/schemaMutations.d.ts +38 -0
- package/lib/linter/schemaMutations.js +617 -0
- package/lib/linter/schemaMutations.js.map +1 -0
- package/lib/linter/schemaPatterns.d.ts +26 -0
- package/lib/linter/schemaPatterns.js +316 -0
- package/lib/linter/schemaPatterns.js.map +1 -0
- package/lib/linter/schemaReferences.d.ts +18 -0
- package/lib/linter/schemaReferences.js +43 -0
- package/lib/linter/schemaReferences.js.map +1 -0
- package/lib/linter/targetSchema.d.ts +3 -0
- package/lib/linter/targetSchema.js +3 -0
- package/lib/linter/targetSchema.js.map +1 -1
- package/lib/linter/testSchema.d.ts +1 -1
- package/lib/linter/testSchema.js +70 -11
- package/lib/linter/testSchema.js.map +1 -1
- package/lib/linter/variableSchema.d.ts +5 -5
- package/lib/linter/variableSchema.js +5 -5
- package/lib/linter/variableSchema.js.map +1 -1
- package/lib/linter/warningCodes.d.ts +1 -1
- package/lib/linter/warningCodes.js +4 -0
- package/lib/linter/warningCodes.js.map +1 -1
- package/lib/list/index.js +45 -4
- package/lib/list/index.js.map +1 -1
- package/lib/list/matrix.d.ts +6 -1
- package/lib/list/matrix.js +31 -0
- package/lib/list/matrix.js.map +1 -1
- package/lib/promoter/index.js +15 -0
- package/lib/promoter/index.js.map +1 -1
- package/lib/rewrite/conditions.d.ts +2 -1
- package/lib/rewrite/conditions.js +1 -0
- package/lib/rewrite/conditions.js.map +1 -1
- package/lib/schemaNodes.d.ts +50 -0
- package/lib/schemaNodes.js +146 -0
- package/lib/schemaNodes.js.map +1 -0
- package/lib/targeting.d.ts +28 -1
- package/lib/targeting.js +111 -0
- package/lib/targeting.js.map +1 -1
- package/lib/tester/segments.d.ts +3 -0
- package/lib/tester/testProject.d.ts +1 -1
- package/lib/tester/testProject.js +16 -3
- package/lib/tester/testProject.js.map +1 -1
- package/lib/tester/testSchema.d.ts +9 -0
- package/lib/tester/testSchema.js +130 -0
- package/lib/tester/testSchema.js.map +1 -0
- package/package.json +7 -6
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.matchesSchemaValue = matchesSchemaValue;
|
|
4
|
+
exports.getInputLeafIssues = getInputLeafIssues;
|
|
4
5
|
exports.getConditionsZodSchema = getConditionsZodSchema;
|
|
5
6
|
const zod_1 = require("zod");
|
|
6
7
|
const sdk_1 = require("@featurevisor/sdk");
|
|
@@ -59,24 +60,6 @@ function isMembershipList(value) {
|
|
|
59
60
|
typeof entry === "string" ||
|
|
60
61
|
(typeof entry === "number" && Number.isFinite(entry))));
|
|
61
62
|
}
|
|
62
|
-
function getPortableRegexError(pattern) {
|
|
63
|
-
try {
|
|
64
|
-
new RegExp(pattern);
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
return "must be a valid regular expression";
|
|
68
|
-
}
|
|
69
|
-
if (/\(\?/.test(pattern)) {
|
|
70
|
-
return "must not use lookaround, named groups, noncapturing groups, atomic groups, or inline mode groups";
|
|
71
|
-
}
|
|
72
|
-
if (/\\(?:[1-9]|k<|k'|g<|g')/.test(pattern)) {
|
|
73
|
-
return "must not use backreferences";
|
|
74
|
-
}
|
|
75
|
-
if (/(?:[?*+]|\{\d+(?:,\d*)?\})\+/.test(pattern)) {
|
|
76
|
-
return "must not use possessive quantifiers";
|
|
77
|
-
}
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
63
|
function valueDeepEqual(a, b) {
|
|
81
64
|
if (a === b)
|
|
82
65
|
return true;
|
|
@@ -375,11 +358,19 @@ function validateAttributeAwareCondition(data, context, attributesByKey, schemas
|
|
|
375
358
|
if (!resolvedLeaf) {
|
|
376
359
|
return;
|
|
377
360
|
}
|
|
361
|
+
validateLeafCondition({ name: data.attribute, operator: data.operator, value: data.value }, resolvedLeaf, context, schemasByKey, { noun: "attribute" });
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Checks an operator and its value against the leaf it compares: an
|
|
365
|
+
* attribute's, or the field of a schema an `input` leaf reads. With
|
|
366
|
+
* `valueFrom`, the value is another field's, so only the operator is checked.
|
|
367
|
+
*/
|
|
368
|
+
function validateLeafCondition(data, resolvedLeaf, context, schemasByKey, { noun, valueFrom = false }) {
|
|
378
369
|
const leafType = getLeafType(resolvedLeaf);
|
|
379
370
|
const leafTypes = getLeafTypes(resolvedLeaf, schemasByKey);
|
|
380
371
|
if (emptinessOperators.includes(data.operator)) {
|
|
381
372
|
if (leafTypes.length > 0 && !leafTypes.some((type) => emptinessTypes.includes(type))) {
|
|
382
|
-
addIssue(context, `Operator "${data.operator}" can only be used with string, array or object
|
|
373
|
+
addIssue(context, `Operator "${data.operator}" can only be used with string, array or object ${noun}s.`);
|
|
383
374
|
}
|
|
384
375
|
return;
|
|
385
376
|
}
|
|
@@ -387,42 +378,42 @@ function validateAttributeAwareCondition(data, context, attributesByKey, schemas
|
|
|
387
378
|
return;
|
|
388
379
|
}
|
|
389
380
|
if (resolvedLeaf.kind === "schema" && leafType === "object") {
|
|
390
|
-
addIssue(context,
|
|
381
|
+
addIssue(context, `${noun === "attribute" ? "Attribute" : "Field"} "${data.name}" resolves to an object. Use a nested ${noun} path or \`exists\`/\`notExists\` instead.`);
|
|
391
382
|
return;
|
|
392
383
|
}
|
|
393
384
|
if (numericOperators.includes(data.operator) &&
|
|
394
385
|
!leafTypes.some((type) => ["integer", "number"].includes(type))) {
|
|
395
|
-
addIssue(context, `Operator "${data.operator}" can only be used with integer or number
|
|
386
|
+
addIssue(context, `Operator "${data.operator}" can only be used with integer or number ${noun}s.`);
|
|
396
387
|
return;
|
|
397
388
|
}
|
|
398
389
|
if (stringOperators.includes(data.operator) &&
|
|
399
390
|
!leafTypes.some((type) => ["string", "semver", "date-time"].includes(type))) {
|
|
400
|
-
addIssue(context, `Operator "${data.operator}" can only be used with string-like
|
|
391
|
+
addIssue(context, `Operator "${data.operator}" can only be used with string-like ${noun}s.`);
|
|
401
392
|
return;
|
|
402
393
|
}
|
|
403
394
|
if (semverOperators.includes(data.operator) &&
|
|
404
395
|
!leafTypes.some((type) => ["string", "semver"].includes(type))) {
|
|
405
|
-
addIssue(context, `Operator "${data.operator}" can only be used with string
|
|
396
|
+
addIssue(context, `Operator "${data.operator}" can only be used with string ${noun}s, including format: semver.`);
|
|
406
397
|
return;
|
|
407
398
|
}
|
|
408
399
|
if (dateOperators.includes(data.operator) &&
|
|
409
400
|
!leafTypes.some((type) => ["string", "date-time"].includes(type))) {
|
|
410
|
-
addIssue(context, `Operator "${data.operator}" can only be used with string
|
|
401
|
+
addIssue(context, `Operator "${data.operator}" can only be used with string ${noun}s, including format: date-time.`);
|
|
411
402
|
return;
|
|
412
403
|
}
|
|
413
404
|
if (arrayMembershipOperators.includes(data.operator)) {
|
|
414
405
|
if (!supportsMembership(leafTypes)) {
|
|
415
|
-
addIssue(context, `Operator "${data.operator}" can only be used with string, integer or number
|
|
406
|
+
addIssue(context, `Operator "${data.operator}" can only be used with string, integer or number ${noun}s, or nested paths declared as one of them.`);
|
|
416
407
|
return;
|
|
417
408
|
}
|
|
418
|
-
if (!Array.isArray(data.value)) {
|
|
409
|
+
if (valueFrom || !Array.isArray(data.value)) {
|
|
419
410
|
return;
|
|
420
411
|
}
|
|
421
412
|
let entryIssue = false;
|
|
422
413
|
data.value.forEach((entry, index) => {
|
|
423
414
|
if (entry !== null && !matchesLeafValue(resolvedLeaf, entry, schemasByKey)) {
|
|
424
415
|
entryIssue = true;
|
|
425
|
-
addIssue(context, `Value at index ${index} does not match the schema of
|
|
416
|
+
addIssue(context, `Value at index ${index} does not match the schema of ${noun} "${data.name}".`, ["value", index]);
|
|
426
417
|
}
|
|
427
418
|
});
|
|
428
419
|
const mixed = !entryIssue && (0, listTypes_1.getMixedListMessage)(data.operator, data.value);
|
|
@@ -433,7 +424,7 @@ function validateAttributeAwareCondition(data, context, attributesByKey, schemas
|
|
|
433
424
|
if ([...arrayOperators, ...arrayListOperators].includes(data.operator)) {
|
|
434
425
|
const itemLeaf = getArrayItemLeaf(resolvedLeaf, schemasByKey);
|
|
435
426
|
if (!itemLeaf) {
|
|
436
|
-
addIssue(context, `Operator "${data.operator}" can only be used with array
|
|
427
|
+
addIssue(context, `Operator "${data.operator}" can only be used with array ${noun}s.`);
|
|
437
428
|
return;
|
|
438
429
|
}
|
|
439
430
|
const itemType = getLeafType(itemLeaf);
|
|
@@ -441,6 +432,8 @@ function validateAttributeAwareCondition(data, context, attributesByKey, schemas
|
|
|
441
432
|
addIssue(context, `Operator "${data.operator}" only supports arrays of primitive values.`);
|
|
442
433
|
return;
|
|
443
434
|
}
|
|
435
|
+
if (valueFrom)
|
|
436
|
+
return;
|
|
444
437
|
if (arrayListOperators.includes(data.operator)) {
|
|
445
438
|
let entryIssue = false;
|
|
446
439
|
if (Array.isArray(data.value))
|
|
@@ -453,7 +446,7 @@ function validateAttributeAwareCondition(data, context, attributesByKey, schemas
|
|
|
453
446
|
}
|
|
454
447
|
if (!matchesLeafValue(itemLeaf, value, schemasByKey)) {
|
|
455
448
|
entryIssue = true;
|
|
456
|
-
addIssue(context, `Value at index ${index} does not match the item schema of
|
|
449
|
+
addIssue(context, `Value at index ${index} does not match the item schema of ${noun} "${data.name}".`, ["value", index]);
|
|
457
450
|
}
|
|
458
451
|
});
|
|
459
452
|
const mixed = !entryIssue && (0, listTypes_1.getMixedListMessage)(data.operator, data.value);
|
|
@@ -461,20 +454,192 @@ function validateAttributeAwareCondition(data, context, attributesByKey, schemas
|
|
|
461
454
|
addWarning(context, mixed, "mixed_list");
|
|
462
455
|
}
|
|
463
456
|
else if (!matchesLeafValue(itemLeaf, data.value, schemasByKey)) {
|
|
464
|
-
addIssue(context, `Value does not match the item schema of
|
|
457
|
+
addIssue(context, `Value does not match the item schema of ${noun} "${data.name}".`);
|
|
465
458
|
}
|
|
466
459
|
return;
|
|
467
460
|
}
|
|
468
461
|
if (commonOperators.includes(data.operator)) {
|
|
469
|
-
if (data.value === null) {
|
|
462
|
+
if (valueFrom || data.value === null) {
|
|
470
463
|
return;
|
|
471
464
|
}
|
|
472
465
|
if (!matchesLeafValue(resolvedLeaf, data.value, schemasByKey)) {
|
|
473
|
-
addIssue(context, `Value does not match the schema of
|
|
466
|
+
addIssue(context, `Value does not match the schema of ${noun} "${data.name}".`);
|
|
474
467
|
}
|
|
475
468
|
}
|
|
476
469
|
}
|
|
477
|
-
|
|
470
|
+
/**
|
|
471
|
+
* Issues an `input` leaf's operator and value have against the field it
|
|
472
|
+
* reads, by the rules attribute conditions follow, with paths relative to the
|
|
473
|
+
* leaf. With `valueFrom`, the other field's type must suit the comparison too.
|
|
474
|
+
*/
|
|
475
|
+
function getInputLeafIssues(leaf, field, other, schemasByKey) {
|
|
476
|
+
const issues = [];
|
|
477
|
+
const context = {
|
|
478
|
+
addIssue: (issue) => {
|
|
479
|
+
if (!issue.params?.warning)
|
|
480
|
+
issues.push({ path: issue.path || [], message: issue.message || "" });
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
const valueFrom = leaf.valueFrom !== undefined;
|
|
484
|
+
validateLeafCondition({ name: leaf.input, operator: leaf.operator, value: leaf.value }, { kind: "schema", schema: field }, context, schemasByKey, { noun: "field", valueFrom });
|
|
485
|
+
if (issues.length > 0 || !valueFrom || !other)
|
|
486
|
+
return issues;
|
|
487
|
+
// The other field holds the value compared: an item for membership, a list for list operators.
|
|
488
|
+
const types = (schema) => getLeafTypes({ kind: "schema", schema: schema }, schemasByKey);
|
|
489
|
+
const fieldTypes = types(field);
|
|
490
|
+
const itemOf = (schema) => getArrayItemLeaf({ kind: "schema", schema: schema }, schemasByKey);
|
|
491
|
+
const otherName = leaf.valueFrom.input;
|
|
492
|
+
const mismatch = (expected) => issues.push({
|
|
493
|
+
path: ["valueFrom", "input"],
|
|
494
|
+
message: `Field "${otherName}" is ${expected}, so "${leaf.operator}" cannot compare "${leaf.input}" with it.`,
|
|
495
|
+
});
|
|
496
|
+
const shares = (left, right) => {
|
|
497
|
+
const kind = (type) => (type === "integer" ? "number" : type);
|
|
498
|
+
return left.some((type) => right.map(kind).includes(kind(type)));
|
|
499
|
+
};
|
|
500
|
+
if (arrayMembershipOperators.includes(leaf.operator)) {
|
|
501
|
+
const item = itemOf(other);
|
|
502
|
+
if (!item)
|
|
503
|
+
mismatch("not an array");
|
|
504
|
+
else if (!shares(fieldTypes, getLeafTypes(item, schemasByKey)))
|
|
505
|
+
mismatch("a list of another type");
|
|
506
|
+
}
|
|
507
|
+
else if (arrayListOperators.includes(leaf.operator)) {
|
|
508
|
+
const item = itemOf(other);
|
|
509
|
+
const own = itemOf(field);
|
|
510
|
+
if (!item)
|
|
511
|
+
mismatch("not an array");
|
|
512
|
+
else if (own && !shares(getLeafTypes(own, schemasByKey), getLeafTypes(item, schemasByKey)))
|
|
513
|
+
mismatch("a list of another type");
|
|
514
|
+
}
|
|
515
|
+
else if (arrayOperators.includes(leaf.operator)) {
|
|
516
|
+
const own = itemOf(field);
|
|
517
|
+
if (own && !shares(getLeafTypes(own, schemasByKey), types(other)))
|
|
518
|
+
mismatch("of another type than its items");
|
|
519
|
+
}
|
|
520
|
+
else if (!shares(fieldTypes, types(other))) {
|
|
521
|
+
mismatch("of another type");
|
|
522
|
+
}
|
|
523
|
+
return issues;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Checks a leaf's `value` and `regexFlags` against its operator, whatever the
|
|
527
|
+
* leaf compares: the shape each operator takes, and for `matches` and
|
|
528
|
+
* `notMatches` a pattern inside the portable subset that cannot backtrack
|
|
529
|
+
* exponentially. `strict` holds a pattern to the stricter rules for patterns
|
|
530
|
+
* applications test untrusted input against. `unknownLeaf` says whether the
|
|
531
|
+
* leaf's own type is unknown, so a list's entries have to agree among themselves.
|
|
532
|
+
*/
|
|
533
|
+
function refineLeafValue(data, context, { strict = false, unknownLeaf } = {}) {
|
|
534
|
+
if (commonOperators.includes(data.operator) &&
|
|
535
|
+
!(data.value === null ||
|
|
536
|
+
typeof data.value === "string" ||
|
|
537
|
+
typeof data.value === "number" ||
|
|
538
|
+
typeof data.value === "boolean" ||
|
|
539
|
+
data.value instanceof Date ||
|
|
540
|
+
data.value === null)) {
|
|
541
|
+
addIssue(context, `when operator is "${data.operator}", value has to be either a string, number, boolean, date or null`);
|
|
542
|
+
}
|
|
543
|
+
if (numericOperators.includes(data.operator) && typeof data.value !== "number") {
|
|
544
|
+
addIssue(context, `when operator is "${data.operator}", value must be a number`);
|
|
545
|
+
}
|
|
546
|
+
if ([...stringOperators, ...semverOperators].includes(data.operator) &&
|
|
547
|
+
typeof data.value !== "string") {
|
|
548
|
+
addIssue(context, semverOperators.includes(data.operator) && typeof data.value === "number"
|
|
549
|
+
? `when operator is "${data.operator}", value must be a string. Quote versions such as '5.5' so YAML keeps them as text.`
|
|
550
|
+
: `when operator is "${data.operator}", value must be a string`);
|
|
551
|
+
}
|
|
552
|
+
else if (semverOperators.includes(data.operator) && !(0, sdk_1.isValidVersion)(data.value)) {
|
|
553
|
+
// The operators treat an unparseable version as no match, so the condition can never pass.
|
|
554
|
+
addIssue(context, `when operator is "${data.operator}", value must be a version the operators can compare, such as 5.5.0, 5.5 or v5.5.0`);
|
|
555
|
+
}
|
|
556
|
+
if (dateOperators.includes(data.operator)) {
|
|
557
|
+
if (typeof data.value === "undefined" || data.value === null || data.value === "") {
|
|
558
|
+
addIssue(context, `when operator is "${data.operator}", value must be provided`);
|
|
559
|
+
}
|
|
560
|
+
else if (typeof data.value === "string") {
|
|
561
|
+
if (!isIsoDateString(data.value)) {
|
|
562
|
+
addIssue(context, `when operator is "${data.operator}", use an ISO 8601 timestamp YYYY-MM-DDTHH:mm:ss with Z or ±HH:mm and optional 1 to 9 fractional digits, for example 2026-11-27T00:00:00.123456Z`);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
else if (data.value instanceof Date) {
|
|
566
|
+
if ((0, sdk_1.getPortableTimestamp)(data.value) === null) {
|
|
567
|
+
addIssue(context, `when operator is "${data.operator}", value is a Date but invalid`);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
addIssue(context, `when operator is "${data.operator}", use an ISO 8601 timestamp YYYY-MM-DDTHH:mm:ss with Z or ±HH:mm and optional 1 to 9 fractional digits, for example 2026-11-27T00:00:00.123456Z`);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (arrayMembershipOperators.includes(data.operator) && !isMembershipList(data.value)) {
|
|
575
|
+
addIssue(context, `when operator is "${data.operator}", value must be a list of strings or of numbers`);
|
|
576
|
+
}
|
|
577
|
+
else if (arrayMembershipOperators.includes(data.operator) && unknownLeaf?.()) {
|
|
578
|
+
// Without a known attribute, the list's own types still have to agree.
|
|
579
|
+
const mixed = (0, listTypes_1.getMixedListMessage)(data.operator, data.value);
|
|
580
|
+
if (mixed)
|
|
581
|
+
addWarning(context, mixed, "mixed_list");
|
|
582
|
+
}
|
|
583
|
+
if (arrayListOperators.includes(data.operator)) {
|
|
584
|
+
if (!Array.isArray(data.value)) {
|
|
585
|
+
addIssue(context, `when operator is "${data.operator}", value must be an array of primitive values`, ["value"]);
|
|
586
|
+
}
|
|
587
|
+
else
|
|
588
|
+
for (let index = 0; index < data.value.length; index++) {
|
|
589
|
+
const value = data.value[index];
|
|
590
|
+
if (!isPrimitiveAttributeValue(value) ||
|
|
591
|
+
(typeof value === "number" && !Number.isFinite(value)))
|
|
592
|
+
addIssue(context, `when operator is "${data.operator}", each candidate must be a string, number, boolean or null`, ["value", index]);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (!arrayMembershipOperators.includes(data.operator) && Array.isArray(data.value)) {
|
|
596
|
+
if (!arrayListOperators.includes(data.operator)) {
|
|
597
|
+
addIssue(context, `when operator is "${data.operator}", value must not be an array`);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (regexFlagsRequired(data.operator)) {
|
|
601
|
+
if (typeof data.value !== "string") {
|
|
602
|
+
addIssue(context, `when operator is "${data.operator}", value must be a string`);
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
const portableRegexError = (0, regexSafety_1.getPortableRegexError)(data.value);
|
|
606
|
+
const backtrackingError = !portableRegexError && (0, regexSafety_1.getBacktrackingError)(data.value, { strict });
|
|
607
|
+
if (portableRegexError) {
|
|
608
|
+
addIssue(context, `when operator is "${data.operator}", value ${portableRegexError} in the cross-SDK regex subset`, ["value"], "unportable_regex");
|
|
609
|
+
}
|
|
610
|
+
else if (backtrackingError) {
|
|
611
|
+
addIssue(context, `when operator is "${data.operator}", value ${backtrackingError}.`, ["value"], "regex_backtracking");
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
else if (data.regexFlags) {
|
|
616
|
+
addIssue(context, `when operator is neither "matches" nor "notMatches", regexFlags are not needed`, ["regexFlags"]);
|
|
617
|
+
}
|
|
618
|
+
if (operatorsWithoutValue.includes(data.operator) && data.value !== undefined) {
|
|
619
|
+
addIssue(context, `when operator is "${data.operator}", value is not needed`);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
function getConditionsZodSchema(projectConfig, attributesByKey, schemasByKey = {}, featuresByKey = {}, variablesByKey = {}, segmentKeys = [],
|
|
623
|
+
/** `input` leaves over the value validated, which only a schema's mutations take. */
|
|
624
|
+
{ input = false } = {}) {
|
|
625
|
+
const regexFlagsZodSchema = zod_1.z.string().refine((value) => {
|
|
626
|
+
if (typeof value === "undefined") {
|
|
627
|
+
return true;
|
|
628
|
+
}
|
|
629
|
+
return /^[gims]+$/.test(value) && new Set(value).size === value.length;
|
|
630
|
+
}, {
|
|
631
|
+
message: `regexFlags must contain unique characters from the cross-SDK set: g, i, m, s`,
|
|
632
|
+
});
|
|
633
|
+
/** Operators the project's configuration disallows, wherever a leaf sits. */
|
|
634
|
+
const refineAllowedOperator = (data, context) => {
|
|
635
|
+
for (const issue of (0, comparisonOperators_1.findDisallowedOperators)(data, projectConfig.disallowOperators || [], [])) {
|
|
636
|
+
context.addIssue({
|
|
637
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
638
|
+
path: issue.path,
|
|
639
|
+
message: `Operator "${issue.operator}" is disallowed by project configuration.`,
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
};
|
|
478
643
|
const plainConditionZodSchema = zod_1.z
|
|
479
644
|
.object({
|
|
480
645
|
attribute: (0, zodHelpers_1.refineWithMessage)(zod_1.z.string(), (value) => resolveAttributePath(value, attributesByKey, schemasByKey) !== null, (value) => `Unknown attribute "${value}".${(0, diagnostics_1.suggestKey)(value, Object.keys(attributesByKey))}`, () => zodHelpers_1.UNKNOWN_REFERENCE),
|
|
@@ -492,28 +657,12 @@ function getConditionsZodSchema(projectConfig, attributesByKey, schemasByKey = {
|
|
|
492
657
|
value: zod_1.z
|
|
493
658
|
.union([zod_1.z.string(), zod_1.z.array(zod_1.z.unknown()), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.date(), zod_1.z.null()])
|
|
494
659
|
.optional(),
|
|
495
|
-
regexFlags:
|
|
496
|
-
.string()
|
|
497
|
-
.refine((value) => {
|
|
498
|
-
if (typeof value === "undefined") {
|
|
499
|
-
return true;
|
|
500
|
-
}
|
|
501
|
-
return /^[gims]+$/.test(value) && new Set(value).size === value.length;
|
|
502
|
-
}, {
|
|
503
|
-
message: `regexFlags must contain unique characters from the cross-SDK set: g, i, m, s`,
|
|
504
|
-
})
|
|
505
|
-
.optional(),
|
|
660
|
+
regexFlags: regexFlagsZodSchema.optional(),
|
|
506
661
|
compareTo: zod_1.z.never().optional(),
|
|
507
662
|
})
|
|
508
663
|
.strict()
|
|
509
664
|
.superRefine((data, context) => {
|
|
510
|
-
|
|
511
|
-
context.addIssue({
|
|
512
|
-
code: zod_1.z.ZodIssueCode.custom,
|
|
513
|
-
path: issue.path,
|
|
514
|
-
message: `Operator "${issue.operator}" is disallowed by project configuration.`,
|
|
515
|
-
});
|
|
516
|
-
}
|
|
665
|
+
refineAllowedOperator(data, context);
|
|
517
666
|
const dateOperatorError = getDateOperatorError(data.attribute, data.operator, attributesByKey, schemasByKey);
|
|
518
667
|
if (dateOperatorError) {
|
|
519
668
|
addIssue(context, dateOperatorError, ["operator"]);
|
|
@@ -528,95 +677,61 @@ function getConditionsZodSchema(projectConfig, attributesByKey, schemasByKey = {
|
|
|
528
677
|
return;
|
|
529
678
|
}
|
|
530
679
|
}
|
|
531
|
-
|
|
532
|
-
!(data.
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
if (arrayMembershipOperators.includes(data.operator) && !isMembershipList(data.value)) {
|
|
572
|
-
addIssue(context, `when operator is "${data.operator}", value must be a list of strings or of numbers`);
|
|
573
|
-
}
|
|
574
|
-
else if (arrayMembershipOperators.includes(data.operator)) {
|
|
575
|
-
const leaf = resolveAttributePath(data.attribute, attributesByKey, schemasByKey);
|
|
576
|
-
// Without a known attribute, the list's own types still have to agree.
|
|
577
|
-
const mixed = !leaf && (0, listTypes_1.getMixedListMessage)(data.operator, data.value);
|
|
578
|
-
if (mixed)
|
|
579
|
-
addWarning(context, mixed, "mixed_list");
|
|
580
|
-
}
|
|
581
|
-
if (arrayListOperators.includes(data.operator)) {
|
|
582
|
-
if (!Array.isArray(data.value)) {
|
|
583
|
-
addIssue(context, `when operator is "${data.operator}", value must be an array of primitive values`, ["value"]);
|
|
584
|
-
}
|
|
585
|
-
else
|
|
586
|
-
for (let index = 0; index < data.value.length; index++) {
|
|
587
|
-
const value = data.value[index];
|
|
588
|
-
if (!isPrimitiveAttributeValue(value) ||
|
|
589
|
-
(typeof value === "number" && !Number.isFinite(value)))
|
|
590
|
-
addIssue(context, `when operator is "${data.operator}", each candidate must be a string, number, boolean or null`, ["value", index]);
|
|
591
|
-
}
|
|
680
|
+
refineLeafValue(data, context, {
|
|
681
|
+
unknownLeaf: () => !resolveAttributePath(data.attribute, attributesByKey, schemasByKey),
|
|
682
|
+
});
|
|
683
|
+
validateAttributeAwareCondition(data, context, attributesByKey, schemasByKey);
|
|
684
|
+
});
|
|
685
|
+
// An input leaf reads the value a schema validates. `value` is always a value,
|
|
686
|
+
// and `valueFrom` reads the other side from another input.
|
|
687
|
+
const inputConditionZodSchema = zod_1.z
|
|
688
|
+
.object({
|
|
689
|
+
input: zod_1.z.string().min(1),
|
|
690
|
+
operator: zod_1.z.enum([
|
|
691
|
+
...commonOperators,
|
|
692
|
+
...numericOperators,
|
|
693
|
+
...stringOperators,
|
|
694
|
+
...semverOperators,
|
|
695
|
+
...dateOperators,
|
|
696
|
+
...arrayMembershipOperators,
|
|
697
|
+
...arrayOperators,
|
|
698
|
+
...arrayListOperators,
|
|
699
|
+
...operatorsWithoutValue,
|
|
700
|
+
]),
|
|
701
|
+
value: zod_1.z
|
|
702
|
+
.union([zod_1.z.string(), zod_1.z.array(zod_1.z.unknown()), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()], {
|
|
703
|
+
message: "A value is text, a number, a boolean, null or a list. To compare with another field, use `valueFrom: { input: ... }`.",
|
|
704
|
+
})
|
|
705
|
+
.optional(),
|
|
706
|
+
valueFrom: zod_1.z
|
|
707
|
+
.object({ input: zod_1.z.string().min(1) })
|
|
708
|
+
.strict()
|
|
709
|
+
.optional(),
|
|
710
|
+
regexFlags: regexFlagsZodSchema.optional(),
|
|
711
|
+
})
|
|
712
|
+
.strict()
|
|
713
|
+
.superRefine((data, context) => {
|
|
714
|
+
refineAllowedOperator(data, context);
|
|
715
|
+
// A value takes the shape its operator needs, as an attribute leaf's does. Its pattern is
|
|
716
|
+
// tested against what users send, so it meets the rules for patterns schemas deliver.
|
|
717
|
+
if (data.valueFrom === undefined) {
|
|
718
|
+
refineLeafValue(data, context, { strict: true });
|
|
719
|
+
return;
|
|
592
720
|
}
|
|
593
|
-
if (
|
|
594
|
-
|
|
595
|
-
addIssue(context, `when operator is "${data.operator}", value must not be an array`);
|
|
596
|
-
}
|
|
721
|
+
if (data.regexFlags && !regexFlagsRequired(data.operator)) {
|
|
722
|
+
addIssue(context, `when operator is neither "matches" nor "notMatches", regexFlags are not needed`, ["regexFlags"]);
|
|
597
723
|
}
|
|
598
|
-
if (
|
|
599
|
-
|
|
600
|
-
addIssue(context, `when operator is "${data.operator}", value must be a string`);
|
|
601
|
-
}
|
|
602
|
-
else {
|
|
603
|
-
const portableRegexError = getPortableRegexError(data.value);
|
|
604
|
-
const backtrackingError = !portableRegexError && (0, regexSafety_1.getBacktrackingError)(data.value);
|
|
605
|
-
if (portableRegexError) {
|
|
606
|
-
addIssue(context, `when operator is "${data.operator}", value ${portableRegexError} in the cross-SDK regex subset`, ["value"], "unportable_regex");
|
|
607
|
-
}
|
|
608
|
-
else if (backtrackingError) {
|
|
609
|
-
addIssue(context, `when operator is "${data.operator}", value ${backtrackingError}.`, ["value"], "regex_backtracking");
|
|
610
|
-
}
|
|
611
|
-
}
|
|
724
|
+
if (data.value !== undefined) {
|
|
725
|
+
addIssue(context, "A condition takes `value` or `valueFrom`, not both.", ["valueFrom"]);
|
|
612
726
|
}
|
|
613
|
-
else if (data.
|
|
614
|
-
addIssue(context, `when operator is
|
|
727
|
+
else if (operatorsWithoutValue.includes(data.operator)) {
|
|
728
|
+
addIssue(context, `when operator is "${data.operator}", valueFrom is not needed`, [
|
|
729
|
+
"valueFrom",
|
|
730
|
+
]);
|
|
615
731
|
}
|
|
616
|
-
if (
|
|
617
|
-
addIssue(context,
|
|
732
|
+
else if (data.operator === "matches" || data.operator === "notMatches") {
|
|
733
|
+
addIssue(context, `\`${data.operator}\` never takes its pattern from \`valueFrom\`, since a pattern from a user is one nobody reviewed.`, ["valueFrom"]);
|
|
618
734
|
}
|
|
619
|
-
validateAttributeAwareCondition(data, context, attributesByKey, schemasByKey);
|
|
620
735
|
});
|
|
621
736
|
const featureSchema = (0, requiredFeaturesSchema_1.getFeatureConditionZodSchema)(featuresByKey);
|
|
622
737
|
const variableSchema = (0, requiredVariablesSchema_1.getVariableConditionZodSchema)(featuresByKey, variablesByKey, schemasByKey, projectConfig);
|
|
@@ -652,6 +767,17 @@ function getConditionsZodSchema(projectConfig, attributesByKey, schemasByKey = {
|
|
|
652
767
|
.strict();
|
|
653
768
|
else if (owns("variable"))
|
|
654
769
|
selected = variableSchema;
|
|
770
|
+
else if (owns("input")) {
|
|
771
|
+
if (!input) {
|
|
772
|
+
ctx.addIssue({
|
|
773
|
+
code: "custom",
|
|
774
|
+
message: "`input` reads the value a schema validates, so it goes only in a schema's mutations and issues.",
|
|
775
|
+
path: ["input"],
|
|
776
|
+
});
|
|
777
|
+
return zod_1.z.NEVER;
|
|
778
|
+
}
|
|
779
|
+
selected = inputConditionZodSchema;
|
|
780
|
+
}
|
|
655
781
|
else if (owns("feature")) {
|
|
656
782
|
if (owns("operator")) {
|
|
657
783
|
ctx.addIssue({
|