Skip to main content

Form Validation Schemas

8.1 campaignFormSchema

Location: src/components/forms/campaignForm/campaign.schema.ts

Purpose: Zod schema for campaign form validation with complex type handling.

Key Validations:

  1. Campaign Name:

    • Minimum 1 character
    • String type
  2. Status:

    • Enum: ["active", "inactive"]
    • Custom error message
  3. Jurisdiction:

    • jurisdictionName - Min 1 character
    • jurisdictionType - Min 1 character
  4. Signature Requirements:

    • requiredSignatures - Positive integer
    • targetSignatures - Positive integer
    • Union type: accepts number or string
    • String transformation: converts to number, truncates decimals
  5. Date Fields:

    • startDate - Coerced to date
    • targetDate - Coerced to date
    • endDate - Optional, nullable, coerced to date
  6. Colors:

    • primaryColor - Hex color regex (#RRGGBB or #RGB)
    • Default: #000000
    • secondaryColor - Hex color regex
    • Default: #FFFFFF
  7. Banner Image:

    • Optional object with:
      • path, id, fullPath, publicURL (required strings)
      • altText (optional string)
  8. Jurisdiction Addition:

    • Enum: ["county", "none"]

Number/String Union:

z.union([
z.number().int().positive(),
z.string().transform((val) => {
const num = Number(val);
return isNaN(num) ? 0 : Math.trunc(num);
}).refine((num) => num > 0, "Must be positive")
])

Notes:

  • Handles form inputs that may be strings or numbers
  • Truncates decimals (no partial signatures)
  • Validates hex colors with regex
  • Coerces dates from strings

8.2 transactionFormSchema

Location: src/components/forms/transactionForm/transaction.schema.ts

Purpose: Zod schema for transaction form with percentage and amount validation.

Key Validations:

  1. Total Amount:

    • Positive number
    • Union type (number or string)
    • Allows decimals
  2. Kitty Percentage (kittyP):

    • Range: 0-100
    • Union type (number or string)
    • Allows decimals
    • Two separate refines for min/max
  3. Dates:

    • datePaid - Coerced to date
    • turnInDate - Coerced to date
  4. Campaign:

    • String, minimum 36 characters (UUID length)
  5. Circulator:

    • Optional, nullable string
    • Minimum 1 character if provided
  6. Type & Method:

    • String types (no specific validation)

Percentage Validation:

z.union([
z.number().min(0).max(100).positive(),
z.string().transform((val) => Number(val))
.refine((num) => num >= 0, "Must be at least 0")
.refine((num) => num <= 100, "Must be at most 100")
])

Notes:

  • Percentage allows 0 (unlike amount which must be positive)
  • Handles string inputs from form fields
  • Validates UUID length for campaign ID