2.2.5 • Published 3 years ago

joi-piece-validator v2.2.5

Weekly downloads
16
License
ISC
Repository
github
Last release
3 years ago

joi-piece-validator

  • Helps you validate part of the schema.
  • For middleware function will return the validation error inside the response

Installation

npm install --save joi-piece-validator

Importing

import validateJoiPieces from 'joi-piece-validator'; // ES6
var validateJoiPieces = require('joi-piece-validator'); // ES5 with npm

Requirements

joi

use same joi version

Usage

Validate

const Joi = require("joi")
const { validateJoiPieces } = require('joi-piece-validator')

//your schema
const joiSchema = {
    email: Joi.string()
        .min(5)
        .max(50)
        .required()
        .email()
        .trim(),
    password: Joi.string()
        .min(5)
        .max(50)
        .required(),
    first_name: Joi.string()
        .min(2)
        .max(50)
        .required()
        .label('first name')
        .trim(),
    last_name: Joi.string()
        .min(2)
        .max(50)
        .required()
        .label('last name')
        .trim()
}

//Only email and password to validate
const email = 'test@gmail.com'
const password = '1'
const pieces = { email, password }

//Pass the joi schema and the piece(s) to validate.
const { error } = validateJoiPieces(joiSchema, pieces)
console.log(error)

Validate req.params using validateParams middleware

const Joi = require("joi")
const { validateParams } = require('joi-piece-validator')

const joiSchema = {
    keyword: Joi.string()
        .max(50)
        .required(),
    place_type: Joi.string()
        .max(50)
        .required()
}

//Example of requesting url: http://localhost:3000/api/places/tokyo/region
//This will validate values of params (keyword and place_type)
route.get('/:keyword/:place_type', validateParams(joiSchema), async(req, res, next) => {
    ... await
})

//will return the validation error inside response

Validate req.query using validateQuery middleware

const Joi = require("joi")
const { validateQuery } = require('joi-piece-validator')

const joiSchema = {
    keyword: Joi.string()
        .max(50)
        .required(),
    place_type: Joi.string()
        .max(50)
        .required()
}

//Example of requesting url: http://localhost:3000/api/places?keyword=tokyo&place_type=region
//This will validate values of query (keyword and place_type)
route.get('/', validateQuery(joiSchema), async(req, res, next) => {
    ... await
})

//will return the validation error inside response

Validate req.body using validateBody middleware

const Joi = require("joi")
const { validateBody } = require('joi-piece-validator')

const joiSchema = {
    keyword: Joi.string()
        .max(50)
        .required(),
    place_type: Joi.string()
        .max(50)
        .required()
}

//validateBody(joiSchema, selectively=false)
//selectively=false is the default value
//will validate all items in req.body
route.get('/', validateBody(joiSchema), async(req, res, next) => {
    ... await
})

//validateBody(joiSchema, selectively=true)
//will only use schema items based on the items from req.body
//validate only req.body items that found inside the schema
route.get('/', validateBody(joiSchema, true), async(req, res, next) => {
    ... await
})

//will return the validation error inside response
2.2.5

3 years ago

2.2.4

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.3

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago