All files / src/validation/address cityValidator.ts

100% Statements 8/8
90.9% Branches 10/11
100% Functions 1/1
100% Lines 8/8

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                                      2149x   2149x     2149x 784x             2149x 581x 581x     2149x    
import { VALIDATION_MESSAGES } from "@validation/messages";
import * as Yup from "yup";
 
interface CityValidationOptions {
  required?: boolean;
  nullable?: boolean;
  maxLength?: number;
  customRequiredMessage?: string;
}
// countryValidator.ts
 
export function createCityValidation(
  options: CityValidationOptions = {},
): Yup.StringSchema<string | null | undefined> {
  const {
    required = false,
    nullable = false,
    maxLength,
    customRequiredMessage,
  } = options;
 
  let schema = nullable ? Yup.string().nullable() : Yup.string();
 
  // Apply max length validation
  if (maxLength !== undefined) {
    schema = schema.max(
      maxLength,
      VALIDATION_MESSAGES.CITY_MAX_LENGTH.replace("{{max}}", String(maxLength)),
    );
  }
 
  // Apply required validation (should be last)
  if (required) {
    const message = customRequiredMessage || VALIDATION_MESSAGES.REQUIRED;
    schema = schema.required(message);
  }
 
  return schema;
}