All files / linter testSchema.ts

59.09% Statements 13/22
75% Branches 6/8
33.33% Functions 4/12
61.9% Lines 13/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 149 150 151 152 153 154 155 1561x       1x         6x   6x                       6x     33x                                   6x     73x                                     408x   3x       405x       405x                                                                                                                                                         6x    
import { z } from "zod";
 
import { ProjectConfig } from "../config";
 
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.array(
      z.union([
        // allowed values in arrays
        z.string(),
        z.number(),
        z.boolean(),
        z.null(),
      ]),
    ),
  );
 
  const segmentTestZodSchema = z
    .object({
      segment: z.string().refine(
        (value) => availableSegmentKeys.includes(value),
        (value) => ({
          message: `Unknown segment "${value}"`,
        }),
      ),
      assertions: z.array(
        z
          .object({
            matrix: matrixZodSchema.optional(),
            description: z.string().optional(),
            context: z.record(z.unknown()),
            expectedToMatch: z.boolean(),
          })
          .strict(),
      ),
    })
    .strict();
 
  const featureTestZodSchema = z
    .object({
      feature: z.string().refine(
        (value) => availableFeatureKeys.includes(value),
        (value) => ({
          message: `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)
              ? z.string().refine(
                  (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) => ({
                    message: `Unknown environment "${value}"`,
                  }),
                )
              : z.never().optional(),
            tag: z
              .string()
              .refine(
                (value) => projectConfig.tags.includes(value),
                (value) => ({
                  message: `Unknown tag "${value}"`,
                }),
              )
              .optional(),
            scope: z
              .string()
              .refine(
                (value) => scopeNames.includes(value),
                (value) => ({
                  message: `Unknown scope "${value}"`,
                }),
              )
              .optional(),
 
            // parent
            sticky: z.record(z.record(z.any())).optional(),
            context: z.record(z.unknown()).optional(),
 
            defaultVariationValue: z.string().optional(),
            defaultVariableValues: z.record(z.unknown()).optional(),
 
            expectedToBeEnabled: z.boolean().optional(),
            expectedVariation: z.string().nullable().optional(),
            expectedVariables: z.record(z.unknown()).optional(),
            expectedEvaluations: z
              .object({
                flag: z.record(z.any()).optional(),
                variation: z.record(z.any()).optional(),
                variables: z.record(z.record(z.any())).optional(),
              })
              .optional(),
 
            children: z
              .array(
                z.object({
                  // copied from parent
                  sticky: z.record(z.record(z.any())).optional(),
                  context: z.record(z.unknown()).optional(),
 
                  defaultVariationValue: z.string().optional(),
                  defaultVariableValues: z.record(z.unknown()).optional(),
 
                  expectedToBeEnabled: z.boolean().optional(),
                  expectedVariation: z.string().nullable().optional(),
                  expectedVariables: z.record(z.unknown()).optional(),
 
                  expectedEvaluations: z
                    .object({
                      flag: z.record(z.any()).optional(),
                      variation: z.record(z.any()).optional(),
                      variables: z.record(z.record(z.any())).optional(),
                    })
                    .optional(),
                }),
              )
              .optional(),
          })
          .strict(),
      ),
    })
    .strict();
 
  return z.union([segmentTestZodSchema, featureTestZodSchema]);
}