All files / src/validation composites.ts

62.5% Statements 5/8
16.66% Branches 1/6
33.33% Functions 1/3
71.42% Lines 5/7

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                      9x                                                                                   9x 184x   184x                                                 184x                            
/**
 * Composite Validation Schemas
 *
 * Complex validation schemas that combine multiple field validators
 *
 * @module validation/composites
 */
 
import * as Yup from "yup";
import { VALIDATION_MESSAGES } from "@validation/messages";
import { createStreetAddressValidator } from "@validation/address/streetValidator";
import { buildZipSchema } from "@validation/address/zipValidator";
import { createCountryValidator } from "@validation/address/countryValidator";
import { createStateValidator } from "@validation/address/stateValidator";
import { createCityValidation } from "@validation/address/cityValidator";
 
/**
 * Non-resident inputs validation schema
 * Used for collecting citizenship and residency information
 *
 * @example
 * ```typescript
 * const schema = Yup.object({
 *   ...createNonResidentInputsSchema()
 * });
 * ```
 */
export const createNonResidentInputsSchema = () => ({
  isNotUSResident: Yup.boolean(),
 
  isUSResident: Yup.boolean(),
  isUSCitizen: Yup.boolean(),
 
  citizenship: Yup.string().when(["isUSResident", "isUSCitizen"], {
    // Require only when NOT a US resident AND NOT a US citizen
    is: (isUSResident: boolean, isUSCitizen: boolean) =>
      !isUSResident && !isUSCitizen,
    then: Yup.string().required("Country of Citizenship is required"),
    otherwise: Yup.string().notRequired(), // explicitly not required
  }),
  isNotResidentInCitizenshipCountry: Yup.boolean(),
  countryOfResidence: Yup.string().when("isUSResident", {
    is: false,
    then: Yup.string().required("Country of Residency is required"),
    otherwise: Yup.string().notRequired().nullable(),
  }),
});
 
/**
 * Address validation schema options
 */
export interface AddressSchemaOptions {
  required?: boolean;
}
 
/**
 * Creates a complete address validation schema
 *
 * @param options - Configuration options
 * @param options.required - Whether all address fields are required
 * @returns Yup object schema for address validation
 *
 * @example
 * ```typescript
 * const schema = Yup.object({
 *   address: createAddressSchema({ required: true })
 * });
 * ```
 */
export const createAddressSchema = (options: AddressSchemaOptions = {}) => {
  const { required = true } = options;
 
  Iif (required) {
    return Yup.object().shape({
      country: createCountryValidator({
        required: true,
        message: VALIDATION_MESSAGES.REQUIRED_SHORT,
      }),
      line1: createStreetAddressValidator({
        required: true,
        customRequiredMessage: VALIDATION_MESSAGES.REQUIRED_SHORT,
      }),
      city: createCityValidation({
        required: true,
        customRequiredMessage: VALIDATION_MESSAGES.REQUIRED_SHORT,
      }),
      state: createStateValidator({
        required: true,
        customRequiredMessage: VALIDATION_MESSAGES.REQUIRED_SHORT,
      }),
      zip: buildZipSchema({
        required: true,
        allowNull: false,
      }),
    });
  }
 
  return Yup.object().shape({
    country: createCountryValidator({
      required: false,
      nullable: true,
    }),
    line1: createStreetAddressValidator({ required: false }),
    city: createCityValidation({ required: false }),
    state: createStateValidator({ required: false }),
    zip: buildZipSchema({
      required: false,
      allowNull: true,
    }),
  });
};