4.12.6 • Published 2 months ago

@vee-validate/zod v4.12.6

Weekly downloads
249
License
MIT
Repository
github
Last release
2 months ago

@vee-validate/zod

Official vee-validate integration with Zod schema validation

Guide

Zod is an excellent library for value validation which mirrors static typing APIs.

In their own words it is a:

TypeScript-first schema validation with static type inference

You can use zod as a typed schema with the @vee-validate/zod package:

# npm
npm install @vee-validate/zod
# yarn
yarn add @vee-validate/zod
# pnpm
pnpm add @vee-validate/zod

The @vee-valdiate/zod package exposes a toTypedSchema function that accepts any zod schema. Which then you can pass along to validationSchema option on useForm.

This makes the form values and submitted values typed automatically and caters for both input and output types of that schema.

import { useForm } from 'vee-validate';
import { object, string } from 'zod';
import { toTypedSchema } from '@vee-validate/zod';

const { values, handleSubmit } = useForm({
  validationSchema: toTypedSchema(
    object({
      email: string().min(1, 'required'),
      password: string().min(1, 'required'),
      name: string().optional(),
    })
  ),
});

// ❌ Type error, which means `values` is type-safe
values.email.endsWith('@gmail.com');

handleSubmit(submitted => {
  // No errors, because email is required!
  submitted.email.endsWith('@gmail.com');

  // ❌ Type error, because `name` is not required so it could be undefined
  // Means that your fields are now type safe!
  submitted.name.length;
});

Zod default values

You can also define default values on your zod schema directly and it will be picked up by the form:

import { useForm } from 'vee-validate';
import { object, string } from 'zod';
import { toTypedSchema } from '@vee-validate/zod';

const { values, handleSubmit } = useForm({
  validationSchema: toTypedSchema(
    object({
      email: string().default('something@email.com'),
      password: string().default(''),
    })
  ),
});

Your initial values will be using the schema defaults, and also the defaults will be used if the values submitted is missing these fields.

Zod preprocess

You can also define preprocessors to cast your fields before submission:

import { useForm } from 'vee-validate';
import { object, number, preprocess } from 'zod';
import { toTypedSchema } from '@vee-validate/zod';

const { values, handleSubmit } = useForm({
  validationSchema: toTypedSchema(
    object({
      age: preprocess(val => Number(val), number()),
    })
  ),
});

// typed as `unknown` since the source value can be anything
values.age;

handleSubmit(submitted => {
  // will be typed as number because zod made sure it is!
  values.age;
});
4.12.6

2 months ago

4.12.5

3 months ago

4.12.4

4 months ago

4.12.3

5 months ago

4.10.9

9 months ago

4.10.5

10 months ago

4.10.6

10 months ago

4.10.7

10 months ago

4.10.8

10 months ago

4.10.4

10 months ago

4.12.0

5 months ago

4.12.1

5 months ago

4.12.2

5 months ago

4.11.8

7 months ago

4.11.4

8 months ago

4.11.5

8 months ago

4.11.6

8 months ago

4.11.7

7 months ago

4.12.0-alpha.1

6 months ago

4.12.0-alpha.0

6 months ago

4.11.0

9 months ago

4.11.1

9 months ago

4.11.2

9 months ago

4.11.3

8 months ago

4.10.3

10 months ago

4.9.4

12 months ago

4.9.6

11 months ago

4.9.5

12 months ago

4.10.1

10 months ago

4.10.2

10 months ago

4.10.0

10 months ago

4.9.3

12 months ago

4.9.0

12 months ago

4.9.2

12 months ago

4.9.1

12 months ago

4.8.5

1 year ago

4.8.4

1 year ago

4.8.6

1 year ago

4.8.1

1 year ago

4.8.0

1 year ago

4.8.3

1 year ago

4.8.2

1 year ago

4.7.4

1 year ago

4.7.2

2 years ago

4.7.1

2 years ago

4.7.3

1 year ago

4.7.0

2 years ago

4.6.7

2 years ago

4.6.6

2 years ago

4.6.9

2 years ago

4.6.8

2 years ago

4.6.3

2 years ago

4.6.5

2 years ago

4.6.4

2 years ago

4.6.10

2 years ago

4.6.1

2 years ago

4.6.0

2 years ago

4.6.2

2 years ago

4.5.11

2 years ago

4.5.10

2 years ago

4.5.9

2 years ago

4.5.8

2 years ago

4.5.7

2 years ago

4.5.6

2 years ago

4.5.4

3 years ago

4.5.3

3 years ago

4.5.5

3 years ago

4.5.2

3 years ago

4.5.1

3 years ago

4.5.0

3 years ago

4.4.11

3 years ago

4.5.0-alpha.6

3 years ago

4.4.10

3 years ago

4.5.0-alpha.5

3 years ago

4.5.0-alpha.4

3 years ago

4.4.9

3 years ago

4.4.8

3 years ago

4.5.0-alpha.3

3 years ago

4.4.7

3 years ago

4.5.0-alpha.2

3 years ago

4.4.6

3 years ago

4.5.0-alpha.1

3 years ago

4.4.5

3 years ago

4.4.4

3 years ago

4.5.0-alpha.0

3 years ago

4.4.3

3 years ago

4.4.2

3 years ago

4.4.0-alpha.0

3 years ago

4.3.6

3 years ago

4.3.5

3 years ago

4.4.1

3 years ago

4.3.2

3 years ago

4.4.0

3 years ago

4.4.0-alpha.2

3 years ago

4.3.4

3 years ago

4.4.0-alpha.1

3 years ago

4.3.3

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.4

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.20

3 years ago

4.1.19

3 years ago