0.0.12 • Published 4 years ago

nestjs-graphql-validator v0.0.12

Weekly downloads
95
License
ISC
Repository
github
Last release
4 years ago

nestjs graphql validator NPM version

Would you like to validate that your string is not longer than 250 characters? Or is acceptable by regex? You can...

Example usage

In your resolver file

import NestjsGraphqlValidator from 'nestjs-graphql-validator'

@Mutation()
@UsePipes(new NestjsGraphqlValidator({
    email: { maxLen: 255, minLen: 10, rules: ['isEmail'] },
}))
public exampleA(
    @Args('email') email: string,
    @Args('name') name: string,
) {
    // ...
    return { email, name }
}

@Mutation()
@UsePipes(new NestjsGraphqlValidator({
    email: { regExp: /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/ },
}))
public exampleB(
    @Args('email') email: string,
) {
    // ...
}

@Query()
@UsePipes(new NestjsGraphqlValidator({
    number: { max: 20 },
}))
public exampleC(
    @Args('email') email: string,
) {
    // ...
}

@Query()
@UsePipes(new NestjsGraphqlValidator({
    data_email: { maxLen: 255, minLen: 10, rules: ['isEmail'] }, // nested ----> email is in object (data.email)
}))
public exampleD(
    @Args('data') data: { email: string, name: string },
) {
    // ...
}

Email field is validated before body of createUserTest is execuded.

In case of error BadRequestException from import { BadRequestException } from '@nestjs/common'the is throwed.

Supported operations

NameType
maxLenstring
minLenstring
minnumber
maxnumber
regExpregular expression
rulesArray of strings
orNullAccept null/undefined value
enumArray of values

(for rules only isEmail is avalible right now)

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.0

4 years ago