1.0.17 • Published 8 months ago

@felte/validator-zod v1.0.17

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

@felte/validator-zod

Tests Bundle size NPM Version codecov

A package to help you handle validation with Zod in Felte.

Installation

npm install --save @felte/validator-zod zod

# Or, if you use yarn

yarn add @felte/validator-zod zod

Usage

Call validator with an object containing your Zod schema in the schema property. The result of the call can be passed as an extender to Felte:

import { validator } from '@felte/validator-zod';
import { z } from 'zod';

const schema = z.object({
  email: z.string().email().nonempty(),
  password: z.string().nonempty(),
});

const { form } = createForm({
  // ...
  extend: validator({ schema }), // or `extend: [validator({ schema })],`
  // ...
});

OR use the validateSchema function directly in the validate option of createForm. (No need to extend Felte).

import { validateSchema } from '@felte/validator-zod';
import { z } from 'zod';

const schema = z.object({
  email: z.string().email().nonempty(),
  password: z.string().nonempty(),
});

const { form } = createForm({
  // ...
  validate: validateSchema(schema),
  // ...
});

Warnings

Optionally, you can tell this package to assign the results of your validations to your warnings store by setting the level property of the validator function to warning. It's error by default:

import { validator } from '@felte/validator-zod';
import { z } from 'zod';

const schema = z.object({
  email: z.string().email().nonempty(),
  password: z.string().nonempty(),
});

// We only warn if the user has started typing a value
const warnSchema = zod.object({
  password: zod
    .string()
    .refine((value) => (value ? value.length > 8 : true), {
      message: 'Password is not secure',
    }),
});

const { form } = createForm({
  // ...
  extend: [
    validator({ schema }),
    validator({ schema: warnSchema, level: 'warning' }),
  ],
  // ...
});

Typescript

Zod allows you to infer the type of your schema using z.infer. This can be used so you don't need to create a type for your form's data:

import { z } from 'zod';

const schema = z.object({
  email: z.string().email().nonempty(),
  password: z.string().nonempty(),
});

const { form } = createForm<z.infer<typeof schema>>(/* ... */);
1.0.17

8 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.13

1 year ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.0-next.19

2 years ago

1.0.1

2 years ago

1.0.0-next.18

2 years ago

1.0.0

2 years ago

1.0.0-next.17

2 years ago

1.0.0-next.16

2 years ago

1.0.0-next.11

2 years ago

1.0.0-next.10

2 years ago

1.0.0-next.15

2 years ago

1.0.0-next.14

2 years ago

1.0.0-next.13

2 years ago

1.0.3

2 years ago

1.0.0-next.12

2 years ago

1.0.0-next.0

2 years ago

1.0.0-next.1

2 years ago

1.0.0-next.2

2 years ago

1.0.0-next.22

2 years ago

1.0.0-next.21

2 years ago

1.0.0-next.20

2 years ago

1.0.0-next.3

2 years ago

1.0.0-next.4

2 years ago

1.0.0-next.5

2 years ago

0.3.8

2 years ago

1.0.0-next.6

2 years ago

0.3.7

2 years ago

1.0.0-next.7

2 years ago

1.0.0-next.8

2 years ago

1.0.0-next.9

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago