nestjs-class-validator-error-handler

1.0.1 • Public • Published

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

Author

Gaston Morixe

Package Sidebar

Install

npm i nestjs-class-validator-error-handler

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

14.9 kB

Total Files

10

Last publish

Collaborators

  • gastonmorixe