feat: add Joi-based client-side payload validation to reduce 422 errors #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Joi-based Client-Side Payload Validation
Overview
This PR introduces comprehensive client-side payload validation using Joi to catch malformed requests before they reach the API. This significantly reduces 422 errors and improves the developer experience with clear, actionable error messages.
Problem Statement
Previously, invalid payloads were sent to the server, resulting in 422 errors. This created unnecessary API calls, server load, and poor developer experience. Manual validation checks were scattered throughout the codebase and inconsistent.
Solution
Implemented a centralized validation layer using Joi schemas that:
Changes Made
New Files
src/validation/schemas.ts- Joi schemas for all payload typessrc/validation/validator.ts- Validation utility functionvalidatePayload()function with full error collectionsrc/validation/index.ts- Barrel export for clean importsModified Files
src/classes/client.ts- Integrated validation into 9 methodscreateCustomer()- validates customer dataupdateCustomer()- validates customer updatescreateProduct()- validates product creationupdateProduct()- validates product updatescreatePrice()- validates price dataupdatePrice()- validates price updatescreateCheckout()- validates checkout with mutual exclusivity rulecreatePaymentLink()- validates payment link creationupdatePaymentLink()- validates payment link updatessrc/index.ts- Added validation module exportpackage.json- Addedjoi@^17.11.0dependencyValidation Coverage
Type Validation
Format Validation
Enum Validation
'ar' | 'en' | 'fr'Nested Validation
Cross-Field Validation
itemsarray OR (amount+currency), not bothError Message Examples
Testing
All validation scenarios tested:
Build Status
Breaking Changes
None. This is a purely additive feature that validates payloads before network calls. Existing code continues to work unchanged.
Migration Guide
No migration needed. Validation is automatic and transparent. Users will see better error messages when payloads are invalid.
Benefits
Related Issues
Addresses the need for client-side validation to reduce 422 errors and improve error handling.
Checklist