All files / src/validation glyphInputValidation.ts

51.02% Statements 25/49
85.71% Branches 6/7
50% Functions 2/4
51.02% Lines 25/49

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 581x 1x     1x 1x 1x   1x 12x 12x 12x 12x 12x 12x 12x   1x                   1x                   1x 7x 1x 1x 1x 1x 1x   6x 6x 6x                    
import { CoordinateType } from '@/constants/coordinateType';
import { validationMessage } from '@/constants/validationMessages';
import type { GlyphCodeInput, GlyphHexArrayInput, GlyphInput, GlyphNumberArrayInput } from '@/types/glyphTypes';
import type { Validator } from '@/types/validator';
import { multiValidation, notNull } from './baseValidation';
import { lengthIsEqualTo, onlyAllowedChars } from './textValidation';
import { maxItems, minItems } from './arrayValidation';
 
export const GlyphCodeInputValidator: Validator<GlyphCodeInput> = (code: GlyphCodeInput) => {
  const validator = multiValidation(
    notNull(validationMessage.cannotBeNull(CoordinateType.Glyphs.toString())), //
    onlyAllowedChars('1234567890abcdefABCDEF'),
    lengthIsEqualTo(12)
  );
  return validator(code);
};
 
export const GlyphHexArrayInputValidator: Validator<GlyphHexArrayInput> = (array: GlyphHexArrayInput) => {
  const validator = multiValidation<GlyphHexArrayInput>(
    notNull(validationMessage.cannotBeNull(CoordinateType.Glyphs.toString())), //
    (arr) => onlyAllowedChars('1234567890abcdefABCDEF')(arr.join('')),
    minItems(12),
    maxItems(12)
  );
  return validator(array);
};
 
export const GlyphNumberArrayInputValidator: Validator<GlyphNumberArrayInput> = (numArray: GlyphNumberArrayInput) => {
  const validator = multiValidation<GlyphNumberArrayInput>(
    notNull(validationMessage.cannotBeNull(CoordinateType.Glyphs.toString())), //
    (arr) => onlyAllowedChars('1234567890abcdefABCDEF')(arr.map((num) => num.toString(16)).join('')),
    minItems(12),
    maxItems(12)
  );
  return validator(numArray);
};
 
export const GlyphInputValidator: Validator<GlyphInput> = (inputProps: GlyphInput) => {
  if (inputProps.code == null && inputProps.hexArray == null && inputProps.numberArray == null) {
    return {
      isValid: false,
      errorMessage: validationMessage.atleast1InputIsRequired,
    };
  }
 
  if (inputProps.code != null) {
    return GlyphCodeInputValidator(inputProps.code);
  }
  if (inputProps.hexArray != null) {
    return GlyphHexArrayInputValidator(inputProps.hexArray);
  }
  if (inputProps.numberArray != null) {
    return GlyphNumberArrayInputValidator(inputProps.numberArray);
  }
 
  return { isValid: true };
};