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

0.1.0 • Public • Published

safe-form

⚡️ End-to-end type-safety from client to server. Inspired by react-hook-form and next-safe-action.

Features

  • ✅ Ridiculously easy to use
  • ✅ 100% type-safe
  • ✅ Input validation using zod
  • ✅ Server error handling
  • ✅ Automatic input binding

Requirements

Install

npm install safe-form

Usage

First, define your schema in a separate file, so you can use it both in the form and in the server action:

schema.ts

import { z } from 'zod'

export const exampleSchema = z.object({
  name: z.string().min(3, 'Name must be at least 3 characters'),
  message: z.string().min(10, 'Message must be at least 10 characters')
})

Now, create a server action:

action.ts

'use server'

import { createFormAction, FormActionError } from 'safe-form'
import { exampleSchema } from './schema'

export const exampleAction = createFormAction(exampleSchema, async (input) => {
  if (input.name === 'Joe') {
    throw new FormActionError('Joe is not allowed to send messages.') // Custom errors! 💜
  }

  return `Hello, ${input.name}! Your message is: ${input.message}`
})

Finally, create a form as a client component:

form.tsx

'use client'

import { useForm } from 'safe-form'
import { exampleAction } from './action'
import { exampleSchema } from './schema'

export const HelloForm = () => {
  const { connect, bindField, isPending, error, fieldErrors, response } =
    useForm({
      action: exampleAction,
      schema: exampleSchema
    })

  return (
    <form {...connect()}>
      <label htmlFor='name'>Name</label>
      <input {...bindField('name')} />
      {fieldErrors.name && <pre>{fieldErrors.name}</pre>}
      <br />
      <label htmlFor='message'>Message</label>
      <textarea {...bindField('message')} />
      {fieldErrors.message && <pre>{fieldErrors.message}</pre>}
      <br />
      <button type='submit' disabled={isPending}>
        Submit
      </button>
      <br />
      {error && <pre>{error}</pre>}
      {response && <div>{response}</div>}
    </form>
  )
}

License

MIT

Package Sidebar

Install

npm i safe-form

Weekly Downloads

4

Version

0.1.0

License

MIT

Unpacked Size

74.8 kB

Total Files

58

Last publish

Collaborators

  • ivanfilhoz