All files / src/validation galacticInputValidation.ts

95.45% Statements 42/44
80% Branches 12/15
100% Functions 3/3
95.45% Lines 42/44

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 571x 1x 1x     1x 1x     1x 11x 11x 11x 11x 11x 11x 11x   1x 5x 1x 1x 1x 1x 1x   4x 4x 4x         1x 5x 5x 5x 5x 5x   5x 2x 1x 1x   5x   3x 5x 5x 2x 2x 5x   1x 1x  
import { CoordinateType } from '@/constants/coordinateType';
import { maxGalacticCoordLength, minGalacticCoordLength } from '@/constants/restrictions';
import { validationMessage } from '@/constants/validationMessages';
import type { GalacticCodeInput, GalacticInput } from '@/types/galacticTypes';
import type { Validator } from '@/types/validator';
import { multiValidation, notNull } from './baseValidation';
import { lengthIsEqualTo, onlyAllowedChars } from './textValidation';
import type { ValidationResult } from '@/contracts/validationResult';
 
export const GalacticCodeInputValidator: Validator<GalacticCodeInput> = (code: GalacticCodeInput) => {
  const validator = multiValidation(
    notNull(validationMessage.cannotBeNull(CoordinateType.GalacticCoordinates.toString())), //
    lengthIsEqualTo(minGalacticCoordLength, maxGalacticCoordLength),
    onlyAllowedChars(':1234567890abcdefABCDEF')
  );
  return validator(code);
};
 
export const GalacticInputValidator: Validator<GalacticInput> = (inputProps: GalacticInput) => {
  if (inputProps.code == null && inputProps.groups == null) {
    return {
      isValid: false,
      errorMessage: validationMessage.atleast1InputIsRequired,
    };
  }
 
  if (inputProps.code != null) {
    return GalacticCodeInputValidator(inputProps.code);
  }
 
  return { isValid: true };
};
 
export const galacticCodePattern = (value: string): ValidationResult => {
  const safeValue = value ?? '';
  const unexpectedPatternResult = {
    isValid: false,
    errorMessage: validationMessage.unexpectedPattern('Galactic', safeValue),
  };
 
  if (safeValue.length === minGalacticCoordLength) {
    if (safeValue.includes(':')) return unexpectedPatternResult;
    return { isValid: true };
  }
 
  if (safeValue.includes(':') == false) return unexpectedPatternResult;
 
  const semiColonIndexes = [5, 10, 15];
  for (const semiColonIndex of semiColonIndexes) {
    if (safeValue.length < semiColonIndex || safeValue[semiColonIndex - 1] !== ':') {
      return unexpectedPatternResult;
    }
  }
 
  return { isValid: true };
};