All files / src/entities utils.ts

95.45% Statements 21/22
91.66% Branches 11/12
100% Functions 4/4
95.23% Lines 20/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 6942x   42x   42x 162x 76x   86x 86x 86x   2x   84x     42x                 361x                               42x 14x 2x                     42x         12x 6x   6x 6x        
import { z } from "zod";
 
const DEFAULT_NULL = process.env.DEFAULT_NULL === "true";
 
export const flexibleBoolean = z.preprocess(val => {
  if (typeof val === "boolean") {
    return val;
  }
  let result = "false";
  try {
    result = String(val).toLowerCase();
  } catch {
    return false;
  }
  return ["true", "t", "1"].includes(result);
}, z.boolean());
 
export const flexibleBooleanNullable = DEFAULT_NULL
  ? flexibleBoolean.nullable().default(null)
  : flexibleBoolean.nullable();
 
/**
 * Required ID field that accepts string or number input.
 * Unlike z.coerce.string(), this properly rejects undefined/null values
 * instead of coercing them to the literal string "undefined"/"null".
 */
export const requiredId = z.preprocess(val => val ?? "", z.coerce.string().min(1));
 
/**
 * Asserts that a value is defined (not undefined).
 * Used for fields validated by Zod .refine() where TypeScript cannot
 * automatically narrow the type after runtime validation.
 *
 * Note: This intentionally only checks for undefined, not empty strings.
 * Empty string validation is handled by Zod schema .refine() checks which
 * run during Schema.parse(args) BEFORE handler code executes. This function
 * exists solely for TypeScript type narrowing after validation passes.
 *
 * @param value - The value to assert
 * @param fieldName - Name of the field for error messages
 * @throws Error if value is undefined
 */
export function assertDefined<T>(value: T | undefined, fieldName: string): asserts value is T {
  if (value === undefined) {
    throw new Error(`${fieldName} is required but was not provided`);
  }
}
 
/**
 * Validates that the appropriate ID field is provided based on scope.
 * Used by webhook schemas to ensure projectId or groupId is present.
 *
 * @param data - Object containing scope, projectId, and groupId fields
 * @returns true if validation passes
 */
export function validateScopeId(data: {
  scope: "project" | "group";
  projectId?: string;
  groupId?: string;
}): boolean {
  if (data.scope === "project") {
    return !!data.projectId;
  }
  Eif (data.scope === "group") {
    return !!data.groupId;
  }
  return true;
}