Skip to content

Extract the duplicated positive integer field validation into a shared helper#187

Merged
mikewheeleer merged 3 commits into
Agentpay-Org:mainfrom
ekwe7:Extract-the-duplicated-positive-integer-field-validation-into-a-shared-helper
Jun 27, 2026
Merged

Extract the duplicated positive integer field validation into a shared helper#187
mikewheeleer merged 3 commits into
Agentpay-Org:mainfrom
ekwe7:Extract-the-duplicated-positive-integer-field-validation-into-a-shared-helper

Conversation

@ekwe7

@ekwe7 ekwe7 commented Jun 27, 2026

Copy link
Copy Markdown

closes #96

Summary

This PR consolidates the repeated numeric validation logic used across multiple forms into a single reusable utility. The new helper provides a consistent, typed interface for validating integer inputs while preserving the existing validation rules and user-facing behavior.

The change reduces duplication, improves maintainability, and increases confidence through dedicated unit tests.

Problem

Numeric validation was implemented independently in multiple locations:

  • src/app/services/new/page.tsx
  • src/app/services/[serviceId]/edit/page.tsx
  • src/app/usage/page.tsx

Although the validation requirements differ slightly between forms, the underlying parse-and-validate logic was largely duplicated.

This duplication increases maintenance overhead and creates the risk of validation behavior drifting over time.

Solution

Introduced a shared validation utility:

src/lib/validateNumber.ts

The helper exposes typed validation functions that return structured results:

{ ok: true, value: number }

or

{ ok: false, message: string }

This approach centralizes validation logic while allowing each form to preserve its existing acceptance criteria.

Changes

New Validation Utility

Created:

  • src/lib/validateNumber.ts

Added:

  • parseNonNegativeInt()
  • parsePositiveInt()

Features:

  • Typed validation results
  • Consistent error messages
  • Reusable validation behavior across forms
  • JSDoc documentation

Form Updates

Updated:

  • src/app/services/new/page.tsx
  • src/app/services/[serviceId]/edit/page.tsx
  • src/app/usage/page.tsx

Changes include:

  • Replacing inline validation logic with shared helper functions

  • Preserving existing accepted ranges:

    • Price (priceStroops): >= 0
    • Requests: >= 1
  • Maintaining existing submission behavior and payload structure

  • Surfacing validation errors through existing TextField error props

Testing

Added:

  • src/lib/__tests__/validateNumber.test.ts

Updated affected page tests to verify:

  • Shared validation integration
  • Existing validation behavior remains unchanged
  • Error states render correctly

Edge Cases Covered

Validation tests include:

  • Empty input
  • Negative values
  • Floating-point values
  • Leading-zero values
  • Valid non-negative integers
  • Valid positive integers
  • Boundary values for each accepted range

Validation

Executed:

npm run lint
npm run typecheck
npm test
npm run build

Acceptance Criteria

  • Shared validation helper created
  • Typed validation result structure implemented
  • Service creation form uses shared helper
  • Service edit form uses shared helper
  • Usage form uses shared helper
  • Existing accepted ranges preserved
  • Validation messages surfaced through TextField error props
  • Wire payloads unchanged
  • Unit tests added for helper
  • Edge cases covered
  • JSDoc documentation added
  • README updated with validation rules

Benefits

  • Eliminates duplicated validation code
  • Ensures consistent validation behavior across forms
  • Simplifies future maintenance and enhancements
  • Improves testability through isolated validation utilities
  • Reduces risk of validation regressions
  • Keeps form behavior unchanged for end users

@mikewheeleer

Copy link
Copy Markdown
Contributor

solid — in it goes 🚀

@mikewheeleer mikewheeleer merged commit f5938a7 into Agentpay-Org:main Jun 27, 2026
1 check failed
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.

Extract the duplicated positive-integer field validation into a shared helper

3 participants