Home > OS >  class-validator - Validate that an array of strings matches a value
class-validator - Validate that an array of strings matches a value

Time:04-24

I'm using the class-validator npm package and would like to validate that the type property of request body matches one either 'organization' or 'student'.

How would I do this with this package?

This doesn't work:

@IsIn(['organization', 'student'])
type: string

Request body example:

{
   "type": "organization",
   "email": "[email protected]",
   "password": "veryscretpwd"
}

CodePudding user response:

You can use something like this in your model class.

{
        "type": {
                type: String,
                enum: ['organization', 'student'],
                required: [true, 'Type is required.']
        },
        "email": ...
}

Instead of @IsIn use enum.

  • Related