Skip to main content

Custom validation

Query, reqHeaders and reqBody are validated by specifying Class with class-validator.
The class needs to be exported from server/validators/index.ts.

server/validators/index.ts
import { MinLength, IsString } from 'class-validator'
export class LoginBody {
@MinLength(5)
id: string
@MinLength(8)
pass: string
}
export class TokenHeader {
@IsString()
@MinLength(10)
token: string
}
server/api/token/index.ts
import { LoginBody, TokenHeader } from '$/validators'
export type Methods = {
post: {
reqBody: LoginBody
resBody: {
token: string
}
}
delete: {
reqHeaders: TokenHeader
}
}

Results

$ curl -X POST -H "Content-Type: application/json" -d '{"id":"correctId","pass":"correctPass"}' http://localhost:8080/api/token
{"token":"XXXXXXXXXX"}
$ curl -X POST -H "Content-Type: application/json" -d '{"id":"abc","pass":"12345"}' http://localhost:8080/api/token -i
HTTP/1.1 400 Bad Request
$ curl -X POST -H "Content-Type: application/json" -d '{"id":"incorrectId","pass":"incorrectPass"}' http://localhost:8080/api/token -i
HTTP/1.1 401 Unauthorized

Passing options (>= v0.20.0)

server/service/app.ts
import Fastify from 'fastify'
import server from './$server'
const fastify = Fastify()
server(fastify, { basePath: '/api/v1', validator: { whitelist: true } })
fastify.listen(3000)

class-validator options