2.1.0 ā€¢ Published 8 months ago

@roqueform/zod-plugin v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Zod plugin for Roqueform

Validates Roqueform fields with Zod schemas.

npm install --save-prod @roqueform/zod-plugin

Overview

šŸ”Ž API documentation is available here.

This plugin doesn't require any rendering framework. To simplify the usage example, we're going to use the React integration.

import { SyntheticEvent } from 'react';
import { FieldRenderer, useField } from '@roqueform/react';
import { zodPlugin } from '@roqueform/zod-plugin';
import { z } from 'zod';

const planetSchema = z.object({
  name: z.string().min(1),
});

export const App = () => {
  const planetField = useField(
    { name: '' },
    zodPlugin(planetSchema)
  );

  const handleSubmit = (event: SyntheticEvent) => {
    event.preventDefault();

    if (planetField.validate()) {
      // If your shapes have transformations or refinements, you can safely parse
      // the field value after it was successfully validated.
      const value = planetSchema.parse(planetField.value);
      
      doSubmit(value);
    } else {
      // Errors are associated with fields automatically.
    }
  };

  return (
    <form onSubmit={handleSubmit}>

      <FieldRenderer field={planetField.at('name')}>
        {nameField => (
          <>
            <input
              value={nameField.value}
              onChange={event => {
                nameField.setValue(event.target.value);
              }}
            />

            {nameField.errors[0]?.message}
          </>
        )}
      </FieldRenderer>

      <button type="submit">
        {'Submit'}
      </button>

    </form>
  );
};
2.1.0

8 months ago

2.0.0

8 months ago

1.0.1

1 year ago

1.0.0

2 years ago