@exobase/use-json-body
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-rc.25 • Public • Published

title: 'useJsonBody' description: 'A hook to parse and validate a request body' group: 'Hooks' badge: 'Validation'

Provides a hook to parse and validate request arguments from the json body.

Install

yarn add @exobase/use-json-body
# or
yarn add @exobase/hooks

Import

import { useJsonBody } from '@exobase/use-json-body'
// or
import { useJsonBody } from '@exobase/hooks'

Usage

If you're writing Exobase endpoints in Typescript you'll want to import the Props type.

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useJsonBody } from '@exobase/use-json-body'
import { useLambda } from '@exobase/use-lambda'

type Args = {
  username: string
  password: string
}

const createAccount = async ({ args }: Props) => {
  // await db.users.add({
  //   username: args.username,
  //   password: args.password
  // })
}

const STRONG = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/

export default compose(
  useLambda(),
  useJsonBody(z => ({
    username: z.string(),
    password: z.string().refine(p => STRONG.test(p), {
      message: 'Password is too weak'
    })
  })),
  createAccount
)

Dependencies (2)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @exobase/use-json-body

    Weekly Downloads

    71

    Version

    1.0.0-rc.25

    License

    MIT

    Unpacked Size

    20.4 kB

    Total Files

    17

    Last publish

    Collaborators

    • rayepps