Skip to content

Conversation

@aaronaco
Copy link
Contributor

@aaronaco aaronaco commented Jan 8, 2026

Description

This PR adds comprehensive server-side validation for petition creation and updates, along with a complete unit test suite.

Closes #28

AI-Assisted Work Disclosure

This PR was developed with assistance from an AI tool for:

  • Unit test structure and test cases: Guidance on organizing the test suite and identifying critical edge cases.
  • Documentation updates: Help with formatting and clarifying project documentation for better readability.

Type of Change

  • New feature
  • Bug fix
  • Tests
  • Documentation

Changes

1. Server-Side Validation (Zod v4)

  • Added validation schemas in functions/_shared/schemas.ts:

    • createPetitionSchema - Validates petition creation requests
    • updatePetitionSchema - Validates petition update requests
    • formatZodError - Formats Zod validation errors for API responses
  • Implemented validation in API handlers:

    • POST /api/petitions - Validates creation requests with detailed error messages
    • PUT /api/petitions/[id] - Validates update requests with partial update support
  • Validation Rules:

    • Title: 10-150 characters, required
    • Description: min 100 characters, required
    • Type: enum ("local" | "national"), required
    • Location: required for local petitions
    • Target count: 1 to 1,000,000, integer, defaults to 1000
    • Category IDs: array of numbers (optional)
    • Status: enum ("active" | "completed" | "closed") for updates

2. Unit Tests (Vitest)

  • Created test suite with 42 tests total:

    • tests/unit/create-petition.test.ts (27 tests)
    • tests/unit/update-petition.test.ts (13 tests)
    • tests/unit/setup.test.ts (2 tests)
  • Test Configuration:

    • Added vitest.config.ts:
    • Excluded e2e directory from Vitest (E2E tests use Playwright)
    • Ensured proper test isolation

3. Documentation

  • Updated tests/README.md:
    • Added unit tests section
    • Documented test files and coverage
    • Added instructions for running tests

Testing

pnpm test:unit
# ✓ 42 tests passing

Test Coverage:

  • Happy path scenarios (valid data)
  • Sad path scenarios (invalid data with error messages)
  • Edge cases (whitespace trimming, boundary conditions)
  • Error message formatting validation

Checklist

  • Follow existing naming conventions
  • Code follows project style and linting rules
  • Self-review completed
  • Tests added or updated where appropriate
  • Both success and error scenarios are covered by tests
  • Tests can run independently and in isolation
  • All tests pass locally
  • Documentation updated where applicable
  • No breaking changes to the public API

@aaronaco
Copy link
Contributor Author

aaronaco commented Jan 8, 2026

The CI lint check is failing due to pre-existing formatting issues in files unrelated to this PR. My changes are properly formatted. Should I fix these in this PR or should they be addressed separately?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security/Bug] Missing server-side validation on petition creation endpoint (POST /api/petitions)

1 participant