Files
zod-backend/src/core/utils/class-validator-formatter.util.spec.ts
Oracle Public Cloud User 05872b5170 feat:mvp1 initial commit
2024-11-21 06:07:08 +00:00

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);
});
});