< Summary - Envilder CLI

Information
Class: src/envilder/core/application/validateMapFile/ValidateMapFileCommandHandler.ts
Assembly: Default
File(s): src/envilder/core/application/validateMapFile/ValidateMapFileCommandHandler.ts
Tag: 561_41292037308
Line coverage
94%
Covered lines: 16
Uncovered lines: 1
Coverable lines: 17
Total lines: 72
Line coverage: 94.1%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/application/validateMapFile/ValidateMapFileCommandHandler.ts

#LineLine coverage
 1import { inject, injectable } from 'inversify';
 2import { EnvironmentFileError } from '../../domain/errors/DomainErrors.js';
 3import {
 4  type MapFileViolation,
 5  validateMapFile,
 6} from '../../domain/MapFileValidator.js';
 7import type { IVariableStore } from '../../domain/ports/IVariableStore.js';
 8import { TYPES } from '../../types.js';
 9import type { ValidateMapFileCommand } from './ValidateMapFileCommand.js';
 10
 11/**
 12 * Checks a map file without calling any provider: the JSON parses, the
 13 * document matches `spec/map-file.v1.json`, and the provider configuration
 14 * it resolves to (flags over `$config`) is coherent. Returns every violation
 15 * found, an empty list when the file is usable. Only the file is read, and no
 16 * mapped value ever reaches a violation.
 17 */
 18@injectable()
 819export class ValidateMapFileCommandHandler {
 20  constructor(
 21    @inject(TYPES.IVariableStore)
 2922    private readonly variableStore: IVariableStore,
 23  ) {}
 24
 25  async handle(command: ValidateMapFileCommand): Promise<MapFileViolation[]> {
 2926    const content = await this.variableStore.readMapFileContent(
 27      command.mapPath,
 28    );
 2929    if (content === null) {
 130      throw new EnvironmentFileError(
 31        `Failed to read map file: ${command.mapPath}`,
 32      );
 33    }
 34
 35    let document: unknown;
 2836    try {
 2837      document = JSON.parse(content);
 38    } catch (error) {
 339      return [
 40        {
 41          key: '(root)',
 42          message:
 43            `Invalid JSON in parameter map file: ${command.mapPath}` +
 44            describeParseLocation(error),
 45        },
 46      ];
 47    }
 48
 2549    return validateMapFile(document, {
 50      overrides: command.overrides,
 51      supportedProviders: command.supportedProviders,
 52    });
 53  }
 54}
 55
 56/**
 57 * Where parsing stopped, never what it found there: the parser's own message
 58 * quotes the surrounding text, which can be a mapped value or a secret pasted
 59 * into the file.
 60 */
 61function describeParseLocation(error: unknown): string {
 362  const message = error instanceof Error ? error.message : '';
 363  const lineAndColumn = /line (\d+) column (\d+)/.exec(message);
 364  if (lineAndColumn !== null) {
 165    return ` (line ${lineAndColumn[1]}, column ${lineAndColumn[2]})`;
 66  }
 267  const position = /at position (\d+)/.exec(message);
 268  if (position !== null) {
 069    return ` (at position ${position[1]})`;
 70  }
 271  return '';
 72}