@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,77 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
object,
|
|
3
|
-
number,
|
|
4
|
-
string,
|
|
5
|
-
optional,
|
|
6
|
-
pattern,
|
|
7
|
-
size,
|
|
8
|
-
union,
|
|
9
|
-
min,
|
|
10
|
-
max,
|
|
11
|
-
Infer,
|
|
12
|
-
define,
|
|
13
|
-
array,
|
|
14
|
-
boolean,
|
|
15
|
-
} from 'superstruct';
|
|
16
1
|
import { superstructResolver } from '..';
|
|
17
|
-
|
|
18
|
-
const Password = define('Password', (value, ctx) =>
|
|
19
|
-
value === ctx.branch[0].password);
|
|
20
|
-
|
|
21
|
-
const schema = object({
|
|
22
|
-
username: size(pattern(string(), /^\w+$/), 3, 30),
|
|
23
|
-
password: pattern(string(), /^[a-zA-Z0-9]{3,30}/),
|
|
24
|
-
repeatPassword: Password,
|
|
25
|
-
accessToken: optional(union([string(), number()])),
|
|
26
|
-
birthYear: optional(max(min(number(), 1900), 2013)),
|
|
27
|
-
email: optional(pattern(string(), /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/)),
|
|
28
|
-
tags: array(string()),
|
|
29
|
-
enabled: boolean(),
|
|
30
|
-
});
|
|
2
|
+
import { invalidData, schema, validData, fields } from './__fixtures__/data';
|
|
31
3
|
|
|
32
4
|
describe('superstructResolver', () => {
|
|
33
5
|
it('should return values from superstructResolver when validation pass', async () => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
password: 'Password123',
|
|
37
|
-
repeatPassword: 'Password123',
|
|
38
|
-
birthYear: 2000,
|
|
39
|
-
email: 'john@doe.com',
|
|
40
|
-
tags: ['tag1', 'tag2'],
|
|
41
|
-
enabled: true,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const result = await superstructResolver(schema)(data, undefined, {
|
|
45
|
-
fields: {},
|
|
6
|
+
const result = await superstructResolver(schema)(validData, undefined, {
|
|
7
|
+
fields,
|
|
46
8
|
});
|
|
47
9
|
|
|
48
|
-
expect(result).toEqual({ errors: {}, values:
|
|
10
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
49
11
|
});
|
|
50
12
|
|
|
51
13
|
it('should return a single error from superstructResolver when validation fails', async () => {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
email: '',
|
|
55
|
-
birthYear: 'birthYear',
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const result = await superstructResolver(schema)(data, undefined, {
|
|
59
|
-
fields: {},
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
expect(result).toMatchSnapshot();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should return all the errors from superstructResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
66
|
-
const data = {
|
|
67
|
-
password: '___',
|
|
68
|
-
email: '',
|
|
69
|
-
birthYear: 'birthYear',
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const result = await superstructResolver(schema)(data, undefined, {
|
|
73
|
-
fields: {},
|
|
74
|
-
criteriaMode: 'all',
|
|
14
|
+
const result = await superstructResolver(schema)(invalidData, undefined, {
|
|
15
|
+
fields,
|
|
75
16
|
});
|
|
76
17
|
|
|
77
18
|
expect(result).toMatchSnapshot();
|
|
@@ -1,63 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FieldError } from 'react-hook-form';
|
|
2
|
+
import { toNestError } from '@hookform/resolvers';
|
|
3
|
+
|
|
2
4
|
import { StructError, validate } from 'superstruct';
|
|
3
|
-
// @ts-expect-error maybe fixed after the first publish ?
|
|
4
|
-
import { convertArrayToPathName } from '@hookform/resolvers';
|
|
5
5
|
import { Resolver } from './types';
|
|
6
6
|
|
|
7
|
-
const parseErrorSchema = (
|
|
8
|
-
error
|
|
9
|
-
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
...previous,
|
|
17
|
-
...(path
|
|
18
|
-
? previous[currentPath] && validateAllFieldCriteria
|
|
19
|
-
? {
|
|
20
|
-
[currentPath]: appendErrors(
|
|
21
|
-
currentPath,
|
|
22
|
-
validateAllFieldCriteria,
|
|
23
|
-
previous,
|
|
24
|
-
type || '',
|
|
25
|
-
message,
|
|
26
|
-
),
|
|
27
|
-
}
|
|
28
|
-
: {
|
|
29
|
-
[currentPath]: previous[currentPath] || {
|
|
30
|
-
message,
|
|
31
|
-
type,
|
|
32
|
-
...(validateAllFieldCriteria
|
|
33
|
-
? {
|
|
34
|
-
types: { [type || '']: message || true },
|
|
35
|
-
}
|
|
36
|
-
: {}),
|
|
37
|
-
},
|
|
38
|
-
}
|
|
39
|
-
: {}),
|
|
40
|
-
};
|
|
41
|
-
}, {});
|
|
7
|
+
const parseErrorSchema = (error: StructError) =>
|
|
8
|
+
error.failures().reduce<Record<string, FieldError>>(
|
|
9
|
+
(previous, error) =>
|
|
10
|
+
(previous[error.path.join('.')] = {
|
|
11
|
+
message: error.message,
|
|
12
|
+
type: error.type,
|
|
13
|
+
}) && previous,
|
|
14
|
+
{},
|
|
15
|
+
);
|
|
42
16
|
|
|
43
|
-
export const superstructResolver: Resolver = (schema,
|
|
17
|
+
export const superstructResolver: Resolver = (schema, resolverOptions) => (
|
|
44
18
|
values,
|
|
45
|
-
|
|
46
|
-
|
|
19
|
+
_,
|
|
20
|
+
options,
|
|
47
21
|
) => {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
if (errors != null) {
|
|
51
|
-
return {
|
|
52
|
-
values: {},
|
|
53
|
-
errors: transformToNestObject(
|
|
54
|
-
parseErrorSchema(errors, criteriaMode === 'all'),
|
|
55
|
-
),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
22
|
+
const result = validate(values, schema, resolverOptions);
|
|
58
23
|
|
|
59
24
|
return {
|
|
60
|
-
values: result,
|
|
61
|
-
errors:
|
|
25
|
+
values: result[1] || {},
|
|
26
|
+
errors: result[0]
|
|
27
|
+
? toNestError(parseErrorSchema(result[0]), options.fields)
|
|
28
|
+
: {},
|
|
62
29
|
};
|
|
63
30
|
};
|
package/superstruct/src/types.ts
CHANGED
package/vest/dist/vest.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=require("
|
|
1
|
+
var r=require("@hookform/resolvers");function e(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var t=e(require("vest/promisify")),o=function(r,e){var t={};for(var o in r)t[o]||(t[o]={message:r[o][0],type:""}),e&&(t[o].types=r[o].reduce(function(r,e,t){return(r[t]=e)&&r},{}));return t};exports.vestResolver=function(e,s,n){return void 0===n&&(n={}),function(s,u,i){try{var a=function(e){return e.hasErrors()?{values:{},errors:r.toNestError(o(e.getErrors(),"all"===i.criteriaMode),i.fields)}:{values:s,errors:{}}};return Promise.resolve("sync"===n.mode?a(e(s)):Promise.resolve(t.default(e)(s)).then(a))}catch(r){return Promise.reject(r)}}};
|
|
2
2
|
//# sourceMappingURL=vest.js.map
|
package/vest/dist/vest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vest.js","sources":["../src/vest.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"vest.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport promisify from 'vest/promisify';\nimport type { VestErrors, Resolver } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (const path in vestError) {\n if (!errors[path]) {\n errors[path] = { message: vestError[path][0], type: '' };\n }\n\n if (validateAllFieldCriteria) {\n errors[path].types = vestError[path].reduce<Record<number, string>>(\n (acc, message, index) => (acc[index] = message) && acc,\n {},\n );\n }\n }\n return errors;\n};\n\nexport const vestResolver: Resolver = (\n schema,\n _,\n resolverOptions = {},\n) => async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n return result.hasErrors()\n ? {\n values: {},\n errors: toNestError(\n parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),\n options.fields,\n ),\n }\n : { values, errors: {} };\n};\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","path","message","type","types","reduce","acc","index","schema","_","resolverOptions","values","_context","options","result","hasErrors","toNestError","getErrors","criteriaMode","fields","mode","promisify"],"mappings":"gJAKMA,EAAmB,SACvBC,EACAC,GAEA,IAAMC,EAAqC,GAC3C,IAAK,IAAMC,KAAQH,EACZE,EAAOC,KACVD,EAAOC,GAAQ,CAAEC,QAASJ,EAAUG,GAAM,GAAIE,KAAM,KAGlDJ,IACFC,EAAOC,GAAMG,MAAQN,EAAUG,GAAMI,OACnC,SAACC,EAAKJ,EAASK,UAAWD,EAAIC,GAASL,IAAYI,GACnD,KAIN,OAAON,wBAG6B,SACpCQ,EACAC,EACAC,mBAAAA,IAAAA,EAAkB,aACRC,EAAQC,EAAUC,sBACtBC,GAKN,OAAOA,EAAOC,YACV,CACEJ,OAAQ,GACRX,OAAQgB,cACNnB,EAAiBiB,EAAOG,YAAsC,QAAzBJ,EAAQK,cAC7CL,EAAQM,SAGZ,CAAER,OAAAA,EAAQX,OAAQ,4BAZK,SAAzBU,EAAgBU,OACZZ,EAAOG,oBACDU,UAAUb,EAAVa,CAAkBV,aARM"}
|
package/vest/dist/vest.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{toNestError as r}from"@hookform/resolvers";import e from"vest/promisify";var o=function(r,e){var o={};for(var t in r)o[t]||(o[t]={message:r[t][0],type:""}),e&&(o[t].types=r[t].reduce(function(r,e,o){return(r[o]=e)&&r},{}));return o},t=function(t,s,n){return void 0===n&&(n={}),function(s,i,u){try{var a=function(e){return e.hasErrors()?{values:{},errors:r(o(e.getErrors(),"all"===u.criteriaMode),u.fields)}:{values:s,errors:{}}};return Promise.resolve("sync"===n.mode?a(t(s)):Promise.resolve(e(t)(s)).then(a))}catch(r){return Promise.reject(r)}}};export{t as vestResolver};
|
|
2
2
|
//# sourceMappingURL=vest.module.js.map
|
package/vest/dist/vest.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{toNestError as r}from"@hookform/resolvers";import e from"vest/promisify";const o=(r,e)=>{const o={};for(const s in r)o[s]||(o[s]={message:r[s][0],type:""}),e&&(o[s].types=r[s].reduce((r,e,o)=>(r[o]=e)&&r,{}));return o},s=(s,t,a={})=>async(t,i,n)=>{const c="sync"===a.mode?s(t):await e(s)(t);return c.hasErrors()?{values:{},errors:r(o(c.getErrors(),"all"===n.criteriaMode),n.fields)}:{values:t,errors:{}}};export{s as vestResolver};
|
|
2
2
|
//# sourceMappingURL=vest.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vest.modern.js","sources":["../src/vest.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"vest.modern.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport promisify from 'vest/promisify';\nimport type { VestErrors, Resolver } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (const path in vestError) {\n if (!errors[path]) {\n errors[path] = { message: vestError[path][0], type: '' };\n }\n\n if (validateAllFieldCriteria) {\n errors[path].types = vestError[path].reduce<Record<number, string>>(\n (acc, message, index) => (acc[index] = message) && acc,\n {},\n );\n }\n }\n return errors;\n};\n\nexport const vestResolver: Resolver = (\n schema,\n _,\n resolverOptions = {},\n) => async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n return result.hasErrors()\n ? {\n values: {},\n errors: toNestError(\n parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),\n options.fields,\n ),\n }\n : { values, errors: {} };\n};\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","path","message","type","types","reduce","acc","index","vestResolver","schema","_","resolverOptions","async","values","_context","options","result","mode","promisify","hasErrors","toNestError","getErrors","criteriaMode","fields"],"mappings":"gFAKA,MAAMA,EAAmB,CACvBC,EACAC,KAEA,MAAMC,EAAqC,GAC3C,IAAK,MAAMC,KAAQH,EACZE,EAAOC,KACVD,EAAOC,GAAQ,CAAEC,QAASJ,EAAUG,GAAM,GAAIE,KAAM,KAGlDJ,IACFC,EAAOC,GAAMG,MAAQN,EAAUG,GAAMI,OACnC,CAACC,EAAKJ,EAASK,KAAWD,EAAIC,GAASL,IAAYI,EACnD,KAIN,OAAON,GAGIQ,EAAyB,CACpCC,EACAC,EACAC,EAAkB,KACfC,MAAOC,EAAQC,EAAUC,KAC5B,MAAMC,EACqB,SAAzBL,EAAgBM,KACZR,EAAOI,SACDK,EAAUT,EAAVS,CAAkBL,GAE9B,OAAOG,EAAOG,YACV,CACEN,OAAQ,GACRb,OAAQoB,EACNvB,EAAiBmB,EAAOK,YAAsC,QAAzBN,EAAQO,cAC7CP,EAAQQ,SAGZ,CAAEV,OAAAA,EAAQb,OAAQ"}
|
package/vest/dist/vest.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{toNestError as r}from"@hookform/resolvers";import e from"vest/promisify";var o=function(r,e){var o={};for(var t in r)o[t]||(o[t]={message:r[t][0],type:""}),e&&(o[t].types=r[t].reduce(function(r,e,o){return(r[o]=e)&&r},{}));return o},t=function(t,s,n){return void 0===n&&(n={}),function(s,i,u){try{var a=function(e){return e.hasErrors()?{values:{},errors:r(o(e.getErrors(),"all"===u.criteriaMode),u.fields)}:{values:s,errors:{}}};return Promise.resolve("sync"===n.mode?a(t(s)):Promise.resolve(e(t)(s)).then(a))}catch(r){return Promise.reject(r)}}};export{t as vestResolver};
|
|
2
2
|
//# sourceMappingURL=vest.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vest.module.js","sources":["../src/vest.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"vest.module.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport promisify from 'vest/promisify';\nimport type { VestErrors, Resolver } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (const path in vestError) {\n if (!errors[path]) {\n errors[path] = { message: vestError[path][0], type: '' };\n }\n\n if (validateAllFieldCriteria) {\n errors[path].types = vestError[path].reduce<Record<number, string>>(\n (acc, message, index) => (acc[index] = message) && acc,\n {},\n );\n }\n }\n return errors;\n};\n\nexport const vestResolver: Resolver = (\n schema,\n _,\n resolverOptions = {},\n) => async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n return result.hasErrors()\n ? {\n values: {},\n errors: toNestError(\n parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),\n options.fields,\n ),\n }\n : { values, errors: {} };\n};\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","path","message","type","types","reduce","acc","index","vestResolver","schema","_","resolverOptions","values","_context","options","result","hasErrors","toNestError","getErrors","criteriaMode","fields","mode","promisify"],"mappings":"gFAKA,IAAMA,EAAmB,SACvBC,EACAC,GAEA,IAAMC,EAAqC,GAC3C,IAAK,IAAMC,KAAQH,EACZE,EAAOC,KACVD,EAAOC,GAAQ,CAAEC,QAASJ,EAAUG,GAAM,GAAIE,KAAM,KAGlDJ,IACFC,EAAOC,GAAMG,MAAQN,EAAUG,GAAMI,OACnC,SAACC,EAAKJ,EAASK,UAAWD,EAAIC,GAASL,IAAYI,GACnD,KAIN,OAAON,GAGIQ,EAAyB,SACpCC,EACAC,EACAC,mBAAAA,IAAAA,EAAkB,aACRC,EAAQC,EAAUC,sBACtBC,GAKN,OAAOA,EAAOC,YACV,CACEJ,OAAQ,GACRZ,OAAQiB,EACNpB,EAAiBkB,EAAOG,YAAsC,QAAzBJ,EAAQK,cAC7CL,EAAQM,SAGZ,CAAER,OAAAA,EAAQZ,OAAQ,4BAZK,SAAzBW,EAAgBU,OACZZ,EAAOG,oBACDU,EAAUb,EAAVa,CAAkBV,aARM"}
|
package/vest/dist/vest.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("vest/promisify")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","vest/promisify"],r):r((e||self).hookformResolversVest={},e.hookformResolvers,e.promisify)}(this,function(e,r,o){function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=t(o),n=function(e,r){var o={};for(var t in e)o[t]||(o[t]={message:e[t][0],type:""}),r&&(o[t].types=e[t].reduce(function(e,r,o){return(e[o]=r)&&e},{}));return o};e.vestResolver=function(e,o,t){return void 0===t&&(t={}),function(o,i,f){try{var u=function(e){return e.hasErrors()?{values:{},errors:r.toNestError(n(e.getErrors(),"all"===f.criteriaMode),f.fields)}:{values:o,errors:{}}};return Promise.resolve("sync"===t.mode?u(e(o)):Promise.resolve(s.default(e)(o)).then(u))}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=vest.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vest.umd.js","sources":["../src/vest.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"vest.umd.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport promisify from 'vest/promisify';\nimport type { VestErrors, Resolver } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (const path in vestError) {\n if (!errors[path]) {\n errors[path] = { message: vestError[path][0], type: '' };\n }\n\n if (validateAllFieldCriteria) {\n errors[path].types = vestError[path].reduce<Record<number, string>>(\n (acc, message, index) => (acc[index] = message) && acc,\n {},\n );\n }\n }\n return errors;\n};\n\nexport const vestResolver: Resolver = (\n schema,\n _,\n resolverOptions = {},\n) => async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n return result.hasErrors()\n ? {\n values: {},\n errors: toNestError(\n parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),\n options.fields,\n ),\n }\n : { values, errors: {} };\n};\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","path","message","type","types","reduce","acc","index","schema","_","resolverOptions","values","_context","options","result","hasErrors","toNestError","getErrors","criteriaMode","fields","mode","promisify"],"mappings":"scAKMA,EAAmB,SACvBC,EACAC,GAEA,IAAMC,EAAqC,GAC3C,IAAK,IAAMC,KAAQH,EACZE,EAAOC,KACVD,EAAOC,GAAQ,CAAEC,QAASJ,EAAUG,GAAM,GAAIE,KAAM,KAGlDJ,IACFC,EAAOC,GAAMG,MAAQN,EAAUG,GAAMI,OACnC,SAACC,EAAKJ,EAASK,UAAWD,EAAIC,GAASL,IAAYI,GACnD,KAIN,OAAON,kBAG6B,SACpCQ,EACAC,EACAC,mBAAAA,IAAAA,EAAkB,aACRC,EAAQC,EAAUC,sBACtBC,GAKN,OAAOA,EAAOC,YACV,CACEJ,OAAQ,GACRX,OAAQgB,cACNnB,EAAiBiB,EAAOG,YAAsC,QAAzBJ,EAAQK,cAC7CL,EAAQM,SAGZ,CAAER,OAAAA,EAAQX,OAAQ,4BAZK,SAAzBU,EAAgBU,OACZZ,EAAOG,oBACDU,UAAUb,EAAVa,CAAkBV,aARM"}
|
package/vest/package.json
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import * as vest from 'vest';
|
|
3
|
+
|
|
4
|
+
export const validationSuite = vest.create('form', (data: any = {}) => {
|
|
5
|
+
vest.test('username', 'Username is required', () => {
|
|
6
|
+
vest.enforce(data.username).isNotEmpty();
|
|
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
|
+
});
|
|
33
|
+
|
|
34
|
+
export const validData = {
|
|
35
|
+
username: 'asdda',
|
|
36
|
+
password: 'asddfg123!',
|
|
37
|
+
deepObject: {
|
|
38
|
+
data: 'test',
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const invalidData = {
|
|
43
|
+
username: '',
|
|
44
|
+
password: 'a',
|
|
45
|
+
deepObject: {
|
|
46
|
+
data: '',
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
51
|
+
username: {
|
|
52
|
+
ref: { name: 'username' },
|
|
53
|
+
name: 'username',
|
|
54
|
+
},
|
|
55
|
+
password: {
|
|
56
|
+
ref: { name: 'password' },
|
|
57
|
+
name: 'password',
|
|
58
|
+
},
|
|
59
|
+
email: {
|
|
60
|
+
ref: { name: 'email' },
|
|
61
|
+
name: 'email',
|
|
62
|
+
},
|
|
63
|
+
birthday: {
|
|
64
|
+
ref: { name: 'birthday' },
|
|
65
|
+
name: 'birthday',
|
|
66
|
+
},
|
|
67
|
+
};
|
|
@@ -6,6 +6,7 @@ Object {
|
|
|
6
6
|
"deepObject": Object {
|
|
7
7
|
"data": Object {
|
|
8
8
|
"message": "deepObject.data is required",
|
|
9
|
+
"ref": undefined,
|
|
9
10
|
"type": "",
|
|
10
11
|
"types": Object {
|
|
11
12
|
"0": "deepObject.data is required",
|
|
@@ -14,6 +15,9 @@ Object {
|
|
|
14
15
|
},
|
|
15
16
|
"password": Object {
|
|
16
17
|
"message": "Password must be at least 5 chars",
|
|
18
|
+
"ref": Object {
|
|
19
|
+
"name": "password",
|
|
20
|
+
},
|
|
17
21
|
"type": "",
|
|
18
22
|
"types": Object {
|
|
19
23
|
"0": "Password must be at least 5 chars",
|
|
@@ -23,6 +27,9 @@ Object {
|
|
|
23
27
|
},
|
|
24
28
|
"username": Object {
|
|
25
29
|
"message": "Username is required",
|
|
30
|
+
"ref": Object {
|
|
31
|
+
"name": "username",
|
|
32
|
+
},
|
|
26
33
|
"type": "",
|
|
27
34
|
"types": Object {
|
|
28
35
|
"0": "Username is required",
|
|
@@ -40,6 +47,7 @@ Object {
|
|
|
40
47
|
"deepObject": Object {
|
|
41
48
|
"data": Object {
|
|
42
49
|
"message": "deepObject.data is required",
|
|
50
|
+
"ref": undefined,
|
|
43
51
|
"type": "",
|
|
44
52
|
"types": Object {
|
|
45
53
|
"0": "deepObject.data is required",
|
|
@@ -48,6 +56,9 @@ Object {
|
|
|
48
56
|
},
|
|
49
57
|
"password": Object {
|
|
50
58
|
"message": "Password must be at least 5 chars",
|
|
59
|
+
"ref": Object {
|
|
60
|
+
"name": "password",
|
|
61
|
+
},
|
|
51
62
|
"type": "",
|
|
52
63
|
"types": Object {
|
|
53
64
|
"0": "Password must be at least 5 chars",
|
|
@@ -57,6 +68,9 @@ Object {
|
|
|
57
68
|
},
|
|
58
69
|
"username": Object {
|
|
59
70
|
"message": "Username is required",
|
|
71
|
+
"ref": Object {
|
|
72
|
+
"name": "username",
|
|
73
|
+
},
|
|
60
74
|
"type": "",
|
|
61
75
|
"types": Object {
|
|
62
76
|
"0": "Username is required",
|
|
@@ -74,15 +88,22 @@ Object {
|
|
|
74
88
|
"deepObject": Object {
|
|
75
89
|
"data": Object {
|
|
76
90
|
"message": "deepObject.data is required",
|
|
91
|
+
"ref": undefined,
|
|
77
92
|
"type": "",
|
|
78
93
|
},
|
|
79
94
|
},
|
|
80
95
|
"password": Object {
|
|
81
96
|
"message": "Password must be at least 5 chars",
|
|
97
|
+
"ref": Object {
|
|
98
|
+
"name": "password",
|
|
99
|
+
},
|
|
82
100
|
"type": "",
|
|
83
101
|
},
|
|
84
102
|
"username": Object {
|
|
85
103
|
"message": "Username is required",
|
|
104
|
+
"ref": Object {
|
|
105
|
+
"name": "username",
|
|
106
|
+
},
|
|
86
107
|
"type": "",
|
|
87
108
|
},
|
|
88
109
|
},
|
|
@@ -96,15 +117,22 @@ Object {
|
|
|
96
117
|
"deepObject": Object {
|
|
97
118
|
"data": Object {
|
|
98
119
|
"message": "deepObject.data is required",
|
|
120
|
+
"ref": undefined,
|
|
99
121
|
"type": "",
|
|
100
122
|
},
|
|
101
123
|
},
|
|
102
124
|
"password": Object {
|
|
103
125
|
"message": "Password must be at least 5 chars",
|
|
126
|
+
"ref": Object {
|
|
127
|
+
"name": "password",
|
|
128
|
+
},
|
|
104
129
|
"type": "",
|
|
105
130
|
},
|
|
106
131
|
"username": Object {
|
|
107
132
|
"message": "Username is required",
|
|
133
|
+
"ref": Object {
|
|
134
|
+
"name": "username",
|
|
135
|
+
},
|
|
108
136
|
"type": "",
|
|
109
137
|
},
|
|
110
138
|
},
|