All files / src/linter testSchema.ts

100% Statements 21/21
100% Branches 10/10
100% Functions 9/9
100% Lines 21/21

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 147 148 1493x     3x   54x           54x                         54x         123x 1x                             54x         237x 2x                                 1111x   9x       1102x       1100x     2x   2x           19x 1x     18x   1x                                                                                                     54x    
import { z } from "zod";
 
import { ProjectConfig } from "../config";
import { refineWithMessage } from "./zodHelpers";
 
export function getTestsZodSchema(
  projectConfig: ProjectConfig,
  availableFeatureKeys: [string, ...string[]],
  availableSegmentKeys: [string, ...string[]],
  availableTargetKeys: [string, ...string[]],
) {
  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({
      promotable: z.boolean().optional(),
      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({
      promotable: z.boolean().optional(),
      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(),
            target: refineWithMessage(
              z.string(),
              (value) => {
                if (value.indexOf("${{") === 0) {
                  return true;
                }
 
                return availableTargetKeys.includes(value);
              },
              (value) => `Unknown target "${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]);
}