-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathsuperstruct.ts
More file actions
111 lines (92 loc) · 3.21 KB
/
superstruct.ts
File metadata and controls
111 lines (92 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { renderHook } from '@testing-library/react';
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
import * as s from 'superstruct';
import { superstructResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
const shouldUseNativeValidation = false;
describe('superstructResolver', () => {
it('should return values from superstructResolver when validation pass', async () => {
const result = await superstructResolver(schema)(validData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return values from superstructResolver with coerced values', async () => {
const result = await superstructResolver(
s.object({
id: s.coerce(s.string(), s.number(), (val) => String(val)),
}),
{ coerce: true },
)({ id: 1 }, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toEqual({ errors: {}, values: { id: '1' } });
});
it('should return a single error from superstructResolver when validation fails', async () => {
const result = await superstructResolver(schema)(invalidData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toMatchSnapshot();
});
it('should return values from superstructResolver when validation pass & raw=true', async () => {
const result = await superstructResolver(schema, undefined, { raw: true })(
validData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(result).toEqual({ errors: {}, values: validData });
});
/**
* Type inference tests
*/
it('should correctly infer the output type from a superstruct schema', () => {
const resolver = superstructResolver(s.object({ id: s.number() }));
expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: number }, unknown, { id: number }>
>();
});
it('should correctly infer the output type from a superstruct schema for the handleSubmit function in useForm', () => {
const schema = s.object({ id: s.number() });
const {
result: { current: form },
} = renderHook(() =>
useForm({
resolver: superstructResolver(schema, {}),
}),
);
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
SubmitHandler<{
id: number;
}>
>();
});
it('should correctly infer the output type from a superstruct schema with a transform for the handleSubmit function in useForm', () => {
const schema = s.object({
id: s.coerce(s.string(), s.number(), (val) => String(val)),
});
const {
result: { current: form },
} = renderHook(() =>
useForm({
// With coerce, there is no way to infer the input type of the schema
resolver: superstructResolver<{ id: number }, unknown, { id: string }>(
schema,
{ coerce: true },
),
}),
);
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
SubmitHandler<{
id: string;
}>
>();
});
});