Skip to content

Commit

Permalink
test(validator): extract class-validator fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Nov 8, 2024
1 parent ed7c857 commit 322d118
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 2 additions & 29 deletions packages/framework/src/validators/validator.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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.
*
Expand All @@ -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'",
Expand Down

0 comments on commit 322d118

Please sign in to comment.