paak
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

paak

Build Status Coverage Status

Getting Started

  npm i paak

Examples

Front-end (React-form)

import { v } from 'paak'
 
class SomeComponent extends React.component {
  render () {
    return (
        <SomeField 
          label='First Name'
          name='firstName'
          validate={[
            v.string('First name must be a text'),
            v.required('First name is required'),
          ]}
        />    
    )    
  }
}

Back-end (Express/Router/Middleware)

In this example, we are using paak to validate body in REST API.

some-router.js

import { v, n, validate } from 'paak'
 
// schema for this specific route
const schemaForSomeRoute = {
  firstName: [
    v.string('First name must be a text'),
    v.required('First name is required'),
    n.trim()
  ],
  lastName: [
    v.string('Last name must be a text'),
    v.required('Last name is required'),
    n.trim()
  ],
  interests: [
    v.array(
      'Interest must be a collection',
      v.object({
        name: [
          v.string('Name of an interest must be a string')
        ]
      })
    )
  ]
}
 
export const validateBody = (schema: any) => {
  return async (req: Request, res: Response, next: Function) => {
    
    // will return undefined if there is no error
    const hasError = validate(req.body, schema)
  
    if (hasError) {
      res.status(400).json({ error: hasError })
    } else {
      next()
    }    
  }
}
 
router.route('/')
  .post(
    validateBody(schemaForSomeRoute),
    SomeController.create
  )

Package Sidebar

Install

npm i paak

Weekly Downloads

1

Version

1.0.5

License

ISC

Unpacked Size

452 kB

Total Files

59

Last publish

Collaborators

  • minnnam