From 322d118ee522d7b3a6843e0c00c4da627da31283 Mon Sep 17 00:00:00 2001 From: Richard Fontein <32132657+rifont@users.noreply.github.com> Date: Fri, 8 Nov 2024 19:13:35 +0100 Subject: [PATCH] test(validator): extract class-validator fixtures --- .../fixures/class-validator.fixtures.ts | 30 ++++++++++++++++++ .../src/validators/validator.test.ts | 31 ++----------------- 2 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 packages/framework/src/validators/fixures/class-validator.fixtures.ts diff --git a/packages/framework/src/validators/fixures/class-validator.fixtures.ts b/packages/framework/src/validators/fixures/class-validator.fixtures.ts new file mode 100644 index 00000000000..6f158ddc4c7 --- /dev/null +++ b/packages/framework/src/validators/fixures/class-validator.fixtures.ts @@ -0,0 +1,30 @@ +import 'reflect-metadata'; +import { IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator'; +import { Type } from 'class-transformer'; + +export class SimpleStringSchema { + @IsString() + @IsOptional() + name?: string; +} + +class NestedChildrenSchema { + @IsNumber() + age!: number; +} + +export class NestedSchema { + @IsString() + name!: string; + + @ValidateNested() + @Type(() => NestedChildrenSchema) + nested!: NestedChildrenSchema; +} + +export class SimpleStringAndNumberSchema { + @IsString() + name!: string; + @IsNumber() + age!: number; +} diff --git a/packages/framework/src/validators/validator.test.ts b/packages/framework/src/validators/validator.test.ts index a99b4602438..72e3648b264 100644 --- a/packages/framework/src/validators/validator.test.ts +++ b/packages/framework/src/validators/validator.test.ts @@ -1,38 +1,11 @@ import { describe, it, expect } from 'vitest'; import { ZodSchema, z } from 'zod'; -import { IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator'; -import { Type } from 'class-transformer'; import { validateData, transformSchema } from './base.validator'; import { ClassType, JsonSchema, Schema } from '../types/schema.types'; -import 'reflect-metadata'; +import { SimpleStringSchema, NestedSchema, SimpleStringAndNumberSchema } from './fixures/class-validator.fixtures'; const schemas = ['zod', 'class', 'json'] as const; -// Definitions of class-validator schemas -class SimpleStringSchema { - @IsString() - @IsOptional() - name?: string; -} -class NestedChildrenSchema { - @IsNumber() - age!: number; -} -class NestedSchema { - @IsString() - name!: string; - - @ValidateNested() - @Type(() => NestedChildrenSchema) - nested!: NestedChildrenSchema; -} -class SimpleStringAndNumberSchema { - @IsString() - name!: string; - @IsNumber() - age!: number; -} - describe('validators', () => { describe('validateData', () => { type ValidateDataTestCase = { @@ -316,6 +289,7 @@ describe('validators', () => { success: false, errors: { zod: [{ message: 'Expected number, received string', path: '/numVal' }], + class: null, // ClassValidator has no support for `anyOf` /* * TODO: use discriminator to get the correct error message. * @@ -330,7 +304,6 @@ describe('validators', () => { * * @see https://ajv.js.org/json-schema.html#discriminator */ - class: null, // ClassValidator has no support for `anyOf` json: [ { message: "must have required property 'stringVal'",