Validation Error
https://bessapay.net/errors/validation-errorOverview
A Validation Error occurs when the request data fails to meet the required validation rules. Unlike a generic Bad Request error, validation errors provide detailed information about exactly which fields failed validation and why.
Details
- Error Code: VALIDATION_ERROR
- HTTP Status: 400
- Error Type: Client Error
Common scenarios
- • Field values don't match required format
- • Field values exceed min/max length constraints
- • Required fields are missing
- • Values don't meet business rule requirements
- • Nested objects fail schema validation
Example
1{
2 "type": "https://bessapay.net/errors/validation-error",
3 "title": "Validation Error",
4 "status": 400,
5 "detail": "The request contains invalid field values",
6 "instance": "/api/v1/users",
7 "errorCode": "VALIDATION_ERROR",
8 "timestamp": "2023-06-15T15:30:22Z",
9 "errors": [
10 {
11 "field": "email",
12 "message": "Must be a valid email address",
13 "rejectedValue": "not-an-email"
14 },
15 {
16 "field": "password",
17 "message": "Password must be at least 8 characters long"
18 },
19 {
20 "field": "phoneNumber",
21 "message": "Phone number must match pattern +XX-XXXX-XXXX",
22 "rejectedValue": "123456"
23 }
24 ]
25}How to Fix
Check field requirements
Review the API documentation for each field's requirements including data types, formats, patterns, and constraints.
Fix each validation error
The errors array in the response provides specific information about each invalid field. Address each error individually based on the feedback provided.
Implement client-side validation
To provide better user experience, implement client-side validation that matches the server's validation rules before submitting requests.
Use schema validation libraries
Consider using schema validation libraries like Joi, Yup, or JSON Schema to validate your request data before sending it to the API.
Code Example: Handling Validation Errors
1// Example of handling Validation errors
2async function createUser(userData) {
3 try {
4 const response = await fetch('https://api.semuni.com/v1/users', {
5 method: 'POST',
6 headers: {
7 'Authorization': `Bearer ${apiKey}`,
8 'Content-Type': 'application/json'
9 },
10 body: JSON.stringify(userData)
11 });
12
13 if (!response.ok) {
14 const errorData = await response.json();
15
16 if (errorData.type === 'https://bessapay.net/errors/validation-error') {
17 console.error('Validation error:', errorData);
18
19 // Create a map of field errors for easy access in UI
20 const fieldErrors = {};
21
22 if (errorData.errors && Array.isArray(errorData.errors)) {
23 errorData.errors.forEach(error => {
24 if (error.field) {
25 fieldErrors[error.field] = error.message;
26 }
27 });
28 }
29
30 // Update form UI to show validation errors
31 displayValidationErrors(fieldErrors);
32 return { success: false, fieldErrors };
33 }
34
35 // Handle other error types
36 throw new Error(errorData.detail || 'An error occurred');
37 }
38
39 const data = await response.json();
40 return { success: true, data };
41 } catch (error) {
42 console.error('Error creating user:', error);
43 throw error;
44 }
45}