< Summary - Envilder Node.js SDK

Information
Class: src/sdks/nodejs/src/application/contract-validation.ts
Assembly: Default
File(s): src/sdks/nodejs/src/application/contract-validation.ts
Tag: 605_44106794472
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 97
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/sdks/nodejs/src/application/contract-validation.ts

#LineLine coverage
 1import type {
 2  ContractValidationResult,
 3  KeyCheck,
 4  MapFileError,
 5} from '../domain/contract-validation-result.js';
 6import type { ParsedMapFile } from '../domain/parsed-map-file.js';
 7import type { ISecretExistenceChecker } from '../domain/ports/secret-existence-checker.js';
 8import type { SecretProviderType } from '../domain/secret-provider-type.js';
 9
 10/**
 11 * Thrown by a load with contract validation enabled when the map file is
 12 * invalid or a declared secret cannot be confirmed. The message names the
 13 * failing keys and paths, never a secret value.
 14 */
 15export class ContractValidationError extends Error {
 16  /** The full outcome of the check that failed. */
 17  readonly result: ContractValidationResult;
 18
 19  constructor(result: ContractValidationResult) {
 720    super(buildMessage(result));
 721    this.result = result;
 722    this.name = 'ContractValidationError';
 23  }
 24}
 25
 26/** The result for a map file that failed validation: no provider was called. */
 27export function invalidMapFileResult(
 28  mapFileErrors: readonly MapFileError[],
 29): ContractValidationResult {
 430  return { isValid: false, mapFileErrors, keys: [] };
 31}
 32
 33/**
 34 * The result for a provider that cannot check existence (a custom one that
 35 * does not implement `ISecretExistenceChecker`): every key is a provider
 36 * error.
 37 */
 38export function notCheckableResult(
 39  mapFile: ParsedMapFile,
 40  provider: SecretProviderType,
 41): ContractValidationResult {
 442  const keys: KeyCheck[] = [...mapFile.mappings].map(([variable, path]) => ({
 43    variable,
 44    path,
 45    provider,
 46    status: 'provider_error',
 47    reason: 'The provider cannot check whether a secret exists.',
 48  }));
 249  return { isValid: false, mapFileErrors: [], keys };
 50}
 51
 52/**
 53 * Checks that every secret path declared in a valid map file exists, without
 54 * reading any value. A path shared by several variables is checked once.
 55 * Returns one check per variable, in map-file order.
 56 */
 57export async function checkRemoteKeys(
 58  mapFile: ParsedMapFile,
 59  checker: ISecretExistenceChecker,
 60  provider: SecretProviderType,
 61): Promise<ContractValidationResult> {
 1962  const mappings = [...mapFile.mappings];
 63  const existence =
 1964    mappings.length === 0
 65      ? new Map()
 66      : await checker.checkSecretsExist([
 5367          ...new Set(mappings.map(([, path]) => path)),
 68        ]);
 5369  const keys: KeyCheck[] = mappings.map(([variable, path]) => ({
 70    variable,
 71    path,
 72    provider,
 73    ...(existence.get(path) ?? {
 74      status: 'provider_error',
 75      reason: 'The provider returned no result for this secret.',
 76    }),
 77  }));
 1878  return {
 4079    isValid: keys.every((key) => key.status === 'exists'),
 80    mapFileErrors: [],
 81    keys,
 82  };
 83}
 84
 85function buildMessage(result: ContractValidationResult): string {
 786  if (result.mapFileErrors.length > 0) {
 287    const details = result.mapFileErrors
 288      .map((error) => `${error.key}: ${error.message}`)
 89      .join('; ');
 290    return `Map file contract validation failed: ${details}`;
 91  }
 592  const details = result.keys
 893    .filter((key) => key.status !== 'exists')
 694    .map((key) => `${key.variable} (${key.path}): ${key.status}`)
 95    .join(', ');
 596  return `Map file contract validation failed: ${details}`;
 97}