@gp_jcisneros/errors
Version:
Error handling utilities for GreenPay microservices and validation middleware
138 lines (117 loc) • 5.59 kB
JavaScript
const { ValidationError } = require('../src/ValidationError');
describe('ValidationError', () => {
test('should create a required field error', () => {
const error = ValidationError.required('email');
expect(error.message).toBe('email is required');
expect(error.statusCode).toBe(400);
expect(error.name).toBe('ValidationError');
expect(error.type).toBe('VALIDATION_ERROR');
expect(error.field).toBe('email');
expect(error.value).toBe(null);
expect(error.errorCode).toBe('VALIDATION_EMAIL');
expect(error.description).toBe('email is required');
expect(error.integration).toBe('validation-service');
});
test('should create an invalid format error', () => {
const error = ValidationError.invalidFormat('phone', 'XXX-XXX-XXXX', '123');
expect(error.message).toBe('phone must be in format: XXX-XXX-XXXX');
expect(error.field).toBe('phone');
expect(error.value).toBe('123');
expect(error.errorCode).toBe('VALIDATION_PHONE');
expect(error.description).toBe('phone must be in format: XXX-XXX-XXXX');
expect(error.integration).toBe('validation-service');
expect(error.statusCode).toBe(400);
});
test('should create a minimum length error', () => {
const error = ValidationError.minLength('password', 8, '123');
expect(error.message).toBe('password must be at least 8 characters long');
expect(error.field).toBe('password');
expect(error.value).toBe('123');
expect(error.errorCode).toBe('VALIDATION_PASSWORD');
expect(error.description).toBe('password must be at least 8 characters long');
expect(error.integration).toBe('validation-service');
expect(error.statusCode).toBe(400);
});
test('should create a maximum length error', () => {
const error = ValidationError.maxLength('name', 50, 'Very long name');
expect(error.message).toBe('name must be no more than 50 characters long');
expect(error.field).toBe('name');
expect(error.value).toBe('Very long name');
expect(error.errorCode).toBe('VALIDATION_NAME');
expect(error.description).toBe('name must be no more than 50 characters long');
expect(error.integration).toBe('validation-service');
expect(error.statusCode).toBe(400);
});
test('should create an invalid email error', () => {
const error = ValidationError.invalidEmail('email', 'invalid-email');
expect(error.message).toBe('email must be a valid email address');
expect(error.field).toBe('email');
expect(error.value).toBe('invalid-email');
expect(error.errorCode).toBe('VALIDATION_EMAIL');
expect(error.description).toBe('email must be a valid email address');
expect(error.integration).toBe('validation-service');
expect(error.statusCode).toBe(400);
});
test('should create a custom field error', () => {
const error = ValidationError.forField('age', 'Age must be a number', 'abc');
expect(error.message).toBe('Age must be a number');
expect(error.field).toBe('age');
expect(error.value).toBe('abc');
expect(error.errorCode).toBe('VALIDATION_AGE');
expect(error.description).toBe('Age must be a number');
expect(error.integration).toBe('validation-service');
expect(error.statusCode).toBe(400);
});
test('should create error without field', () => {
const error = new ValidationError('General validation error');
expect(error.message).toBe('General validation error');
expect(error.field).toBe(null);
expect(error.value).toBe(null);
expect(error.errorCode).toBe('VALIDATION_GENERAL');
expect(error.description).toBe('General validation error');
expect(error.integration).toBe('validation-service');
expect(error.statusCode).toBe(400);
});
test('should get validation details', () => {
const error = ValidationError.required('email');
const details = error.getValidationDetails();
expect(details.field).toBe('email');
expect(details.value).toBe(null);
expect(details.message).toBe('email is required');
expect(details.type).toBe('VALIDATION_ERROR');
expect(details.errorCode).toBe('VALIDATION_EMAIL');
expect(details.description).toBe('email is required');
expect(details.integration).toBe('validation-service');
});
test('should have required fields', () => {
const error = ValidationError.required('email');
expect(error.hasRequiredFields()).toBe(true);
expect(error.getRequiredFields()).toEqual({
errorCode: 'VALIDATION_EMAIL',
description: 'email is required',
integration: 'validation-service'
});
});
test('should convert to JSON', () => {
const error = ValidationError.invalidEmail('email', 'invalid-email');
const json = error.toJSON();
expect(json.name).toBe('ValidationError');
expect(json.message).toBe('email must be a valid email address');
expect(json.statusCode).toBe(400);
expect(json.errorCode).toBe('VALIDATION_EMAIL');
expect(json.description).toBe('email must be a valid email address');
expect(json.integration).toBe('validation-service');
expect(json.timestamp).toBeDefined();
expect(json.stack).toBeDefined();
});
test('should handle special characters in field names', () => {
const error = ValidationError.required('user_email');
expect(error.errorCode).toBe('VALIDATION_USER_EMAIL');
});
test('should handle null and undefined values', () => {
const error = new ValidationError('Test error', null, null);
expect(error.field).toBe(null);
expect(error.value).toBe(null);
expect(error.errorCode).toBe('VALIDATION_GENERAL');
});
});