@bubojs/validation
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Fastest Validator

fastest validator

Back to Main Menu

This route validation middleware is built around fatestValidator feel free to look at the documentation to learn more

validation scheme

You can use the typing provided by FastestValidator which allows you to create a typed schema for fastest ex :

const userCreateValidations: FastestValidator<User> = {
    schema: {
      $$async: true,
      username: { type: 'string', min: 3, max: 255 },
      email: { type: 'email', normalize: true },
      password: this.passwordValidation,
      birthDate: {
        optional: true,
        type: 'date',
        convert: true
      },
      bio: { type: 'string', optional: true }
    },
    validatorOptions: {
      useNewCustomCheckerFunction: true, // using new version
      messages: {
        // Register our new error message text
        atLeastOneLetter: 'The pass value must contain at least one letter from a-z ranges!',
        atLeastOneUpperCaseLetter: 'The pass value must contain at least one letter from A-Z ranges!',
        atLeastOneDigit: 'The pass value must contain at least one digit from 0 to 9!',
        atLeastOneSpecialCharacter: 'The pass value must contain at least one special character!',
        emailNotAvailable: `The email is already registered!`
      }
    }
  }

Adding the schema to the route

once the schema is created, it must be added to the route to be validated, there is a decorator for that:

import { Body, Controller, Post } from '@bubojs/api'
import { AuthMiddleware, ValidationMiddleware } from '@bubojs/catalog'

@Controller()
class AuthController {
    @ValidationMiddleware(userCreateValidations)
    @Post('/signup')
    async signup(@Body() body: any) {
        ...
    }
}

so the scheme is added to the route

Back to Main Menu

Editor

Readme

Keywords

none

Package Sidebar

Install

npm i @bubojs/validation

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

6.88 kB

Total Files

8

Last publish

Collaborators

  • flowlie
  • barthowlie
  • arnowlie
  • jordanowlie