Skip to content

Commit f3da212

Browse files
authored
feat(yupResolver): add support Yup v1 (#520)
BREAKING CHANGE: require Yup v1
1 parent a11288a commit f3da212

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
"vite": "^4.2.0",
207207
"vite-tsconfig-paths": "^4.0.7",
208208
"vitest": "^0.29.3",
209-
"yup": "^0.32.11",
209+
"yup": "^1.0.2",
210210
"zod": "^3.21.4"
211211
},
212212
"peerDependencies": {

pnpm-lock.yaml

Lines changed: 15 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

yup/src/__tests__/__fixtures__/data.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ export const schema = yup.object({
3030

3131
export const schemaWithWhen = yup.object({
3232
name: yup.string().required(),
33-
value: yup.string().when('name', {
34-
is: 'test',
35-
then: yup.number().required(),
33+
value: yup.string().when('name', ([name], schema) => {
34+
return name === 'test' ? yup.number().required() : schema;
3635
}),
3736
});
3837

yup/src/__tests__/__snapshots__/yup.ts.snap

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exports[`yupResolver > should return a single error from yupResolver when valida
1818
"name": {
1919
"message": "like[0].name is a required field",
2020
"ref": undefined,
21-
"type": "required",
21+
"type": "optionality",
2222
},
2323
},
2424
],
@@ -34,7 +34,7 @@ exports[`yupResolver > should return a single error from yupResolver when valida
3434
"ref": {
3535
"name": "username",
3636
},
37-
"type": "required",
37+
"type": "optionality",
3838
},
3939
},
4040
"values": {},
@@ -59,7 +59,7 @@ exports[`yupResolver > should return a single error from yupResolver with \`mode
5959
"name": {
6060
"message": "like[0].name is a required field",
6161
"ref": undefined,
62-
"type": "required",
62+
"type": "optionality",
6363
},
6464
},
6565
],
@@ -75,7 +75,7 @@ exports[`yupResolver > should return a single error from yupResolver with \`mode
7575
"ref": {
7676
"name": "username",
7777
},
78-
"type": "required",
78+
"type": "optionality",
7979
},
8080
},
8181
"values": {},
@@ -106,9 +106,9 @@ exports[`yupResolver > should return all the errors from yupResolver when valida
106106
"name": {
107107
"message": "like[0].name is a required field",
108108
"ref": undefined,
109-
"type": "required",
109+
"type": "optionality",
110110
"types": {
111-
"required": "like[0].name is a required field",
111+
"optionality": "like[0].name is a required field",
112112
},
113113
},
114114
},
@@ -133,9 +133,9 @@ exports[`yupResolver > should return all the errors from yupResolver when valida
133133
"ref": {
134134
"name": "username",
135135
},
136-
"type": "required",
136+
"type": "optionality",
137137
"types": {
138-
"required": "username is a required field",
138+
"optionality": "username is a required field",
139139
},
140140
},
141141
},
@@ -167,9 +167,9 @@ exports[`yupResolver > should return all the errors from yupResolver when valida
167167
"name": {
168168
"message": "like[0].name is a required field",
169169
"ref": undefined,
170-
"type": "required",
170+
"type": "optionality",
171171
"types": {
172-
"required": "like[0].name is a required field",
172+
"optionality": "like[0].name is a required field",
173173
},
174174
},
175175
},
@@ -194,9 +194,9 @@ exports[`yupResolver > should return all the errors from yupResolver when valida
194194
"ref": {
195195
"name": "username",
196196
},
197-
"type": "required",
197+
"type": "optionality",
198198
"types": {
199-
"required": "username is a required field",
199+
"optionality": "username is a required field",
200200
},
201201
},
202202
},
@@ -229,3 +229,16 @@ exports[`yupResolver > should return correct error message with using yup.test 1
229229
"values": {},
230230
}
231231
`;
232+
233+
exports[`yupResolver > should throw an error without inner property 1`] = `
234+
{
235+
"errors": {
236+
"value": {
237+
"message": "value must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`\\"\\"\`).",
238+
"ref": undefined,
239+
"type": "typeError",
240+
},
241+
},
242+
"values": {},
243+
}
244+
`;

yup/src/__tests__/yup.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('yupResolver', () => {
9292
name: yup
9393
.string()
9494
.required()
95-
.when('$min', (min: boolean, schema: yup.StringSchema) => {
95+
.when('$min', ([min], schema) => {
9696
return min ? schema.min(6) : schema;
9797
}),
9898
});
@@ -178,14 +178,16 @@ describe('yupResolver', () => {
178178
);
179179
});
180180

181-
it('should throw an error without inner property', () => {
182-
expect(
183-
yupResolver(schemaWithWhen)({ name: 'test', value: '' }, undefined, {
181+
it('should throw an error without inner property', async () => {
182+
const result = await yupResolver(schemaWithWhen)(
183+
{ name: 'test', value: '' },
184+
undefined,
185+
{
184186
fields,
185187
shouldUseNativeValidation,
186-
}),
187-
).rejects.toThrowErrorMatchingInlineSnapshot(
188-
'"You cannot `concat()` schema\'s of different types: string and number"',
188+
},
189189
);
190+
191+
expect(result).toMatchSnapshot();
190192
});
191193
});

yup/src/types.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
import {
2-
FieldValues,
3-
ResolverOptions,
4-
ResolverResult,
5-
} from 'react-hook-form';
1+
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
62
import * as Yup from 'yup';
7-
import type Lazy from 'yup/lib/Lazy';
83

9-
type Options<T extends Yup.AnyObjectSchema | Lazy<any>> = Parameters<
10-
T['validate']
11-
>[1];
4+
type Options<T extends Yup.ObjectSchema<any>> = Parameters<T['validate']>[1];
125

13-
export type Resolver = <T extends Yup.AnyObjectSchema | Lazy<any>>(
6+
export type Resolver = <T extends Yup.ObjectSchema<any>>(
147
schema: T,
158
schemaOptions?: Options<T>,
16-
factoryOptions?: { mode?: 'async' | 'sync', rawValues?: boolean; },
9+
factoryOptions?: { mode?: 'async' | 'sync'; rawValues?: boolean },
1710
) => <TFieldValues extends FieldValues, TContext>(
1811
values: TFieldValues,
1912
context: TContext | undefined,

0 commit comments

Comments
 (0)