Skip to content

Commit 7807f95

Browse files
authored
fix: handle raw: true option to pass form submission values correctly (#733)
* test: make tests happy * fix(resolver): handle `raw: true` option to pass form submission values correctly Ensure that the `raw: true` option in the Resolver now correctly passes a copy of the form submission values, preventing data loss or inconsistencies. * doc(effect): update readme * doc(zod): update import example
1 parent 3233667 commit 7807f95

File tree

14 files changed

+21
-17
lines changed

14 files changed

+21
-17
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ TypeScript-first schema validation with static type inference
119119
```tsx
120120
import { useForm } from 'react-hook-form';
121121
import { zodResolver } from '@hookform/resolvers/zod';
122-
import * as z from 'zod';
122+
import { z } from 'zod';
123123

124124
const schema = z.object({
125125
name: z.string().min(1, { message: 'Required' }),
@@ -626,7 +626,7 @@ Universal adapter for schema validation, compatible with [any validation library
626626
```typescript jsx
627627
import { useForm } from 'react-hook-form';
628628
import { typeschemaResolver } from '@hookform/resolvers/typeschema';
629-
import * as z from 'zod';
629+
import { z } from 'zod';
630630

631631
// Use your favorite validation library
632632
const schema = z.object({
@@ -663,10 +663,10 @@ import { Schema } from 'effect';
663663

664664
const schema = Schema.Struct({
665665
username: Schema.String.pipe(
666-
Schema.nonEmpty({ message: () => 'username required' }),
666+
Schema.nonEmptyString({ message: () => 'username required' }),
667667
),
668668
password: Schema.String.pipe(
669-
Schema.nonEmpty({ message: () => 'password required' }),
669+
Schema.nonEmptyString({ message: () => 'password required' }),
670670
),
671671
});
672672

arktype/src/__tests__/Form.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ test("form's validation with arkType and TypeScript's integration", async () =>
4747
await user.click(screen.getByText(/submit/i));
4848

4949
expect(
50-
screen.getByText('username must be more than length 1 (was 0)'),
50+
screen.getByText('username must be more than length 1'),
5151
).toBeInTheDocument();
5252
expect(
53-
screen.getByText('password must be more than length 1 (was 0)'),
53+
screen.getByText('password must be more than length 1'),
5454
).toBeInTheDocument();
5555
expect(handleSubmit).not.toHaveBeenCalled();
5656
});

arktype/src/arktype.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ export const arktypeResolver: Resolver =
2626

2727
return {
2828
errors: {} as FieldErrors,
29-
values: resolverOptions.raw ? values : out,
29+
values: resolverOptions.raw ? Object.assign({}, values) : out,
3030
};
3131
};

bun.lockb

0 Bytes
Binary file not shown.

class-validator/src/__tests__/class-validator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('classValidatorResolver', () => {
2525

2626
it('should return values as a raw object from classValidatorResolver when `rawValues` set to true', async () => {
2727
const result = await classValidatorResolver(Schema, undefined, {
28-
rawValues: true,
28+
raw: true,
2929
})(validData, undefined, {
3030
fields,
3131
shouldUseNativeValidation,

class-validator/src/class-validator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const classValidatorResolver: Resolver =
6161
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
6262

6363
return {
64-
values: resolverOptions.rawValues ? values : data,
64+
values: resolverOptions.raw ? Object.assign({}, values) : data,
6565
errors: {},
6666
};
6767
};

class-validator/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type Resolver = <T extends { [_: string]: any }>(
88
validator?: ValidatorOptions;
99
transformer?: ClassTransformOptions;
1010
},
11-
resolverOptions?: { mode?: 'async' | 'sync'; rawValues?: boolean },
11+
resolverOptions?: { mode?: 'async' | 'sync'; raw?: boolean },
1212
) => <TFieldValues extends FieldValues, TContext>(
1313
values: TFieldValues,
1414
context: TContext | undefined,

superstruct/src/superstruct.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const superstructResolver: Resolver =
2929
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
3030

3131
return {
32-
values: resolverOptions.raw ? values : result[1],
32+
values: resolverOptions.raw ? Object.assign({}, values) : result[1],
3333
errors: {},
3434
};
3535
};

typeschema/src/typeschema.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export const typeschemaResolver: Resolver =
5353
if (result.success) {
5454
return {
5555
errors: {} as FieldErrors,
56-
values: resolverOptions.raw ? values : (result.data as any),
56+
values: resolverOptions.raw
57+
? Object.assign({}, values)
58+
: (result.data as any),
5759
};
5860
}
5961

valibot/src/__tests__/valibot.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('valibotResolver', () => {
130130
expect(result).toEqual({
131131
errors: {
132132
type: {
133-
message: 'Invalid type: Expected "a" | "b" but received "c"',
133+
message: 'Invalid type: Expected ("a" | "b") but received "c"',
134134
ref: undefined,
135135
type: 'variant',
136136
},

valibot/src/valibot.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export const valibotResolver: Resolver =
6767

6868
// Otherwise, return resolver result with values
6969
return {
70-
values: resolverOptions.raw ? values : (result.output as FieldValues),
70+
values: resolverOptions.raw
71+
? Object.assign({}, values)
72+
: (result.output as FieldValues),
7173
errors: {},
7274
};
7375
};

vine/src/vine.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const vineResolver: Resolver =
4646

4747
return {
4848
errors: {} as FieldErrors,
49-
values: resolverOptions.raw ? values : data,
49+
values: resolverOptions.raw ? Object.assign({}, values) : data,
5050
};
5151
} catch (error: any) {
5252
if (error instanceof errors.E_VALIDATION_ERROR) {

yup/src/yup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function yupResolver<TFieldValues extends FieldValues>(
7878
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
7979

8080
return {
81-
values: resolverOptions.raw ? values : result,
81+
values: resolverOptions.raw ? Object.assign({}, values) : result,
8282
errors: {},
8383
};
8484
} catch (e: any) {

zod/src/zod.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const zodResolver: Resolver =
6868

6969
return {
7070
errors: {} as FieldErrors,
71-
values: resolverOptions.raw ? values : data,
71+
values: resolverOptions.raw ? Object.assign({}, values) : data,
7272
};
7373
} catch (error: any) {
7474
if (isZodError(error)) {

0 commit comments

Comments
 (0)