nestjs-class-validator-error-handler
GraphQL Error handler for NestJS models using class-validator
Usage
Your model must
- Extend it from the
Model
class - Enable
class-validator
by using the decorator@ValidateSchema()
on the class - Export the schema using
YourModel.schema
property
Example User.model.ts
import * as DB from '@nestjs/mongoose'
import * as GQL from '@nestjs/graphql'
import * as mongoose from 'mongoose'
import * as V from 'class-validator'
import { ValidateSchema } from 'nestjs-class-validator-error-handler'
@GQL.ObjectType()
@DB.Schema({
timestamps: true,
})
@ValidateSchema()
export class UserModel extends Model {
@GQL.Field(() => GQL.ID)
_id: mongoose.Types.ObjectId
@GQL.Field(() => String, { nullable: false })
@DB.Prop({
required: true,
})
@V.Contains('Hello')
username: string
}
export type UserModelDocument = UserModel & mongoose.Document
export const UserModelSchema = UserModel.schema
Licence
MIT