Skip to content

Commit

Permalink
feat: update to zod 3 (#200)
Browse files Browse the repository at this point in the history
Co-authored-by: Joris <reixjoris@gmail.com>
  • Loading branch information
dependabot[bot] and jorisre authored Aug 19, 2021
1 parent c2cd360 commit 35cedc7
Show file tree
Hide file tree
Showing 10 changed files with 1,321 additions and 1,272 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ import * as t from 'typanion';

const isUser = t.isObject({
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
age: t.applyCascade(t.isNumber(), [t.isInteger(), t.isInInclusiveRange(1, 100)]),
age: t.applyCascade(t.isNumber(), [
t.isInteger(),
t.isInInclusiveRange(1, 100),
]),
});

const App = () => {
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@
"devDependencies": {
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.1.9",
"@testing-library/user-event": "^13.2.1",
"@types/jest": "^27.0.0",
"@types/react": "^17.0.8",
"@types/react": "^17.0.17",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"check-export-map": "^1.1.1",
Expand All @@ -177,30 +177,30 @@
"computed-types": "^1.7.0",
"eslint": "^7.27.0",
"eslint-config-prettier": "^8.3.0",
"fp-ts": "^2.10.5",
"fp-ts": "^2.11.1",
"husky": "^7.0.1",
"io-ts": "^2.0.0",
"io-ts-types": "^0.5.16",
"jest": "^27.0.3",
"joi": "^17.4.0",
"lint-staged": "^11.0.0",
"microbundle": "^0.13.1",
"jest": "^27.0.6",
"joi": "^17.4.2",
"lint-staged": "^11.1.2",
"microbundle": "^0.13.3",
"monocle-ts": "^2.3.10",
"newtype-ts": "^0.3.4",
"nope-validator": "^1.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.0",
"prettier": "^2.3.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hook-form": "7.12.2",
"reflect-metadata": "^0.1.13",
"superstruct": "^0.15.2",
"ts-jest": "^27.0.1",
"ts-jest": "^27.0.4",
"typanion": "^3.3.2",
"typescript": "^4.3.2",
"vest": "^3.2.3",
"vest": "^3.2.5",
"yup": "^0.32.9",
"zod": "^1.11.17"
"zod": "^3.7.1"
},
"peerDependencies": {
"react-hook-form": "^7.0.0"
Expand Down
2,439 changes: 1,272 additions & 1,167 deletions yarn.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions zod/src/__tests__/Form-native-validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import React from 'react';
import { useForm } from 'react-hook-form';
import { act, render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import * as Zod from 'zod';
import { z } from 'zod';
import { zodResolver } from '..';

const USERNAME_REQUIRED_MESSAGE = 'username field is required';
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';

const schema = Zod.object({
username: Zod.string().nonempty({ message: USERNAME_REQUIRED_MESSAGE }),
password: Zod.string().nonempty({ message: PASSWORD_REQUIRED_MESSAGE }),
const schema = z.object({
username: z.string().nonempty({ message: USERNAME_REQUIRED_MESSAGE }),
password: z.string().nonempty({ message: PASSWORD_REQUIRED_MESSAGE }),
});

type FormData = Zod.infer<typeof schema>;
type FormData = z.infer<typeof schema>;

interface Props {
onSubmit: (data: FormData) => void;
Expand Down
10 changes: 5 additions & 5 deletions zod/src/__tests__/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import React from 'react';
import { render, screen, act } from '@testing-library/react';
import user from '@testing-library/user-event';
import { useForm } from 'react-hook-form';
import * as Zod from 'zod';
import { z } from 'zod';
import { zodResolver } from '..';

const schema = Zod.object({
username: Zod.string().nonempty({ message: 'username field is required' }),
password: Zod.string().nonempty({ message: 'password field is required' }),
const schema = z.object({
username: z.string().nonempty({ message: 'username field is required' }),
password: z.string().nonempty({ message: 'password field is required' }),
});

type FormData = Zod.infer<typeof schema> & { unusedProperty: string };
type FormData = z.infer<typeof schema> & { unusedProperty: string };

interface Props {
onSubmit: (data: FormData) => void;
Expand Down
8 changes: 4 additions & 4 deletions zod/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Field, InternalFieldName } from 'react-hook-form';
import * as z from 'zod';
import { z } from 'zod';

export const schema = z
.object({
Expand Down Expand Up @@ -30,9 +30,9 @@ export const schema = z
)
.optional(),
})
.refine((data) => data.password === data.repeatPassword, {
message: "Passwords don't match",
path: ['confirm'], // set path of error
.refine((obj) => obj.password === obj.repeatPassword, {
message: 'Passwords do not match',
path: ['confirm'],
});

export const validData: z.infer<typeof schema> = {
Expand Down
86 changes: 14 additions & 72 deletions zod/src/__tests__/__snapshots__/zod.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ Object {
"ref": undefined,
"type": "invalid_type",
},
"confirm": Object {
"message": "Passwords don't match",
"ref": undefined,
"type": "custom_error",
},
"email": Object {
"message": "Invalid email",
"ref": Object {
Expand All @@ -30,8 +25,8 @@ Object {
"ref": undefined,
"type": "invalid_type",
},
"like": Object {
"0": Object {
"like": Array [
Object {
"id": Object {
"message": "Expected number, received string",
"ref": undefined,
Expand All @@ -43,10 +38,7 @@ Object {
"type": "invalid_type",
},
},
"message": "Expected number, received string",
"ref": undefined,
"type": "invalid_type",
},
],
"password": Object {
"message": "One uppercase character",
"ref": Object {
Expand Down Expand Up @@ -94,11 +86,6 @@ Object {
"ref": undefined,
"type": "invalid_type",
},
"confirm": Object {
"message": "Passwords don't match",
"ref": undefined,
"type": "custom_error",
},
"email": Object {
"message": "Invalid email",
"ref": Object {
Expand All @@ -111,8 +98,8 @@ Object {
"ref": undefined,
"type": "invalid_type",
},
"like": Object {
"0": Object {
"like": Array [
Object {
"id": Object {
"message": "Expected number, received string",
"ref": undefined,
Expand All @@ -124,10 +111,7 @@ Object {
"type": "invalid_type",
},
},
"message": "Expected number, received string",
"ref": undefined,
"type": "invalid_type",
},
],
"password": Object {
"message": "One uppercase character",
"ref": Object {
Expand Down Expand Up @@ -182,19 +166,7 @@ Object {
"ref": undefined,
"type": "invalid_type",
"types": Object {
"invalid_type": Array [
"Expected number, received string",
"Expected undefined, received string",
],
"invalid_union": "Invalid input",
},
},
"confirm": Object {
"message": "Passwords don't match",
"ref": undefined,
"type": "custom_error",
"types": Object {
"custom_error": "Passwords don't match",
"invalid_type": "Expected number, received string",
},
},
"email": Object {
Expand All @@ -215,8 +187,8 @@ Object {
"invalid_type": "Required",
},
},
"like": Object {
"0": Object {
"like": Array [
Object {
"id": Object {
"message": "Expected number, received string",
"ref": undefined,
Expand All @@ -234,14 +206,7 @@ Object {
},
},
},
"message": "Expected number, received string",
"ref": undefined,
"type": "invalid_type",
"types": Object {
"invalid_type": "Expected undefined, received array",
"invalid_union": "Invalid input",
},
},
],
"password": Object {
"message": "One uppercase character",
"ref": Object {
Expand Down Expand Up @@ -278,9 +243,7 @@ Object {
"ref": undefined,
"type": "invalid_string",
"types": Object {
"invalid_literal_value": "Input must be \\"\\"",
"invalid_string": "Custom error url",
"invalid_union": "Invalid input",
},
},
"username": Object {
Expand Down Expand Up @@ -318,19 +281,7 @@ Object {
"ref": undefined,
"type": "invalid_type",
"types": Object {
"invalid_type": Array [
"Expected number, received string",
"Expected undefined, received string",
],
"invalid_union": "Invalid input",
},
},
"confirm": Object {
"message": "Passwords don't match",
"ref": undefined,
"type": "custom_error",
"types": Object {
"custom_error": "Passwords don't match",
"invalid_type": "Expected number, received string",
},
},
"email": Object {
Expand All @@ -351,8 +302,8 @@ Object {
"invalid_type": "Required",
},
},
"like": Object {
"0": Object {
"like": Array [
Object {
"id": Object {
"message": "Expected number, received string",
"ref": undefined,
Expand All @@ -370,14 +321,7 @@ Object {
},
},
},
"message": "Expected number, received string",
"ref": undefined,
"type": "invalid_type",
"types": Object {
"invalid_type": "Expected undefined, received array",
"invalid_union": "Invalid input",
},
},
],
"password": Object {
"message": "One uppercase character",
"ref": Object {
Expand Down Expand Up @@ -414,9 +358,7 @@ Object {
"ref": undefined,
"type": "invalid_string",
"types": Object {
"invalid_literal_value": "Input must be \\"\\"",
"invalid_string": "Custom error url",
"invalid_union": "Invalid input",
},
},
"username": Object {
Expand Down
2 changes: 1 addition & 1 deletion zod/src/__tests__/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('zodResolver', () => {
expect(result).toMatchSnapshot();
});

it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
it.only('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
const result = await zodResolver(schema, undefined, { mode: 'sync' })(
invalidData,
undefined,
Expand Down
7 changes: 3 additions & 4 deletions zod/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
UnpackNestedValue,
ResolverOptions,
} from 'react-hook-form';
import * as z from 'zod';
import type { ParseParams } from 'zod/lib/src/parser';
import { z } from 'zod';

export type Resolver = <T extends z.ZodSchema<any, any>>(
export type Resolver = <T extends z.Schema<any, any>>(
schema: T,
schemaOptions?: ParseParams,
schemaOptions?: Partial<z.ParseParamsNoData>,
factoryOptions?: { mode?: 'async' | 'sync' },
) => <TFieldValues extends FieldValues, TContext>(
values: UnpackNestedValue<TFieldValues>,
Expand Down
4 changes: 2 additions & 2 deletions zod/src/zod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { appendErrors, FieldError } from 'react-hook-form';
import * as z from 'zod';
import { z } from 'zod';
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
import type { Resolver } from './types';

const parseErrorSchema = (
zodErrors: z.ZodSuberror[],
zodErrors: z.ZodIssue[],
validateAllFieldCriteria: boolean,
) => {
const errors: Record<string, FieldError> = {};
Expand Down

0 comments on commit 35cedc7

Please sign in to comment.