@hookform/resolvers 3.7.0 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -0
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/dist/types.d.ts +1 -1
- package/ajv/src/__tests__/ajv.ts +7 -1
- package/ajv/src/ajv.ts +1 -1
- package/ajv/src/types.ts +1 -5
- package/arktype/dist/arktype.js +1 -1
- package/arktype/dist/arktype.js.map +1 -1
- package/arktype/dist/arktype.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs.map +1 -1
- package/arktype/dist/arktype.module.js +1 -1
- package/arktype/dist/arktype.module.js.map +1 -1
- package/arktype/dist/arktype.umd.js +1 -1
- package/arktype/dist/arktype.umd.js.map +1 -1
- package/arktype/dist/types.d.ts +1 -1
- package/arktype/package.json +2 -1
- package/arktype/src/__tests__/Form-native-validation.tsx +5 -5
- package/arktype/src/__tests__/Form.tsx +2 -2
- package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
- package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +29 -29
- package/arktype/src/__tests__/arktype.ts +1 -1
- package/arktype/src/arktype.ts +2 -2
- package/arktype/src/types.ts +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.modern.mjs.map +1 -1
- package/class-validator/dist/class-validator.module.js.map +1 -1
- package/class-validator/dist/class-validator.umd.js.map +1 -1
- package/class-validator/dist/types.d.ts +2 -2
- package/class-validator/src/__tests__/Form-native-validation.tsx +2 -2
- package/class-validator/src/__tests__/Form.tsx +2 -2
- package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +2 -2
- package/class-validator/src/__tests__/class-validator.ts +4 -4
- package/class-validator/src/class-validator.ts +2 -2
- package/class-validator/src/types.ts +2 -2
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.modern.mjs.map +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/dist/types.d.ts +1 -1
- package/computed-types/src/__tests__/Form-native-validation.tsx +2 -2
- package/computed-types/src/__tests__/Form.tsx +2 -2
- package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
- package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +6 -6
- package/computed-types/src/__tests__/computed-types.ts +1 -1
- package/computed-types/src/computed-types.ts +2 -2
- package/computed-types/src/types.ts +1 -1
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +2 -2
- package/dist/resolvers.mjs.map +1 -1
- package/dist/resolvers.module.js +1 -1
- package/dist/resolvers.module.js.map +1 -1
- package/dist/resolvers.umd.js +1 -1
- package/dist/resolvers.umd.js.map +1 -1
- package/dist/toNestErrors.d.ts +1 -6
- package/dist/validateFieldsNatively.d.ts +1 -1
- package/effect-ts/dist/effect-ts.js +1 -1
- package/effect-ts/dist/effect-ts.js.map +1 -1
- package/effect-ts/dist/effect-ts.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
- package/effect-ts/dist/effect-ts.module.js +1 -1
- package/effect-ts/dist/effect-ts.module.js.map +1 -1
- package/effect-ts/dist/effect-ts.umd.js +1 -1
- package/effect-ts/dist/effect-ts.umd.js.map +1 -1
- package/effect-ts/src/__tests__/Form-native-validation.tsx +2 -2
- package/effect-ts/src/__tests__/Form.tsx +2 -2
- package/effect-ts/src/__tests__/__snapshots__/effect-ts.ts.snap +2 -2
- package/effect-ts/src/__tests__/effect-ts.ts +1 -1
- package/effect-ts/src/effect-ts.ts +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.d.ts +4 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js +2 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js.map +1 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.mjs +2 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs +2 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs.map +1 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js +2 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js.map +1 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js +2 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js.map +1 -0
- package/fluentvalidation-ts/dist/index.d.ts +1 -0
- package/fluentvalidation-ts/package.json +18 -0
- package/fluentvalidation-ts/src/__tests__/Form-native-validation.tsx +88 -0
- package/fluentvalidation-ts/src/__tests__/Form.tsx +63 -0
- package/fluentvalidation-ts/src/__tests__/__fixtures__/data.ts +121 -0
- package/fluentvalidation-ts/src/__tests__/__snapshots__/fluentvalidation-ts.ts.snap +129 -0
- package/fluentvalidation-ts/src/__tests__/fluentvalidation-ts.ts +113 -0
- package/fluentvalidation-ts/src/fluentvalidation-ts.ts +102 -0
- package/fluentvalidation-ts/src/index.ts +1 -0
- package/io-ts/dist/errorsToRecord.d.ts +0 -1
- package/io-ts/dist/io-ts.js +1 -1
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs.map +1 -1
- package/io-ts/dist/io-ts.module.js +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/io-ts/src/__tests__/Form-native-validation.tsx +2 -2
- package/io-ts/src/__tests__/Form.tsx +2 -2
- package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +9 -9
- package/io-ts/src/__tests__/errorsToRecord.ts +29 -19
- package/io-ts/src/__tests__/io-ts.ts +1 -1
- package/io-ts/src/errorsToRecord.ts +19 -10
- package/io-ts/src/io-ts.ts +1 -1
- package/joi/dist/joi.js +1 -1
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/joi.mjs +1 -1
- package/joi/dist/joi.modern.mjs +1 -1
- package/joi/dist/joi.modern.mjs.map +1 -1
- package/joi/dist/joi.module.js +1 -1
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/dist/types.d.ts +1 -1
- package/joi/src/__tests__/Form-native-validation.tsx +2 -2
- package/joi/src/__tests__/Form.tsx +2 -2
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +56 -56
- package/joi/src/__tests__/joi.ts +1 -1
- package/joi/src/joi.ts +1 -1
- package/joi/src/types.ts +1 -5
- package/nope/dist/nope.js.map +1 -1
- package/nope/dist/nope.modern.mjs.map +1 -1
- package/nope/dist/nope.module.js.map +1 -1
- package/nope/dist/nope.umd.js.map +1 -1
- package/nope/dist/types.d.ts +1 -1
- package/nope/src/__tests__/Form-native-validation.tsx +2 -2
- package/nope/src/__tests__/Form.tsx +2 -2
- package/nope/src/__tests__/__fixtures__/data.ts +1 -1
- package/nope/src/__tests__/nope.ts +1 -1
- package/nope/src/nope.ts +1 -1
- package/nope/src/types.ts +2 -2
- package/package.json +69 -54
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.modern.mjs.map +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/dist/types.d.ts +1 -1
- package/superstruct/src/__tests__/Form-native-validation.tsx +2 -2
- package/superstruct/src/__tests__/Form.tsx +2 -2
- package/superstruct/src/__tests__/__fixtures__/data.ts +8 -8
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +4 -4
- package/superstruct/src/__tests__/superstruct.ts +1 -1
- package/superstruct/src/superstruct.ts +1 -1
- package/superstruct/src/types.ts +1 -1
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -1
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/dist/types.d.ts +1 -1
- package/typanion/src/__tests__/Form-native-validation.tsx +1 -1
- package/typanion/src/__tests__/Form.tsx +1 -1
- package/typanion/src/__tests__/__fixtures__/data.ts +12 -7
- package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +4 -4
- package/typanion/src/__tests__/typanion.ts +1 -1
- package/typanion/src/typanion.ts +1 -1
- package/typanion/src/types.ts +4 -4
- package/typebox/dist/typebox.js +1 -1
- package/typebox/dist/typebox.js.map +1 -1
- package/typebox/dist/typebox.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs.map +1 -1
- package/typebox/dist/typebox.module.js +1 -1
- package/typebox/dist/typebox.module.js.map +1 -1
- package/typebox/dist/typebox.umd.js +1 -1
- package/typebox/dist/typebox.umd.js.map +1 -1
- package/typebox/dist/types.d.ts +3 -2
- package/typebox/src/__tests__/Form-compiler.tsx +57 -0
- package/typebox/src/__tests__/Form-native-validation-compiler.tsx +86 -0
- package/typebox/src/__tests__/Form-native-validation.tsx +4 -4
- package/typebox/src/__tests__/Form.tsx +3 -3
- package/typebox/src/__tests__/__fixtures__/data.ts +6 -2
- package/typebox/src/__tests__/__snapshots__/typebox-compiler.ts.snap +188 -0
- package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +13 -13
- package/typebox/src/__tests__/typebox-compiler.ts +37 -0
- package/typebox/src/__tests__/typebox.ts +1 -1
- package/typebox/src/typebox.ts +8 -3
- package/typebox/src/types.ts +3 -2
- package/typeschema/dist/index.d.ts +2 -0
- package/typeschema/dist/types.d.ts +9 -0
- package/typeschema/dist/typeschema.d.ts +2 -0
- package/typeschema/dist/typeschema.js +2 -0
- package/typeschema/dist/typeschema.js.map +1 -0
- package/typeschema/dist/typeschema.mjs +2 -0
- package/typeschema/dist/typeschema.modern.mjs +2 -0
- package/typeschema/dist/typeschema.modern.mjs.map +1 -0
- package/typeschema/dist/typeschema.module.js +2 -0
- package/typeschema/dist/typeschema.module.js.map +1 -0
- package/typeschema/dist/typeschema.umd.js +2 -0
- package/typeschema/dist/typeschema.umd.js.map +1 -0
- package/typeschema/package.json +18 -0
- package/typeschema/src/__tests__/Form-native-validation.tsx +82 -0
- package/typeschema/src/__tests__/Form.tsx +53 -0
- package/typeschema/src/__tests__/__fixtures__/data.ts +88 -0
- package/typeschema/src/__tests__/__snapshots__/typeschema.ts.snap +225 -0
- package/typeschema/src/__tests__/typeschema.ts +70 -0
- package/typeschema/src/index.ts +2 -0
- package/typeschema/src/types.ts +18 -0
- package/typeschema/src/typeschema.ts +70 -0
- package/valibot/dist/types.d.ts +1 -1
- package/valibot/dist/valibot.js +1 -1
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/src/__tests__/Form-native-validation.tsx +2 -2
- package/valibot/src/__tests__/Form.tsx +1 -1
- package/valibot/src/__tests__/valibot.ts +4 -4
- package/valibot/src/types.ts +1 -1
- package/valibot/src/valibot.ts +12 -6
- package/vest/dist/types.d.ts +2 -2
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/vest.modern.mjs.map +1 -1
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/src/__tests__/Form-native-validation.tsx +1 -1
- package/vest/src/__tests__/Form.tsx +1 -1
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +0 -6
- package/vest/src/__tests__/vest.ts +14 -8
- package/vest/src/types.ts +11 -4
- package/vest/src/vest.ts +1 -1
- package/vine/dist/types.d.ts +1 -1
- package/vine/dist/vine.js +1 -1
- package/vine/dist/vine.js.map +1 -1
- package/vine/dist/vine.mjs +1 -1
- package/vine/dist/vine.modern.mjs +1 -1
- package/vine/dist/vine.modern.mjs.map +1 -1
- package/vine/dist/vine.module.js +1 -1
- package/vine/dist/vine.module.js.map +1 -1
- package/vine/dist/vine.umd.js +1 -1
- package/vine/dist/vine.umd.js.map +1 -1
- package/vine/src/__tests__/Form-native-validation.tsx +9 -5
- package/vine/src/__tests__/Form.tsx +9 -5
- package/vine/src/__tests__/__fixtures__/data.ts +4 -2
- package/vine/src/__tests__/vine.ts +1 -1
- package/vine/src/types.ts +1 -1
- package/vine/src/vine.ts +10 -8
- package/yup/dist/yup.d.ts +1 -1
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.modern.mjs.map +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/src/__tests__/Form-native-validation.tsx +1 -1
- package/yup/src/__tests__/Form.tsx +1 -1
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +13 -13
- package/yup/src/__tests__/yup.ts +9 -7
- package/yup/src/yup.ts +2 -2
- package/zod/dist/types.d.ts +1 -1
- package/zod/dist/zod.js +1 -1
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.mjs +1 -1
- package/zod/dist/zod.modern.mjs +1 -1
- package/zod/dist/zod.modern.mjs.map +1 -1
- package/zod/dist/zod.module.js +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/src/__tests__/Form-native-validation.tsx +2 -2
- package/zod/src/__tests__/Form.tsx +1 -1
- package/zod/src/__tests__/zod.ts +1 -1
- package/zod/src/types.ts +1 -1
- package/zod/src/zod.ts +4 -3
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import user from '@testing-library/user-event';
|
|
3
|
+
import { Validator } from 'fluentvalidation-ts';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { useForm } from 'react-hook-form';
|
|
6
|
+
import { fluentValidationResolver } from '../fluentvalidation-ts';
|
|
7
|
+
|
|
8
|
+
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
|
|
9
|
+
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
|
|
10
|
+
|
|
11
|
+
type FormData = {
|
|
12
|
+
username: string;
|
|
13
|
+
password: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
class FormDataValidator extends Validator<FormData> {
|
|
17
|
+
constructor() {
|
|
18
|
+
super();
|
|
19
|
+
|
|
20
|
+
this.ruleFor('username').notEmpty().withMessage(USERNAME_REQUIRED_MESSAGE);
|
|
21
|
+
this.ruleFor('password').notEmpty().withMessage(PASSWORD_REQUIRED_MESSAGE);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface Props {
|
|
26
|
+
onSubmit: (data: FormData) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function TestComponent({ onSubmit }: Props) {
|
|
30
|
+
const { register, handleSubmit } = useForm<FormData>({
|
|
31
|
+
resolver: fluentValidationResolver(new FormDataValidator()),
|
|
32
|
+
shouldUseNativeValidation: true,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
37
|
+
<input {...register('username')} placeholder="username" />
|
|
38
|
+
|
|
39
|
+
<input {...register('password')} placeholder="password" />
|
|
40
|
+
|
|
41
|
+
<button type="submit">submit</button>
|
|
42
|
+
</form>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test("form's native validation with fluentvalidation-ts", async () => {
|
|
47
|
+
const handleSubmit = vi.fn();
|
|
48
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
49
|
+
|
|
50
|
+
// username
|
|
51
|
+
let usernameField = screen.getByPlaceholderText(
|
|
52
|
+
/username/i,
|
|
53
|
+
) as HTMLInputElement;
|
|
54
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
55
|
+
expect(usernameField.validationMessage).toBe('');
|
|
56
|
+
|
|
57
|
+
// password
|
|
58
|
+
let passwordField = screen.getByPlaceholderText(
|
|
59
|
+
/password/i,
|
|
60
|
+
) as HTMLInputElement;
|
|
61
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
62
|
+
expect(passwordField.validationMessage).toBe('');
|
|
63
|
+
|
|
64
|
+
await user.click(screen.getByText(/submit/i));
|
|
65
|
+
|
|
66
|
+
// username
|
|
67
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
68
|
+
expect(usernameField.validity.valid).toBe(false);
|
|
69
|
+
expect(usernameField.validationMessage).toBe(USERNAME_REQUIRED_MESSAGE);
|
|
70
|
+
|
|
71
|
+
// password
|
|
72
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
73
|
+
expect(passwordField.validity.valid).toBe(false);
|
|
74
|
+
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE);
|
|
75
|
+
|
|
76
|
+
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
77
|
+
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
78
|
+
|
|
79
|
+
// username
|
|
80
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
81
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
82
|
+
expect(usernameField.validationMessage).toBe('');
|
|
83
|
+
|
|
84
|
+
// password
|
|
85
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
86
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
87
|
+
expect(passwordField.validationMessage).toBe('');
|
|
88
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import user from '@testing-library/user-event';
|
|
3
|
+
import { Validator } from 'fluentvalidation-ts';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { SubmitHandler, useForm } from 'react-hook-form';
|
|
6
|
+
import { fluentValidationResolver } from '../fluentvalidation-ts';
|
|
7
|
+
|
|
8
|
+
type FormData = {
|
|
9
|
+
username: string;
|
|
10
|
+
password: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
class FormDataValidator extends Validator<FormData> {
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
|
|
17
|
+
this.ruleFor('username')
|
|
18
|
+
.notEmpty()
|
|
19
|
+
.withMessage('username is a required field');
|
|
20
|
+
this.ruleFor('password')
|
|
21
|
+
.notEmpty()
|
|
22
|
+
.withMessage('password is a required field');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Props {
|
|
27
|
+
onSubmit: SubmitHandler<FormData>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function TestComponent({ onSubmit }: Props) {
|
|
31
|
+
const {
|
|
32
|
+
register,
|
|
33
|
+
formState: { errors },
|
|
34
|
+
handleSubmit,
|
|
35
|
+
} = useForm({
|
|
36
|
+
resolver: fluentValidationResolver(new FormDataValidator()), // Useful to check TypeScript regressions
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
41
|
+
<input {...register('username')} />
|
|
42
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
43
|
+
|
|
44
|
+
<input {...register('password')} />
|
|
45
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
46
|
+
|
|
47
|
+
<button type="submit">submit</button>
|
|
48
|
+
</form>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
test("form's validation with Yup and TypeScript's integration", async () => {
|
|
53
|
+
const handleSubmit = vi.fn();
|
|
54
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
55
|
+
|
|
56
|
+
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
|
57
|
+
|
|
58
|
+
await user.click(screen.getByText(/submit/i));
|
|
59
|
+
|
|
60
|
+
expect(screen.getByText(/username is a required field/i)).toBeInTheDocument();
|
|
61
|
+
expect(screen.getByText(/password is a required field/i)).toBeInTheDocument();
|
|
62
|
+
expect(handleSubmit).not.toHaveBeenCalled();
|
|
63
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Validator } from 'fluentvalidation-ts';
|
|
2
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
3
|
+
|
|
4
|
+
const beNumeric = (value: string | number | undefined) => !isNaN(Number(value));
|
|
5
|
+
|
|
6
|
+
export type Schema = {
|
|
7
|
+
username: string;
|
|
8
|
+
password: string;
|
|
9
|
+
repeatPassword: string;
|
|
10
|
+
accessToken?: string;
|
|
11
|
+
birthYear?: number;
|
|
12
|
+
email?: string;
|
|
13
|
+
tags?: string[];
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
like?: {
|
|
16
|
+
id: number;
|
|
17
|
+
name: string;
|
|
18
|
+
}[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type SchemaWithWhen = {
|
|
22
|
+
name: string;
|
|
23
|
+
value: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class SchemaValidator extends Validator<Schema> {
|
|
27
|
+
constructor() {
|
|
28
|
+
super();
|
|
29
|
+
|
|
30
|
+
this.ruleFor('username')
|
|
31
|
+
.notEmpty()
|
|
32
|
+
.matches(/^\w+$/)
|
|
33
|
+
.minLength(3)
|
|
34
|
+
.maxLength(30);
|
|
35
|
+
|
|
36
|
+
this.ruleFor('password')
|
|
37
|
+
.notEmpty()
|
|
38
|
+
.matches(/.*[A-Z].*/)
|
|
39
|
+
.withMessage('One uppercase character')
|
|
40
|
+
.matches(/.*[a-z].*/)
|
|
41
|
+
.withMessage('One lowercase character')
|
|
42
|
+
.matches(/.*\d.*/)
|
|
43
|
+
.withMessage('One number')
|
|
44
|
+
.matches(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'))
|
|
45
|
+
.withMessage('One special character')
|
|
46
|
+
.minLength(8)
|
|
47
|
+
.withMessage('Must be at least 8 characters in length');
|
|
48
|
+
|
|
49
|
+
this.ruleFor('repeatPassword')
|
|
50
|
+
.notEmpty()
|
|
51
|
+
.must((repeatPassword, data) => repeatPassword === data.password);
|
|
52
|
+
|
|
53
|
+
this.ruleFor('accessToken');
|
|
54
|
+
this.ruleFor('birthYear')
|
|
55
|
+
.must(beNumeric)
|
|
56
|
+
.inclusiveBetween(1900, 2013)
|
|
57
|
+
.when((birthYear) => birthYear != undefined);
|
|
58
|
+
|
|
59
|
+
this.ruleFor('email').emailAddress();
|
|
60
|
+
this.ruleFor('tags');
|
|
61
|
+
this.ruleFor('enabled');
|
|
62
|
+
|
|
63
|
+
this.ruleForEach('like').setValidator(() => new LikeValidator());
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class LikeValidator extends Validator<{
|
|
68
|
+
id: number;
|
|
69
|
+
name: string;
|
|
70
|
+
}> {
|
|
71
|
+
constructor() {
|
|
72
|
+
super();
|
|
73
|
+
|
|
74
|
+
this.ruleFor('id').notNull();
|
|
75
|
+
this.ruleFor('name').notEmpty().length(4, 4);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const validData = {
|
|
80
|
+
username: 'Doe',
|
|
81
|
+
password: 'Password123_',
|
|
82
|
+
repeatPassword: 'Password123_',
|
|
83
|
+
birthYear: 2000,
|
|
84
|
+
email: 'john@doe.com',
|
|
85
|
+
tags: ['tag1', 'tag2'],
|
|
86
|
+
enabled: true,
|
|
87
|
+
accesstoken: 'accesstoken',
|
|
88
|
+
like: [
|
|
89
|
+
{
|
|
90
|
+
id: 1,
|
|
91
|
+
name: 'name',
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
} as Schema;
|
|
95
|
+
|
|
96
|
+
export const invalidData = {
|
|
97
|
+
password: '___',
|
|
98
|
+
email: '',
|
|
99
|
+
birthYear: 'birthYear',
|
|
100
|
+
like: [{ id: 'z' }],
|
|
101
|
+
// Must be set to "unknown", otherwise typescript knows that it is invalid
|
|
102
|
+
} as unknown as Required<Schema>;
|
|
103
|
+
|
|
104
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
105
|
+
username: {
|
|
106
|
+
ref: { name: 'username' },
|
|
107
|
+
name: 'username',
|
|
108
|
+
},
|
|
109
|
+
password: {
|
|
110
|
+
ref: { name: 'password' },
|
|
111
|
+
name: 'password',
|
|
112
|
+
},
|
|
113
|
+
email: {
|
|
114
|
+
ref: { name: 'email' },
|
|
115
|
+
name: 'email',
|
|
116
|
+
},
|
|
117
|
+
birthday: {
|
|
118
|
+
ref: { name: 'birthday' },
|
|
119
|
+
name: 'birthday',
|
|
120
|
+
},
|
|
121
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`fluentValidationResolver > should return a single error from fluentValidationResolver when validation fails 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"errors": {
|
|
6
|
+
"birthYear": {
|
|
7
|
+
"message": "Value is not valid",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "validation",
|
|
10
|
+
},
|
|
11
|
+
"email": {
|
|
12
|
+
"message": "Not a valid email address",
|
|
13
|
+
"ref": {
|
|
14
|
+
"name": "email",
|
|
15
|
+
},
|
|
16
|
+
"type": "validation",
|
|
17
|
+
},
|
|
18
|
+
"password": {
|
|
19
|
+
"message": "One uppercase character",
|
|
20
|
+
"ref": {
|
|
21
|
+
"name": "password",
|
|
22
|
+
},
|
|
23
|
+
"type": "validation",
|
|
24
|
+
},
|
|
25
|
+
"repeatPassword": {
|
|
26
|
+
"message": "Value is not valid",
|
|
27
|
+
"ref": undefined,
|
|
28
|
+
"type": "validation",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
"values": {},
|
|
32
|
+
}
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
exports[`fluentValidationResolver > should return a single error from fluentValidationResolver with \`mode: sync\` when validation fails 1`] = `
|
|
36
|
+
{
|
|
37
|
+
"errors": {
|
|
38
|
+
"birthYear": {
|
|
39
|
+
"message": "Value is not valid",
|
|
40
|
+
"ref": undefined,
|
|
41
|
+
"type": "validation",
|
|
42
|
+
},
|
|
43
|
+
"email": {
|
|
44
|
+
"message": "Not a valid email address",
|
|
45
|
+
"ref": {
|
|
46
|
+
"name": "email",
|
|
47
|
+
},
|
|
48
|
+
"type": "validation",
|
|
49
|
+
},
|
|
50
|
+
"password": {
|
|
51
|
+
"message": "One uppercase character",
|
|
52
|
+
"ref": {
|
|
53
|
+
"name": "password",
|
|
54
|
+
},
|
|
55
|
+
"type": "validation",
|
|
56
|
+
},
|
|
57
|
+
"repeatPassword": {
|
|
58
|
+
"message": "Value is not valid",
|
|
59
|
+
"ref": undefined,
|
|
60
|
+
"type": "validation",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
"values": {},
|
|
64
|
+
}
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
exports[`fluentValidationResolver > should return all the errors from fluentValidationResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
68
|
+
{
|
|
69
|
+
"errors": {
|
|
70
|
+
"birthYear": {
|
|
71
|
+
"message": "Value is not valid",
|
|
72
|
+
"ref": undefined,
|
|
73
|
+
"type": "validation",
|
|
74
|
+
},
|
|
75
|
+
"email": {
|
|
76
|
+
"message": "Not a valid email address",
|
|
77
|
+
"ref": {
|
|
78
|
+
"name": "email",
|
|
79
|
+
},
|
|
80
|
+
"type": "validation",
|
|
81
|
+
},
|
|
82
|
+
"password": {
|
|
83
|
+
"message": "One uppercase character",
|
|
84
|
+
"ref": {
|
|
85
|
+
"name": "password",
|
|
86
|
+
},
|
|
87
|
+
"type": "validation",
|
|
88
|
+
},
|
|
89
|
+
"repeatPassword": {
|
|
90
|
+
"message": "Value is not valid",
|
|
91
|
+
"ref": undefined,
|
|
92
|
+
"type": "validation",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
"values": {},
|
|
96
|
+
}
|
|
97
|
+
`;
|
|
98
|
+
|
|
99
|
+
exports[`fluentValidationResolver > should return all the errors from fluentValidationResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
|
|
100
|
+
{
|
|
101
|
+
"errors": {
|
|
102
|
+
"birthYear": {
|
|
103
|
+
"message": "Value is not valid",
|
|
104
|
+
"ref": undefined,
|
|
105
|
+
"type": "validation",
|
|
106
|
+
},
|
|
107
|
+
"email": {
|
|
108
|
+
"message": "Not a valid email address",
|
|
109
|
+
"ref": {
|
|
110
|
+
"name": "email",
|
|
111
|
+
},
|
|
112
|
+
"type": "validation",
|
|
113
|
+
},
|
|
114
|
+
"password": {
|
|
115
|
+
"message": "One uppercase character",
|
|
116
|
+
"ref": {
|
|
117
|
+
"name": "password",
|
|
118
|
+
},
|
|
119
|
+
"type": "validation",
|
|
120
|
+
},
|
|
121
|
+
"repeatPassword": {
|
|
122
|
+
"message": "Value is not valid",
|
|
123
|
+
"ref": undefined,
|
|
124
|
+
"type": "validation",
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
"values": {},
|
|
128
|
+
}
|
|
129
|
+
`;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import { fluentValidationResolver } from '..';
|
|
3
|
+
import {
|
|
4
|
+
SchemaValidator,
|
|
5
|
+
fields,
|
|
6
|
+
invalidData,
|
|
7
|
+
validData,
|
|
8
|
+
} from './__fixtures__/data';
|
|
9
|
+
|
|
10
|
+
const shouldUseNativeValidation = false;
|
|
11
|
+
|
|
12
|
+
const validator = new SchemaValidator();
|
|
13
|
+
|
|
14
|
+
describe('fluentValidationResolver', () => {
|
|
15
|
+
it('should return values from fluentValidationResolver when validation pass', async () => {
|
|
16
|
+
const validatorSpy = vi.spyOn(validator, 'validate');
|
|
17
|
+
|
|
18
|
+
const result = await fluentValidationResolver(validator)(
|
|
19
|
+
validData,
|
|
20
|
+
undefined,
|
|
21
|
+
{
|
|
22
|
+
fields,
|
|
23
|
+
shouldUseNativeValidation,
|
|
24
|
+
},
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
expect(validatorSpy).toHaveBeenCalledTimes(1);
|
|
28
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should return values from fluentValidationResolver with `mode: sync` when validation pass', async () => {
|
|
32
|
+
const validatorSpy = vi.spyOn(validator, 'validate');
|
|
33
|
+
|
|
34
|
+
const result = await fluentValidationResolver(validator)(
|
|
35
|
+
validData,
|
|
36
|
+
undefined,
|
|
37
|
+
{ fields, shouldUseNativeValidation },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(validatorSpy).toHaveBeenCalledTimes(1);
|
|
41
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return a single error from fluentValidationResolver when validation fails', async () => {
|
|
45
|
+
const result = await fluentValidationResolver(validator)(
|
|
46
|
+
invalidData,
|
|
47
|
+
undefined,
|
|
48
|
+
{
|
|
49
|
+
fields,
|
|
50
|
+
shouldUseNativeValidation,
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result).toMatchSnapshot();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should return a single error from fluentValidationResolver with `mode: sync` when validation fails', async () => {
|
|
58
|
+
const validateSpy = vi.spyOn(validator, 'validate');
|
|
59
|
+
|
|
60
|
+
const result = await fluentValidationResolver(validator)(
|
|
61
|
+
invalidData,
|
|
62
|
+
undefined,
|
|
63
|
+
{ fields, shouldUseNativeValidation },
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(validateSpy).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(result).toMatchSnapshot();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should return all the errors from fluentValidationResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
71
|
+
const result = await fluentValidationResolver(validator)(
|
|
72
|
+
invalidData,
|
|
73
|
+
undefined,
|
|
74
|
+
{
|
|
75
|
+
fields,
|
|
76
|
+
criteriaMode: 'all',
|
|
77
|
+
shouldUseNativeValidation,
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(result).toMatchSnapshot();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should return all the errors from fluentValidationResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
85
|
+
const result = await fluentValidationResolver(validator)(
|
|
86
|
+
invalidData,
|
|
87
|
+
undefined,
|
|
88
|
+
{
|
|
89
|
+
fields,
|
|
90
|
+
criteriaMode: 'all',
|
|
91
|
+
shouldUseNativeValidation,
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(result).toMatchSnapshot();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should return values from fluentValidationResolver when validation pass & raw=true', async () => {
|
|
99
|
+
const schemaSpy = vi.spyOn(validator, 'validate');
|
|
100
|
+
|
|
101
|
+
const result = await fluentValidationResolver(validator)(
|
|
102
|
+
validData,
|
|
103
|
+
undefined,
|
|
104
|
+
{
|
|
105
|
+
fields,
|
|
106
|
+
shouldUseNativeValidation,
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
|
111
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
|
+
import {
|
|
3
|
+
AsyncValidator,
|
|
4
|
+
ValidationErrors,
|
|
5
|
+
Validator,
|
|
6
|
+
} from 'fluentvalidation-ts';
|
|
7
|
+
import { FieldError, FieldValues, Resolver } from 'react-hook-form';
|
|
8
|
+
|
|
9
|
+
function traverseObject<T>(
|
|
10
|
+
object: ValidationErrors<T>,
|
|
11
|
+
errors: Record<string, FieldError>,
|
|
12
|
+
parentIndices: (string | number)[] = [],
|
|
13
|
+
) {
|
|
14
|
+
for (const key in object) {
|
|
15
|
+
const currentIndex = [...parentIndices, key];
|
|
16
|
+
const currentValue = object[key];
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(currentValue)) {
|
|
19
|
+
currentValue.forEach((item: any, index: number) => {
|
|
20
|
+
traverseObject(item, errors, [...currentIndex, index]);
|
|
21
|
+
});
|
|
22
|
+
} else if (typeof currentValue === 'object' && currentValue !== null) {
|
|
23
|
+
traverseObject(currentValue, errors, currentIndex);
|
|
24
|
+
} else if (typeof currentValue === 'string') {
|
|
25
|
+
errors[currentIndex.join('.')] = {
|
|
26
|
+
type: 'validation',
|
|
27
|
+
message: currentValue,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const parseErrorSchema = <T>(
|
|
34
|
+
validationErrors: ValidationErrors<T>,
|
|
35
|
+
validateAllFieldCriteria: boolean,
|
|
36
|
+
) => {
|
|
37
|
+
if (validateAllFieldCriteria) {
|
|
38
|
+
// TODO: check this but i think its always one validation error
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const errors: Record<string, FieldError> = {};
|
|
42
|
+
traverseObject(validationErrors, errors);
|
|
43
|
+
|
|
44
|
+
return errors;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export function fluentValidationResolver<TFieldValues extends FieldValues>(
|
|
48
|
+
validator: Validator<TFieldValues>,
|
|
49
|
+
): Resolver<TFieldValues> {
|
|
50
|
+
return async (values, _context, options) => {
|
|
51
|
+
const validationResult = validator.validate(values);
|
|
52
|
+
const isValid = Object.keys(validationResult).length === 0;
|
|
53
|
+
|
|
54
|
+
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
|
55
|
+
|
|
56
|
+
return isValid
|
|
57
|
+
? {
|
|
58
|
+
values: values,
|
|
59
|
+
errors: {},
|
|
60
|
+
}
|
|
61
|
+
: {
|
|
62
|
+
values: {},
|
|
63
|
+
errors: toNestErrors(
|
|
64
|
+
parseErrorSchema(
|
|
65
|
+
validationResult,
|
|
66
|
+
!options.shouldUseNativeValidation &&
|
|
67
|
+
options.criteriaMode === 'all',
|
|
68
|
+
),
|
|
69
|
+
options,
|
|
70
|
+
),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function fluentAsyncValidationResolver<
|
|
76
|
+
TFieldValues extends FieldValues,
|
|
77
|
+
TValidator extends AsyncValidator<TFieldValues>,
|
|
78
|
+
>(validator: TValidator): Resolver<TFieldValues> {
|
|
79
|
+
return async (values, _context, options) => {
|
|
80
|
+
const validationResult = await validator.validateAsync(values);
|
|
81
|
+
const isValid = Object.keys(validationResult).length === 0;
|
|
82
|
+
|
|
83
|
+
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
|
84
|
+
|
|
85
|
+
return isValid
|
|
86
|
+
? {
|
|
87
|
+
values: values,
|
|
88
|
+
errors: {},
|
|
89
|
+
}
|
|
90
|
+
: {
|
|
91
|
+
values: {},
|
|
92
|
+
errors: toNestErrors(
|
|
93
|
+
parseErrorSchema(
|
|
94
|
+
validationResult,
|
|
95
|
+
!options.shouldUseNativeValidation &&
|
|
96
|
+
options.criteriaMode === 'all',
|
|
97
|
+
),
|
|
98
|
+
options,
|
|
99
|
+
),
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fluentvalidation-ts';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ValidationError } from 'io-ts';
|
|
2
|
-
import * as ReadonlyArray from 'fp-ts/ReadonlyArray';
|
|
3
2
|
import { ErrorObject } from './types';
|
|
4
3
|
declare const errorsToRecord: (validateAllFieldCriteria: boolean) => (validationErrors: ReadonlyArray<ValidationError>) => ErrorObject;
|
|
5
4
|
export default errorsToRecord;
|
package/io-ts/dist/io-ts.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("
|
|
1
|
+
var e=require("@hookform/resolvers"),n=require("fp-ts/Either"),t=require("fp-ts/function"),r=require("fp-ts/Option"),i=require("fp-ts/ReadonlyArray"),u=require("fp-ts/ReadonlyRecord"),o=require("fp-ts/Semigroup"),a=require("io-ts");function f(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach(function(t){if("default"!==t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})}}),n.default=e,n}var c=/*#__PURE__*/f(n),p=/*#__PURE__*/f(r),s=/*#__PURE__*/f(i),l=/*#__PURE__*/f(u),d=/*#__PURE__*/f(o);function m(){return m=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)({}).hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},m.apply(null,arguments)}var y=function(e){return e.reduce(function(e,n,r){return t.pipe(n,c.fold(function(e){return(r>0?".":"")+e},function(e){return"["+e+"]"}),function(n){return""+e+n})},"")},v=["path"],g=[a.TaggedUnionType,a.UnionType,a.IntersectionType,a.ExactType,a.RefinementType],O=function(e){var n,r=t.pipe(n=e.context,s.filterMapWithIndex(function(e,t){var r=e-1,i=-1===r?void 0:n[r];return void 0===i||g.some(function(e){return i.type instanceof e})?p.none:p.some(t)}),s.map(function(e){return e.key}),s.map(function(e){return t.pipe(e,function(e){return parseInt(e,10)},c.fromPredicate(t.not(Number.isNaN),function(){return e}))}),s.toArray,y);return{message:t.pipe(e.message,c.fromNullable(e.context),c.mapLeft(t.flow(s.last,p.map(function(e){return"expected "+e.type.name+" but got "+JSON.stringify(e.actual)}),p.getOrElseW(function(){return t.absurd("Error context is missing name")}))),c.getOrElseW(t.identity)),type:t.pipe(e.context,s.last,p.map(function(e){return e.type.name}),p.getOrElse(function(){return"unknown"})),path:r}},b=function(e){return t.pipe(e,s.map(function(e){var n;return(n={})[e.path]={type:e.type,message:e.message},n}),function(e){return d.fold({concat:function(e,n){return Object.assign({},n,e)}})({},e)})},h={concat:function(e,n){var t;return m({},n,{types:m({},e.types,(t={},t[e.type]=e.message,t[n.type]=n.message,t))})}},x=function(e){return t.pipe(l.fromFoldableMap(h,s.Foldable)(e,function(e){return[e.path,e]}),l.map(function(e){return function(e,n){if(null==e)return{};var t={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(n.indexOf(r)>=0)continue;t[r]=e[r]}return t}(e,v)}))};exports.ioTsResolver=function(n){return function(r,i,u){return t.pipe(r,n.decode,c.mapLeft((o=!u.shouldUseNativeValidation&&"all"===u.criteriaMode,function(e){var n=o?x:b;return t.pipe(e,s.map(O),n)})),c.mapLeft(function(n){return e.toNestErrors(n,u)}),c.fold(function(e){return{values:{},errors:e}},function(n){return u.shouldUseNativeValidation&&e.validateFieldsNatively({},u),{values:n,errors:{}}}));var o}};
|
|
2
2
|
//# sourceMappingURL=io-ts.js.map
|