@hookform/resolvers 2.0.0-beta.7 → 2.0.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 +19 -10
- package/dist/index.d.ts +1 -1
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.modern.js +1 -1
- package/dist/resolvers.modern.js.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/toNestError.d.ts +2 -0
- 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.js +1 -1
- package/joi/dist/joi.modern.js.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__/__fixtures__/data.ts +76 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +118 -0
- package/joi/src/__tests__/joi.ts +35 -88
- package/joi/src/joi.ts +47 -64
- package/joi/src/types.ts +1 -1
- package/package.json +14 -15
- package/superstruct/dist/superstruct.js +1 -1
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.mjs +1 -1
- package/superstruct/dist/superstruct.modern.js +1 -1
- package/superstruct/dist/superstruct.modern.js.map +1 -1
- package/superstruct/dist/superstruct.module.js +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/dist/types.d.ts +1 -1
- package/superstruct/package.json +2 -1
- package/superstruct/src/__tests__/__fixtures__/data.ts +73 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +26 -56
- package/superstruct/src/__tests__/superstruct.ts +6 -65
- package/superstruct/src/superstruct.ts +20 -53
- package/superstruct/src/types.ts +1 -1
- package/vest/dist/vest.js +1 -1
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/vest.mjs +1 -1
- package/vest/dist/vest.modern.js +1 -1
- package/vest/dist/vest.modern.js.map +1 -1
- package/vest/dist/vest.module.js +1 -1
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/package.json +3 -1
- package/vest/src/__tests__/__fixtures__/data.ts +67 -0
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +28 -0
- package/vest/src/__tests__/vest.ts +22 -95
- package/vest/src/types.ts +1 -1
- package/vest/src/vest.ts +31 -42
- package/yup/dist/types.d.ts +4 -3
- package/yup/dist/yup.js +1 -1
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.mjs +1 -1
- package/yup/dist/yup.modern.js +1 -1
- package/yup/dist/yup.modern.js.map +1 -1
- package/yup/dist/yup.module.js +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/package.json +2 -1
- package/yup/src/__tests__/__fixtures__/data.ts +65 -0
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +99 -13
- package/yup/src/__tests__/yup.ts +23 -97
- package/yup/src/types.ts +6 -3
- package/yup/src/yup.ts +30 -60
- 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.js +1 -1
- package/zod/dist/zod.modern.js.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__/__fixtures__/data.ts +68 -0
- package/zod/src/__tests__/__snapshots__/zod.ts.snap +146 -0
- package/zod/src/__tests__/zod.ts +19 -79
- package/zod/src/types.ts +1 -1
- package/zod/src/zod.ts +45 -50
- package/dist/convertArrayToPathName.d.ts +0 -1
|
@@ -1,137 +1,64 @@
|
|
|
1
|
-
import * as vest from 'vest';
|
|
2
1
|
import { vestResolver } from '..';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
vest.test('username', 'Must be longer than 3 chars', () => {
|
|
10
|
-
vest.enforce(data.username).longerThan(3);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
vest.test('deepObject.data', 'deepObject.data is required', () => {
|
|
14
|
-
vest.enforce(data.deepObject.data).isNotEmpty();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
vest.test('password', 'Password is required', () => {
|
|
18
|
-
vest.enforce(data.password).isNotEmpty();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
vest.test('password', 'Password must be at least 5 chars', () => {
|
|
22
|
-
vest.enforce(data.password).longerThanOrEquals(5);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
vest.test('password', 'Password must contain a digit', () => {
|
|
26
|
-
vest.enforce(data.password).matches(/[0-9]/);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
vest.test('password', 'Password must contain a symbol', () => {
|
|
30
|
-
vest.enforce(data.password).matches(/[^A-Za-z0-9]/);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
2
|
+
import {
|
|
3
|
+
invalidData,
|
|
4
|
+
validationSuite,
|
|
5
|
+
validData,
|
|
6
|
+
fields,
|
|
7
|
+
} from './__fixtures__/data';
|
|
33
8
|
|
|
34
9
|
describe('vestResolver', () => {
|
|
35
10
|
it('should return values from vestResolver when validation pass', async () => {
|
|
36
|
-
const data = {
|
|
37
|
-
username: 'asdda',
|
|
38
|
-
password: 'asddfg123!',
|
|
39
|
-
deepObject: {
|
|
40
|
-
data: 'test',
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
11
|
expect(
|
|
44
|
-
await vestResolver(validationSuite)(
|
|
12
|
+
await vestResolver(validationSuite)(validData, undefined, { fields }),
|
|
45
13
|
).toEqual({
|
|
46
|
-
values:
|
|
14
|
+
values: validData,
|
|
47
15
|
errors: {},
|
|
48
16
|
});
|
|
49
17
|
});
|
|
50
18
|
|
|
51
19
|
it('should return values from vestResolver with `mode: sync` when validation pass', async () => {
|
|
52
|
-
const data = {
|
|
53
|
-
username: 'asdda',
|
|
54
|
-
password: 'asddfg123!',
|
|
55
|
-
deepObject: {
|
|
56
|
-
data: 'test',
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
20
|
expect(
|
|
60
|
-
await vestResolver(validationSuite, undefined, {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
{ fields: {} },
|
|
64
|
-
),
|
|
21
|
+
await vestResolver(validationSuite, undefined, {
|
|
22
|
+
mode: 'sync',
|
|
23
|
+
})(validData, undefined, { fields }),
|
|
65
24
|
).toEqual({
|
|
66
|
-
values:
|
|
25
|
+
values: validData,
|
|
67
26
|
errors: {},
|
|
68
27
|
});
|
|
69
28
|
});
|
|
70
29
|
|
|
71
30
|
it('should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false', async () => {
|
|
72
|
-
const data = {
|
|
73
|
-
username: '',
|
|
74
|
-
password: 'a',
|
|
75
|
-
deepObject: {
|
|
76
|
-
data: '',
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
|
|
80
31
|
expect(
|
|
81
|
-
await vestResolver(validationSuite)(
|
|
32
|
+
await vestResolver(validationSuite)(invalidData, undefined, {
|
|
33
|
+
fields,
|
|
34
|
+
}),
|
|
82
35
|
).toMatchSnapshot();
|
|
83
36
|
});
|
|
84
37
|
|
|
85
38
|
it('should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false and `mode: sync`', async () => {
|
|
86
|
-
const data = {
|
|
87
|
-
username: '',
|
|
88
|
-
password: 'a',
|
|
89
|
-
deepObject: {
|
|
90
|
-
data: '',
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
|
|
94
39
|
expect(
|
|
95
|
-
await vestResolver(validationSuite, undefined, {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
{ fields: {} },
|
|
99
|
-
),
|
|
40
|
+
await vestResolver(validationSuite, undefined, {
|
|
41
|
+
mode: 'sync',
|
|
42
|
+
})(invalidData, undefined, { fields }),
|
|
100
43
|
).toMatchSnapshot();
|
|
101
44
|
});
|
|
102
45
|
|
|
103
46
|
it('should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true', async () => {
|
|
104
|
-
const data = {
|
|
105
|
-
username: '',
|
|
106
|
-
password: 'a',
|
|
107
|
-
deepObject: {
|
|
108
|
-
data: '',
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
|
|
112
47
|
expect(
|
|
113
48
|
await vestResolver(validationSuite)(
|
|
114
|
-
|
|
49
|
+
invalidData,
|
|
115
50
|
{},
|
|
116
|
-
{ fields
|
|
51
|
+
{ fields, criteriaMode: 'all' },
|
|
117
52
|
),
|
|
118
53
|
).toMatchSnapshot();
|
|
119
54
|
});
|
|
120
55
|
|
|
121
56
|
it('should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true and `mode: sync`', async () => {
|
|
122
|
-
const data = {
|
|
123
|
-
username: '',
|
|
124
|
-
password: 'a',
|
|
125
|
-
deepObject: {
|
|
126
|
-
data: '',
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
|
|
130
57
|
expect(
|
|
131
58
|
await vestResolver(validationSuite, undefined, { mode: 'sync' })(
|
|
132
|
-
|
|
59
|
+
invalidData,
|
|
133
60
|
{},
|
|
134
|
-
{ fields
|
|
61
|
+
{ fields, criteriaMode: 'all' },
|
|
135
62
|
),
|
|
136
63
|
).toMatchSnapshot();
|
|
137
64
|
});
|
package/vest/src/types.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type ICreateResult = ReturnType<typeof Vest.create>;
|
|
|
11
11
|
export type Resolver = (
|
|
12
12
|
schema: ICreateResult,
|
|
13
13
|
schemaOptions?: never,
|
|
14
|
-
factoryOptions?: { mode
|
|
14
|
+
factoryOptions?: { mode?: 'async' | 'sync' },
|
|
15
15
|
) => <TFieldValues extends FieldValues, TContext>(
|
|
16
16
|
values: UnpackNestedValue<TFieldValues>,
|
|
17
17
|
context: TContext | undefined,
|
package/vest/src/vest.ts
CHANGED
|
@@ -1,56 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toNestError } from '@hookform/resolvers';
|
|
2
|
+
import { FieldError } from 'react-hook-form';
|
|
2
3
|
import promisify from 'vest/promisify';
|
|
3
|
-
import { DraftResult, IVestResult } from 'vest/vestResult';
|
|
4
4
|
import type { VestErrors, Resolver } from './types';
|
|
5
5
|
|
|
6
6
|
const parseErrorSchema = (
|
|
7
7
|
vestError: VestErrors,
|
|
8
8
|
validateAllFieldCriteria: boolean,
|
|
9
9
|
) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
[
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
: {}),
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
}, {});
|
|
10
|
+
const errors: Record<string, FieldError> = {};
|
|
11
|
+
for (const path in vestError) {
|
|
12
|
+
if (!errors[path]) {
|
|
13
|
+
errors[path] = { message: vestError[path][0], type: '' };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (validateAllFieldCriteria) {
|
|
17
|
+
errors[path].types = vestError[path].reduce<Record<number, string>>(
|
|
18
|
+
(acc, message, index) => (acc[index] = message) && acc,
|
|
19
|
+
{},
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return errors;
|
|
29
24
|
};
|
|
30
25
|
|
|
31
26
|
export const vestResolver: Resolver = (
|
|
32
27
|
schema,
|
|
33
28
|
_,
|
|
34
|
-
|
|
35
|
-
) => async (values, _context,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
} else {
|
|
41
|
-
result = schema(values);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const errors = result.getErrors();
|
|
45
|
-
|
|
46
|
-
if (!result.hasErrors()) {
|
|
47
|
-
return { values, errors: {} };
|
|
48
|
-
}
|
|
29
|
+
resolverOptions = {},
|
|
30
|
+
) => async (values, _context, options) => {
|
|
31
|
+
const result =
|
|
32
|
+
resolverOptions.mode === 'sync'
|
|
33
|
+
? schema(values)
|
|
34
|
+
: await promisify(schema)(values);
|
|
49
35
|
|
|
50
|
-
return
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
36
|
+
return result.hasErrors()
|
|
37
|
+
? {
|
|
38
|
+
values: {},
|
|
39
|
+
errors: toNestError(
|
|
40
|
+
parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),
|
|
41
|
+
options.fields,
|
|
42
|
+
),
|
|
43
|
+
}
|
|
44
|
+
: { values, errors: {} };
|
|
56
45
|
};
|
package/yup/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { FieldValues, ResolverOptions, ResolverResult, UnpackNestedValue } from 'react-hook-form';
|
|
2
2
|
import * as Yup from 'yup';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import type Lazy from 'yup/lib/Lazy';
|
|
4
|
+
declare type Options<T extends Yup.AnyObjectSchema | Lazy<any>> = Parameters<T['validate']>[1];
|
|
5
|
+
export declare type Resolver = <T extends Yup.AnyObjectSchema | Lazy<any>>(schema: T, schemaOptions?: Options<T>, factoryOptions?: {
|
|
6
|
+
mode?: 'async' | 'sync';
|
|
6
7
|
}) => <TFieldValues extends FieldValues, TContext>(values: UnpackNestedValue<TFieldValues>, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
|
7
8
|
export {};
|
package/yup/dist/yup.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("react-hook-form");
|
|
1
|
+
var e=require("@hookform/resolvers"),r=require("react-hook-form");exports.yupResolver=function(t,o,n){return void 0===o&&(o={abortEarly:!1}),void 0===n&&(n={}),function(s,a,u){try{return Promise.resolve(function(e,r){try{var u=(o.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(t["sync"===n.mode?"validateSync":"validate"](s,Object.assign({},o,{context:a}))).then(function(e){return{values:e,errors:{}}}))}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(t){return{values:{},errors:e.toNestError((o=t,n="all"===u.criteriaMode,o.inner.reduce(function(e,t){return e[t.path]||(e[t.path]={message:t.message,type:t.type}),n&&(e[t.path]=r.appendErrors(t.path,n,e,t.type,t.message)),e},{})),u.fields)};var o,n}))}catch(e){return Promise.reject(e)}}};
|
|
2
2
|
//# sourceMappingURL=yup.js.map
|
package/yup/dist/yup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.js","sources":["../src/yup.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"yup.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup from 'yup';\nimport { toNestError } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver = (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n resolverOptions = {},\n) => async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](values, Object.assign({}, schemaOptions, { context }));\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(e, options.criteriaMode === 'all'),\n options.fields,\n ),\n };\n }\n};\n"],"names":["schema","schemaOptions","resolverOptions","abortEarly","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","result","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","appendErrors","fields"],"mappings":"sFAgCqC,SACnCA,EACAC,EAGAC,mBAHAD,IAAAA,EAAgB,CACdE,YAAY,aAEdD,IAAAA,EAAkB,aACRE,EAAQC,EAASC,uDAErBL,EAAcI,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iHAIiBX,EACM,SAAzBE,EAAgBU,KAAkB,eAAiB,YACnDR,EAAQS,OAAOC,OAAO,GAAIb,EAAe,CAAEI,QAAAA,oBAFvCU,GAIN,MAAO,CACLX,OAAQW,EACRC,OAAQ,4EAEHC,GACP,MAAO,CACLb,OAAQ,GACRY,OAAQE,eAhDZC,EAiDuBF,EAhDvBG,EAgDmD,QAAzBd,EAAQe,aA9C3BF,EAAMG,MAAMC,OAAmC,SAACC,EAAUL,GAe/D,OAdKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,IACFI,EAASL,EAAMM,MAASG,eACtBT,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNR,EAAMO,UAIHF,GACN,KA+BGlB,EAAQuB,SAnDS,IACvBV,EACAC,KAqBmC"}
|
package/yup/dist/yup.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{toNestError as e}from"@hookform/resolvers";import{appendErrors as t}from"react-hook-form";var r=function(r,o,n){return void 0===o&&(o={abortEarly:!1}),void 0===n&&(n={}),function(s,a,c){try{return Promise.resolve(function(e,t){try{var c=(o.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(r["sync"===n.mode?"validateSync":"validate"](s,Object.assign({},o,{context:a}))).then(function(e){return{values:e,errors:{}}}))}catch(e){return t(e)}return c&&c.then?c.then(void 0,t):c}(0,function(r){return{values:{},errors:e((o=r,n="all"===c.criteriaMode,o.inner.reduce(function(e,r){return e[r.path]||(e[r.path]={message:r.message,type:r.type}),n&&(e[r.path]=t(r.path,n,e,r.type,r.message)),e},{})),c.fields)};var o,n}))}catch(e){return Promise.reject(e)}}};export{r as yupResolver};
|
|
2
2
|
//# sourceMappingURL=yup.module.js.map
|
package/yup/dist/yup.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{toNestError as e}from"@hookform/resolvers";import{appendErrors as t}from"react-hook-form";const o=(o,r={abortEarly:!1},s={})=>async(a,n,c)=>{try{return r.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),{values:await o["sync"===s.mode?"validateSync":"validate"](a,Object.assign({},r,{context:n})),errors:{}}}catch(o){return{values:{},errors:e((p=o,l="all"===c.criteriaMode,p.inner.reduce((e,o)=>(e[o.path]||(e[o.path]={message:o.message,type:o.type}),l&&(e[o.path]=t(o.path,l,e,o.type,o.message)),e),{})),c.fields)}}var p,l};export{o as yupResolver};
|
|
2
2
|
//# sourceMappingURL=yup.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.modern.js","sources":["../src/yup.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"yup.modern.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup from 'yup';\nimport { toNestError } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver = (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n resolverOptions = {},\n) => async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](values, Object.assign({}, schemaOptions, { context }));\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(e, options.criteriaMode === 'all'),\n options.fields,\n ),\n };\n }\n};\n"],"names":["yupResolver","schema","schemaOptions","abortEarly","resolverOptions","async","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","appendErrors","fields"],"mappings":"iGASA,MAuBaA,EAAwB,CACnCC,EACAC,EAAgB,CACdC,YAAY,GAEdC,EAAkB,KACfC,MAAOC,EAAQC,EAASC,KAC3B,IAYE,OAXIN,EAAcK,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iGAQG,CACLP,aALmBL,EACM,SAAzBG,EAAgBU,KAAkB,eAAiB,YACnDR,EAAQS,OAAOC,OAAO,GAAId,EAAe,CAAEK,QAAAA,KAI3CU,OAAQ,IAEV,MAAOC,GACP,MAAO,CACLZ,OAAQ,GACRW,OAAQE,GAhDZC,EAiDuBF,EAhDvBG,EAgDmD,QAAzBb,EAAQc,aA9C3BF,EAAMG,MAAMC,OAAmC,CAACC,EAAUL,KAC1DK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,IACFI,EAASL,EAAMM,MAASG,EACtBT,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNR,EAAMO,UAIHF,GACN,KA+BGjB,EAAQsB,SAnDS,IACvBV,EACAC"}
|
package/yup/dist/yup.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{toNestError as e}from"@hookform/resolvers";import{appendErrors as t}from"react-hook-form";var r=function(r,o,n){return void 0===o&&(o={abortEarly:!1}),void 0===n&&(n={}),function(s,a,c){try{return Promise.resolve(function(e,t){try{var c=(o.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(r["sync"===n.mode?"validateSync":"validate"](s,Object.assign({},o,{context:a}))).then(function(e){return{values:e,errors:{}}}))}catch(e){return t(e)}return c&&c.then?c.then(void 0,t):c}(0,function(r){return{values:{},errors:e((o=r,n="all"===c.criteriaMode,o.inner.reduce(function(e,r){return e[r.path]||(e[r.path]={message:r.message,type:r.type}),n&&(e[r.path]=t(r.path,n,e,r.type,r.message)),e},{})),c.fields)};var o,n}))}catch(e){return Promise.reject(e)}}};export{r as yupResolver};
|
|
2
2
|
//# sourceMappingURL=yup.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.module.js","sources":["../src/yup.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"yup.module.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup from 'yup';\nimport { toNestError } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver = (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n resolverOptions = {},\n) => async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](values, Object.assign({}, schemaOptions, { context }));\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(e, options.criteriaMode === 'all'),\n options.fields,\n ),\n };\n }\n};\n"],"names":["yupResolver","schema","schemaOptions","resolverOptions","abortEarly","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","result","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","appendErrors","fields"],"mappings":"iGASA,IAuBaA,EAAwB,SACnCC,EACAC,EAGAC,mBAHAD,IAAAA,EAAgB,CACdE,YAAY,aAEdD,IAAAA,EAAkB,aACRE,EAAQC,EAASC,uDAErBL,EAAcI,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iHAIiBX,EACM,SAAzBE,EAAgBU,KAAkB,eAAiB,YACnDR,EAAQS,OAAOC,OAAO,GAAIb,EAAe,CAAEI,QAAAA,oBAFvCU,GAIN,MAAO,CACLX,OAAQW,EACRC,OAAQ,4EAEHC,GACP,MAAO,CACLb,OAAQ,GACRY,OAAQE,GAhDZC,EAiDuBF,EAhDvBG,EAgDmD,QAAzBd,EAAQe,aA9C3BF,EAAMG,MAAMC,OAAmC,SAACC,EAAUL,GAe/D,OAdKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,IACFI,EAASL,EAAMM,MAASG,EACtBT,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNR,EAAMO,UAIHF,GACN,KA+BGlB,EAAQuB,SAnDS,IACvBV,EACAC,KAqBmC"}
|
package/yup/dist/yup.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","react-hook-form"],o):o((e||self).hookformResolversYup={},e.hookformResolvers,e.reactHookForm)}(this,function(e,o,r){e.yupResolver=function(e,t,n){return void 0===t&&(t={abortEarly:!1}),void 0===n&&(n={}),function(s,i,u){try{return Promise.resolve(function(o,r){try{var u=(t.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(e["sync"===n.mode?"validateSync":"validate"](s,Object.assign({},t,{context:i}))).then(function(e){return{values:e,errors:{}}}))}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(e){return{values:{},errors:o.toNestError((t=e,n="all"===u.criteriaMode,t.inner.reduce(function(e,o){return e[o.path]||(e[o.path]={message:o.message,type:o.type}),n&&(e[o.path]=r.appendErrors(o.path,n,e,o.type,o.message)),e},{})),u.fields)};var t,n}))}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=yup.umd.js.map
|
package/yup/dist/yup.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.umd.js","sources":["../src/yup.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"yup.umd.js","sources":["../src/yup.ts"],"sourcesContent":["import Yup from 'yup';\nimport { toNestError } from '@hookform/resolvers';\nimport { appendErrors, FieldError } from 'react-hook-form';\nimport { Resolver } from './types';\n\n/**\n * Why `path!` ? because it could be `undefined` in some case\n * https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string\n */\nconst parseErrorSchema = (\n error: Yup.ValidationError,\n validateAllFieldCriteria: boolean,\n) => {\n return error.inner.reduce<Record<string, FieldError>>((previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n error.message,\n ) as FieldError;\n }\n\n return previous;\n }, {});\n};\n\nexport const yupResolver: Resolver = (\n schema,\n schemaOptions = {\n abortEarly: false,\n },\n resolverOptions = {},\n) => async (values, context, options) => {\n try {\n if (schemaOptions.context && process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n \"You should not used the yup options context. Please, use the 'useForm' context object instead\",\n );\n }\n\n const result = await schema[\n resolverOptions.mode === 'sync' ? 'validateSync' : 'validate'\n ](values, Object.assign({}, schemaOptions, { context }));\n\n return {\n values: result,\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(e, options.criteriaMode === 'all'),\n options.fields,\n ),\n };\n }\n};\n"],"names":["schema","schemaOptions","resolverOptions","abortEarly","values","context","options","process","env","NODE_ENV","console","warn","mode","Object","assign","result","errors","e","toNestError","error","validateAllFieldCriteria","criteriaMode","inner","reduce","previous","path","message","type","appendErrors","fields"],"mappings":"sYAgCqC,SACnCA,EACAC,EAGAC,mBAHAD,IAAAA,EAAgB,CACdE,YAAY,aAEdD,IAAAA,EAAkB,aACRE,EAAQC,EAASC,uDAErBL,EAAcI,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iHAIiBX,EACM,SAAzBE,EAAgBU,KAAkB,eAAiB,YACnDR,EAAQS,OAAOC,OAAO,GAAIb,EAAe,CAAEI,QAAAA,oBAFvCU,GAIN,MAAO,CACLX,OAAQW,EACRC,OAAQ,4EAEHC,GACP,MAAO,CACLb,OAAQ,GACRY,OAAQE,eAhDZC,EAiDuBF,EAhDvBG,EAgDmD,QAAzBd,EAAQe,aA9C3BF,EAAMG,MAAMC,OAAmC,SAACC,EAAUL,GAe/D,OAdKK,EAASL,EAAMM,QAClBD,EAASL,EAAMM,MAAS,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,OAG5DP,IACFI,EAASL,EAAMM,MAASG,eACtBT,EAAMM,KACNL,EACAI,EACAL,EAAMQ,KACNR,EAAMO,UAIHF,GACN,KA+BGlB,EAAQuB,SAnDS,IACvBV,EACAC,KAqBmC"}
|
package/yup/package.json
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import * as yup from 'yup';
|
|
3
|
+
|
|
4
|
+
export const schema = yup.object({
|
|
5
|
+
username: yup.string().matches(/^\w+$/).min(3).max(30).required(),
|
|
6
|
+
password: yup
|
|
7
|
+
.string()
|
|
8
|
+
.matches(/^[a-zA-Z0-9]{3,30}/)
|
|
9
|
+
.required(),
|
|
10
|
+
repeatPassword: yup.ref('password'),
|
|
11
|
+
accessToken: yup.string(),
|
|
12
|
+
birthYear: yup.number().min(1900).max(2013),
|
|
13
|
+
email: yup.string().email(),
|
|
14
|
+
tags: yup.array(yup.string()),
|
|
15
|
+
enabled: yup.boolean(),
|
|
16
|
+
like: yup.array().of(
|
|
17
|
+
yup.object({
|
|
18
|
+
id: yup.number().required(),
|
|
19
|
+
name: yup.string().length(4).required(),
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const validData: yup.InferType<typeof schema> = {
|
|
25
|
+
username: 'Doe',
|
|
26
|
+
password: 'Password123',
|
|
27
|
+
repeatPassword: 'Password123',
|
|
28
|
+
birthYear: 2000,
|
|
29
|
+
email: 'john@doe.com',
|
|
30
|
+
tags: ['tag1', 'tag2'],
|
|
31
|
+
enabled: true,
|
|
32
|
+
accessToken: 'accessToken',
|
|
33
|
+
like: [
|
|
34
|
+
{
|
|
35
|
+
id: 1,
|
|
36
|
+
name: 'name',
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const invalidData = {
|
|
42
|
+
password: '___',
|
|
43
|
+
email: '',
|
|
44
|
+
birthYear: 'birthYear',
|
|
45
|
+
like: [{ id: 'z' }],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
49
|
+
username: {
|
|
50
|
+
ref: { name: 'username' },
|
|
51
|
+
name: 'username',
|
|
52
|
+
},
|
|
53
|
+
password: {
|
|
54
|
+
ref: { name: 'password' },
|
|
55
|
+
name: 'password',
|
|
56
|
+
},
|
|
57
|
+
email: {
|
|
58
|
+
ref: { name: 'email' },
|
|
59
|
+
name: 'email',
|
|
60
|
+
},
|
|
61
|
+
birthday: {
|
|
62
|
+
ref: { name: 'birthday' },
|
|
63
|
+
name: 'birthday',
|
|
64
|
+
},
|
|
65
|
+
};
|