All files / src/validation/schemas address.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 0/0
100% Lines 4/4

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                              145x                     145x                                   145x           145x  
import * as yup from "yup";
import { VALIDATION_MESSAGES } from "@validation/messages";
 
/**
 * Canonical Address Schema
 *
 * This is the single source of truth for address validation across the application.
 * All forms should use this schema (or extend it) for address fields.
 *
 * Structure: Always nested under an "address" key
 * Example: { address: { line1, city, state, zip, country } }
 *
 * @see src/utils/address/normalize.ts for converting external data to this shape
 * @see src/utils/address/serialize.ts for converting to API-specific formats
 */
export const AddressSchema = yup.object({
  line1: yup.string().required(VALIDATION_MESSAGES.ADDRESS_REQUIRED),
  city: yup.string().required(VALIDATION_MESSAGES.CITY_REQUIRED),
  state: yup.string().nullable(),
  zip: yup.string().nullable(),
  country: yup.string().required(VALIDATION_MESSAGES.COUNTRY_REQUIRED),
});
 
/**
 * Optional Address Schema (for forms where address is not required)
 */
export const OptionalAddressSchema = yup.object({
  line1: yup.string().optional(),
  city: yup.string().optional(),
  state: yup.string().nullable(),
  zip: yup.string().nullable(),
  country: yup.string().optional(),
});
 
/**
 * Canonical Address Type
 * Inferred directly from the Yup schema to ensure perfect alignment
 */
export type AddressType = yup.InferType<typeof AddressSchema>;
 
/**
 * For forms that include an address field
 * Usage: const FormSchema = yup.object({ address: AddressFieldSchema })
 */
export const AddressFieldSchema = AddressSchema.required();
 
/**
 * For optional address fields
 * Usage: const FormSchema = yup.object({ address: OptionalAddressFieldSchema })
 */
export const OptionalAddressFieldSchema = OptionalAddressSchema.optional();