All files / linter testSchema.ts

82.6% Statements 19/23
87.5% Branches 7/8
66.66% Functions 8/12
86.36% Lines 19/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 1472x     2x   2x         13x   13x                         13x       45x 1x                             13x       102x 1x                                 549x   5x       544x       542x     2x   2x         1x                                                                                                                 13x    
import { z } from "zod";
 
import { ProjectConfig } from "../config";
import { refineWithMessage } from "./zodHelpers";
 
export function getTestsZodSchema(
  projectConfig: ProjectConfig,
  availableFeatureKeys: [string, ...string[]],
  availableSegmentKeys: [string, ...string[]],
) {
  const scopeNames = projectConfig.scopes ? projectConfig.scopes.map((scope) => scope.name) : [];
 
  const matrixZodSchema = z.record(
    z.string(),
    z.array(
      z.union([
        // allowed values in arrays
        z.string(),
        z.number(),
        z.boolean(),
        z.null(),
      ]),
    ),
  );
 
  const segmentTestZodSchema = z
    .object({
      segment: refineWithMessage(
        z.string(),
        (value) => availableSegmentKeys.includes(value),
        (value) => `Unknown segment "${value}"`,
      ),
      assertions: z.array(
        z
          .object({
            matrix: matrixZodSchema.optional(),
            description: z.string().optional(),
            context: z.record(z.string(), z.unknown()),
            expectedToMatch: z.boolean(),
          })
          .strict(),
      ),
    })
    .strict();
 
  const featureTestZodSchema = z
    .object({
      feature: refineWithMessage(
        z.string(),
        (value) => availableFeatureKeys.includes(value),
        (value) => `Unknown feature "${value}"`,
      ),
      assertions: z.array(
        z
          .object({
            matrix: matrixZodSchema.optional(),
            description: z.string().optional(),
            at: z.union([
              z.number().min(0).max(100),
 
              // because of supporting matrix
              z.string(),
            ]),
            environment: Array.isArray(projectConfig.environments)
              ? refineWithMessage(
                  z.string(),
                  (value) => {
                    if (value.indexOf("${{") === 0) {
                      // allow unknown strings for matrix
                      return true;
                    }
 
                    // otherwise only known environments should be passed
                    if (
                      Array.isArray(projectConfig.environments) &&
                      projectConfig.environments.includes(value)
                    ) {
                      return true;
                    }
 
                    return false;
                  },
                  (value) => `Unknown environment "${value}"`,
                )
              : z.never().optional(),
            tag: refineWithMessage(
              z.string(),
              (value) => projectConfig.tags.includes(value),
              (value) => `Unknown tag "${value}"`,
            ).optional(),
            scope: refineWithMessage(
              z.string(),
              (value) => scopeNames.includes(value),
              (value) => `Unknown scope "${value}"`,
            ).optional(),
 
            // parent
            sticky: z.record(z.string(), z.record(z.string(), z.any())).optional(),
            context: z.record(z.string(), z.unknown()).optional(),
 
            defaultVariationValue: z.string().optional(),
            defaultVariableValues: z.record(z.string(), z.unknown()).optional(),
 
            expectedToBeEnabled: z.boolean().optional(),
            expectedVariation: z.string().nullable().optional(),
            expectedVariables: z.record(z.string(), z.unknown()).optional(),
            expectedEvaluations: z
              .object({
                flag: z.record(z.string(), z.any()).optional(),
                variation: z.record(z.string(), z.any()).optional(),
                variables: z.record(z.string(), z.record(z.string(), z.any())).optional(),
              })
              .optional(),
 
            children: z
              .array(
                z.object({
                  // copied from parent
                  sticky: z.record(z.string(), z.record(z.string(), z.any())).optional(),
                  context: z.record(z.string(), z.unknown()).optional(),
 
                  defaultVariationValue: z.string().optional(),
                  defaultVariableValues: z.record(z.string(), z.unknown()).optional(),
 
                  expectedToBeEnabled: z.boolean().optional(),
                  expectedVariation: z.string().nullable().optional(),
                  expectedVariables: z.record(z.string(), z.unknown()).optional(),
 
                  expectedEvaluations: z
                    .object({
                      flag: z.record(z.string(), z.any()).optional(),
                      variation: z.record(z.string(), z.any()).optional(),
                      variables: z.record(z.string(), z.record(z.string(), z.any())).optional(),
                    })
                    .optional(),
                }),
              )
              .optional(),
          })
          .strict(),
      ),
    })
    .strict();
 
  return z.union([segmentTestZodSchema, featureTestZodSchema]);
}