mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-10 15:17:44 +00:00
123 lines
2.9 KiB
TypeScript
123 lines
2.9 KiB
TypeScript
import { ValidationError } from '@nestjs/common';
|
|
import { IFieldError } from '../interfaces';
|
|
import { formatClassValidatorErrors } from './class-validator-formatter.util';
|
|
|
|
describe('formatClassValidatorErrors', () => {
|
|
test('should return empty array when given empty array', () => {
|
|
expect(formatClassValidatorErrors([])).toEqual([]);
|
|
});
|
|
|
|
test('should format single error object correctly', () => {
|
|
const errors: ValidationError = {
|
|
property: 'username',
|
|
constraints: {
|
|
isNotEmpty: 'Username should not be empty',
|
|
},
|
|
};
|
|
|
|
const expectedOutput: IFieldError[] = [
|
|
{
|
|
field: 'username',
|
|
message: 'Username should not be empty',
|
|
},
|
|
];
|
|
|
|
expect(formatClassValidatorErrors(errors)).toEqual(expectedOutput);
|
|
});
|
|
|
|
test('should format multiple error objects correctly', () => {
|
|
const errors: ValidationError[] = [
|
|
{
|
|
property: 'username',
|
|
constraints: {
|
|
isNotEmpty: 'Username should not be empty',
|
|
},
|
|
// no children list
|
|
},
|
|
{
|
|
property: 'password',
|
|
constraints: {
|
|
isNotEmpty: 'Password should not be empty',
|
|
},
|
|
children: [], // empty children list
|
|
},
|
|
];
|
|
|
|
const expectedOutput: IFieldError[] = [
|
|
{
|
|
field: 'username',
|
|
message: 'Username should not be empty',
|
|
},
|
|
{
|
|
field: 'password',
|
|
message: 'Password should not be empty',
|
|
},
|
|
];
|
|
|
|
expect(formatClassValidatorErrors(errors)).toEqual(expectedOutput);
|
|
});
|
|
|
|
test('should format nested error objects correctly', () => {
|
|
const errors: ValidationError[] = [
|
|
{
|
|
property: 'username',
|
|
constraints: {
|
|
isNotEmpty: 'Username should not be empty',
|
|
},
|
|
},
|
|
{
|
|
property: 'address',
|
|
constraints: {},
|
|
children: [
|
|
{
|
|
property: 'city',
|
|
constraints: {
|
|
isNotEmpty: 'City should not be empty',
|
|
},
|
|
},
|
|
{
|
|
property: 'street',
|
|
constraints: {
|
|
isNotEmpty: 'Street should not be empty',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
const expectedOutput: IFieldError[] = [
|
|
{
|
|
field: 'username',
|
|
message: 'Username should not be empty',
|
|
},
|
|
{
|
|
field: 'address.city',
|
|
message: 'City should not be empty',
|
|
},
|
|
{
|
|
field: 'address.street',
|
|
message: 'Street should not be empty',
|
|
},
|
|
];
|
|
|
|
expect(formatClassValidatorErrors(errors)).toEqual(expectedOutput);
|
|
});
|
|
|
|
test('should format error object without constraints', () => {
|
|
const errors: ValidationError[] = [
|
|
{
|
|
property: 'username',
|
|
},
|
|
];
|
|
|
|
const expectedOutput: IFieldError[] = [
|
|
{
|
|
field: 'username',
|
|
message: '',
|
|
},
|
|
];
|
|
|
|
expect(formatClassValidatorErrors(errors)).toEqual(expectedOutput);
|
|
});
|
|
});
|