@hookform/resolvers 2.9.3 → 2.9.4
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/computed-types/dist/computed-types.js +1 -1
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.mjs +1 -1
- package/computed-types/dist/computed-types.modern.js +1 -1
- package/computed-types/dist/computed-types.modern.js.map +1 -1
- package/computed-types/dist/computed-types.module.js +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/src/__tests__/Form.tsx +9 -0
- package/computed-types/src/__tests__/__fixtures__/data.ts +14 -0
- package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +22 -3
- package/computed-types/src/computed-types.ts +3 -11
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=require("@hookform/resolvers")
|
|
1
|
+
var r=require("@hookform/resolvers");exports.computedTypesResolver=function(e){return function(t,o,n){try{return Promise.resolve(function(o,s){try{var u=Promise.resolve(e(t)).then(function(e){return n.shouldUseNativeValidation&&r.validateFieldsNatively({},n),{errors:{},values:e}})}catch(r){return s(r)}return u&&u.then?u.then(void 0,s):u}(0,function(e){return{values:{},errors:r.toNestError((t=e,(t.errors||[]).reduce(function(r,e){return r[e.path.join(".")]={type:e.error.name,message:e.error.message},r},{})),n)};var t}))}catch(r){return Promise.reject(r)}}};
|
|
2
2
|
//# sourceMappingURL=computed-types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed-types.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"computed-types.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (computedTypesError: ValidationError) => {\n const parsedErrors: FieldErrors = {};\n return (computedTypesError.errors || []).reduce((acc, error) => {\n acc[error.path.join('.')] = {\n type: error.error.name,\n message: error.error.message,\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const computedTypesResolver: Resolver =\n (schema) => async (values, _, options) => {\n try {\n const data = await schema(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {},\n values: data,\n };\n } catch (error: any) {\n return {\n values: {},\n errors: toNestError(parseErrorSchema(error), options),\n };\n }\n };\n"],"names":["schema","values","_","options","data","shouldUseNativeValidation","validateFieldsNatively","errors","error","toNestError","computedTypesError","reduce","acc","path","join","type","name","message"],"mappings":"mEAkBE,SAACA,mBAAkBC,EAAQC,EAAGC,sEAEPH,EAAOC,kBAApBG,GAIN,OAFAD,EAAQE,2BAA6BC,yBAAuB,GAAIH,GAEzD,CACLI,OAAQ,GACRN,OAAQG,0EAEHI,GACP,MAAO,CACLP,OAAQ,GACRM,OAAQE,eA1BUC,EA0BmBF,GAxBnCE,EAAmBH,QAAU,IAAII,OAAO,SAACC,EAAKJ,GAMpD,OALAI,EAAIJ,EAAMK,KAAKC,KAAK,MAAQ,CAC1BC,KAAMP,EAAMA,MAAMQ,KAClBC,QAAST,EAAMA,MAAMS,SAGhBL,GAPyB,KAyBiBT,IA1B5B,IAACO,KAaxB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";var t=function
|
|
1
|
+
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";var t=function(t){return function(o,n,s){try{return Promise.resolve(function(e,n){try{var u=Promise.resolve(t(o)).then(function(e){return s.shouldUseNativeValidation&&r({},s),{errors:{},values:e}})}catch(r){return n(r)}return u&&u.then?u.then(void 0,n):u}(0,function(r){return{values:{},errors:e((t=r,(t.errors||[]).reduce(function(r,e){return r[e.path.join(".")]={type:e.error.name,message:e.error.message},r},{})),s)};var t}))}catch(r){return Promise.reject(r)}}};export{t as computedTypesResolver};
|
|
2
2
|
//# sourceMappingURL=computed-types.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";const o=
|
|
1
|
+
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";const o=o=>async(s,a,t)=>{try{const e=await o(s);return t.shouldUseNativeValidation&&r({},t),{errors:{},values:e}}catch(r){return{values:{},errors:e((n=r,(n.errors||[]).reduce((r,e)=>(r[e.path.join(".")]={type:e.error.name,message:e.error.message},r),{})),t)}}var n};export{o as computedTypesResolver};
|
|
2
2
|
//# sourceMappingURL=computed-types.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed-types.modern.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"computed-types.modern.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (computedTypesError: ValidationError) => {\n const parsedErrors: FieldErrors = {};\n return (computedTypesError.errors || []).reduce((acc, error) => {\n acc[error.path.join('.')] = {\n type: error.error.name,\n message: error.error.message,\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const computedTypesResolver: Resolver =\n (schema) => async (values, _, options) => {\n try {\n const data = await schema(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {},\n values: data,\n };\n } catch (error: any) {\n return {\n values: {},\n errors: toNestError(parseErrorSchema(error), options),\n };\n }\n };\n"],"names":["computedTypesResolver","schema","async","values","_","options","data","shouldUseNativeValidation","validateFieldsNatively","errors","error","toNestError","computedTypesError","reduce","acc","path","join","type","name","message"],"mappings":"8EAKA,MAYaA,EACVC,GAAWC,MAAOC,EAAQC,EAAGC,KAC5B,IACE,MAAMC,QAAaL,EAAOE,GAI1B,OAFAE,EAAQE,2BAA6BC,EAAuB,GAAIH,GAEzD,CACLI,OAAQ,GACRN,OAAQG,GAEV,MAAOI,GACP,MAAO,CACLP,OAAQ,GACRM,OAAQE,GA1BUC,EA0BmBF,GAxBnCE,EAAmBH,QAAU,IAAII,OAAO,CAACC,EAAKJ,KACpDI,EAAIJ,EAAMK,KAAKC,KAAK,MAAQ,CAC1BC,KAAMP,EAAMA,MAAMQ,KAClBC,QAAST,EAAMA,MAAMS,SAGhBL,GAPyB,KAyBiBT,IA1B3BO,IAAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";var t=function
|
|
1
|
+
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";var t=function(t){return function(o,n,s){try{return Promise.resolve(function(e,n){try{var u=Promise.resolve(t(o)).then(function(e){return s.shouldUseNativeValidation&&r({},s),{errors:{},values:e}})}catch(r){return n(r)}return u&&u.then?u.then(void 0,n):u}(0,function(r){return{values:{},errors:e((t=r,(t.errors||[]).reduce(function(r,e){return r[e.path.join(".")]={type:e.error.name,message:e.error.message},r},{})),s)};var t}))}catch(r){return Promise.reject(r)}}};export{t as computedTypesResolver};
|
|
2
2
|
//# sourceMappingURL=computed-types.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed-types.module.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"computed-types.module.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (computedTypesError: ValidationError) => {\n const parsedErrors: FieldErrors = {};\n return (computedTypesError.errors || []).reduce((acc, error) => {\n acc[error.path.join('.')] = {\n type: error.error.name,\n message: error.error.message,\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const computedTypesResolver: Resolver =\n (schema) => async (values, _, options) => {\n try {\n const data = await schema(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {},\n values: data,\n };\n } catch (error: any) {\n return {\n values: {},\n errors: toNestError(parseErrorSchema(error), options),\n };\n }\n };\n"],"names":["computedTypesResolver","schema","values","_","options","data","shouldUseNativeValidation","validateFieldsNatively","errors","error","toNestError","computedTypesError","reduce","acc","path","join","type","name","message"],"mappings":"8EAKA,IAYaA,EACX,SAACC,mBAAkBC,EAAQC,EAAGC,sEAEPH,EAAOC,kBAApBG,GAIN,OAFAD,EAAQE,2BAA6BC,EAAuB,GAAIH,GAEzD,CACLI,OAAQ,GACRN,OAAQG,0EAEHI,GACP,MAAO,CACLP,OAAQ,GACRM,OAAQE,GA1BUC,EA0BmBF,GAxBnCE,EAAmBH,QAAU,IAAII,OAAO,SAACC,EAAKJ,GAMpD,OALAI,EAAIJ,EAAMK,KAAKC,KAAK,MAAQ,CAC1BC,KAAMP,EAAMA,MAAMQ,KAClBC,QAAST,EAAMA,MAAMS,SAGhBL,GAPyB,KAyBiBT,IA1B5B,IAACO,KAaxB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],r):r((e||self).hookformResolversComputedTypes={},e.hookformResolvers)}(this,function(e,r){
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],r):r((e||self).hookformResolversComputedTypes={},e.hookformResolvers)}(this,function(e,r){e.computedTypesResolver=function(e){return function(o,t,n){try{return Promise.resolve(function(t,s){try{var i=Promise.resolve(e(o)).then(function(e){return n.shouldUseNativeValidation&&r.validateFieldsNatively({},n),{errors:{},values:e}})}catch(e){return s(e)}return i&&i.then?i.then(void 0,s):i}(0,function(e){return{values:{},errors:r.toNestError((o=e,(o.errors||[]).reduce(function(e,r){return e[r.path.join(".")]={type:r.error.name,message:r.error.message},e},{})),n)};var o}))}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=computed-types.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed-types.umd.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (
|
|
1
|
+
{"version":3,"file":"computed-types.umd.js","sources":["../src/computed-types.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationError } from 'computed-types';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (computedTypesError: ValidationError) => {\n const parsedErrors: FieldErrors = {};\n return (computedTypesError.errors || []).reduce((acc, error) => {\n acc[error.path.join('.')] = {\n type: error.error.name,\n message: error.error.message,\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const computedTypesResolver: Resolver =\n (schema) => async (values, _, options) => {\n try {\n const data = await schema(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n errors: {},\n values: data,\n };\n } catch (error: any) {\n return {\n values: {},\n errors: toNestError(parseErrorSchema(error), options),\n };\n }\n };\n"],"names":["schema","values","_","options","data","shouldUseNativeValidation","validateFieldsNatively","errors","error","toNestError","computedTypesError","reduce","acc","path","join","type","name","message"],"mappings":"2VAkBE,SAACA,mBAAkBC,EAAQC,EAAGC,sEAEPH,EAAOC,kBAApBG,GAIN,OAFAD,EAAQE,2BAA6BC,yBAAuB,GAAIH,GAEzD,CACLI,OAAQ,GACRN,OAAQG,0EAEHI,GACP,MAAO,CACLP,OAAQ,GACRM,OAAQE,eA1BUC,EA0BmBF,GAxBnCE,EAAmBH,QAAU,IAAII,OAAO,SAACC,EAAKJ,GAMpD,OALAI,EAAIJ,EAAMK,KAAKC,KAAK,MAAQ,CAC1BC,KAAMP,EAAMA,MAAMQ,KAClBC,QAAST,EAAMA,MAAMS,SAGhBL,GAPyB,KAyBiBT,IA1B5B,IAACO,KAaxB"}
|
|
@@ -8,6 +8,9 @@ import { computedTypesResolver } from '..';
|
|
|
8
8
|
const schema = Schema({
|
|
9
9
|
username: string.min(2).error('username field is required'),
|
|
10
10
|
password: string.min(2).error('password field is required'),
|
|
11
|
+
address: Schema({
|
|
12
|
+
zipCode: string.min(5).max(5).error('zipCode field is required'),
|
|
13
|
+
}),
|
|
11
14
|
});
|
|
12
15
|
|
|
13
16
|
type FormData = Type<typeof schema> & { unusedProperty: string };
|
|
@@ -33,6 +36,11 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
33
36
|
<input {...register('password')} />
|
|
34
37
|
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
35
38
|
|
|
39
|
+
<input {...register('address.zipCode')} />
|
|
40
|
+
{errors.address?.zipCode && (
|
|
41
|
+
<span role="alert">{errors.address.zipCode.message}</span>
|
|
42
|
+
)}
|
|
43
|
+
|
|
36
44
|
<button type="submit">submit</button>
|
|
37
45
|
</form>
|
|
38
46
|
);
|
|
@@ -50,5 +58,6 @@ test("form's validation with computed-types and TypeScript's integration", async
|
|
|
50
58
|
|
|
51
59
|
expect(screen.getByText(/username field is required/i)).toBeInTheDocument();
|
|
52
60
|
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
|
|
61
|
+
expect(screen.getByText(/zipCode field is required/i)).toBeInTheDocument();
|
|
53
62
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
54
63
|
});
|
|
@@ -26,6 +26,12 @@ export const schema = Schema({
|
|
|
26
26
|
name: string.min(4).max(4),
|
|
27
27
|
})
|
|
28
28
|
.optional(),
|
|
29
|
+
address: Schema({
|
|
30
|
+
city: string.min(3, 'Is required'),
|
|
31
|
+
zipCode: string
|
|
32
|
+
.min(5, 'Must be 5 characters long')
|
|
33
|
+
.max(5, 'Must be 5 characters long'),
|
|
34
|
+
}),
|
|
29
35
|
});
|
|
30
36
|
|
|
31
37
|
export const validData: Type<typeof schema> = {
|
|
@@ -43,6 +49,10 @@ export const validData: Type<typeof schema> = {
|
|
|
43
49
|
name: 'name',
|
|
44
50
|
},
|
|
45
51
|
],
|
|
52
|
+
address: {
|
|
53
|
+
city: 'Awesome city',
|
|
54
|
+
zipCode: '12345',
|
|
55
|
+
},
|
|
46
56
|
};
|
|
47
57
|
|
|
48
58
|
export const invalidData = {
|
|
@@ -50,6 +60,10 @@ export const invalidData = {
|
|
|
50
60
|
email: '',
|
|
51
61
|
birthYear: 'birthYear',
|
|
52
62
|
like: [{ id: 'z' }],
|
|
63
|
+
address: {
|
|
64
|
+
city: '',
|
|
65
|
+
zipCode: '123',
|
|
66
|
+
},
|
|
53
67
|
};
|
|
54
68
|
|
|
55
69
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
exports[`computedTypesResolver should return a single error from computedTypesResolver when validation fails 1`] = `
|
|
4
4
|
Object {
|
|
5
5
|
"errors": Object {
|
|
6
|
+
"address": Object {
|
|
7
|
+
"city": Object {
|
|
8
|
+
"message": "Is required",
|
|
9
|
+
"ref": undefined,
|
|
10
|
+
"type": "ValidationError",
|
|
11
|
+
},
|
|
12
|
+
"zipCode": Object {
|
|
13
|
+
"message": "Must be 5 characters long",
|
|
14
|
+
"ref": undefined,
|
|
15
|
+
"type": "ValidationError",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
6
18
|
"birthYear": Object {
|
|
7
19
|
"message": "Expect value to be \\"number\\"",
|
|
8
20
|
"ref": undefined,
|
|
@@ -21,9 +33,16 @@ Object {
|
|
|
21
33
|
"type": "ValidationError",
|
|
22
34
|
},
|
|
23
35
|
"like": Object {
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
"id": Object {
|
|
37
|
+
"message": "Expect value to be \\"number\\"",
|
|
38
|
+
"ref": undefined,
|
|
39
|
+
"type": "ValidationError",
|
|
40
|
+
},
|
|
41
|
+
"name": Object {
|
|
42
|
+
"message": "Expect value to be \\"string\\"",
|
|
43
|
+
"ref": undefined,
|
|
44
|
+
"type": "ValidationError",
|
|
45
|
+
},
|
|
27
46
|
},
|
|
28
47
|
"password": Object {
|
|
29
48
|
"message": "One uppercase character",
|
|
@@ -3,22 +3,14 @@ import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
|
|
|
3
3
|
import type { ValidationError } from 'computed-types';
|
|
4
4
|
import type { Resolver } from './types';
|
|
5
5
|
|
|
6
|
-
const parseErrorSchema = (
|
|
7
|
-
|
|
8
|
-
parsedErrors: FieldErrors = {},
|
|
9
|
-
path = '',
|
|
10
|
-
) => {
|
|
6
|
+
const parseErrorSchema = (computedTypesError: ValidationError) => {
|
|
7
|
+
const parsedErrors: FieldErrors = {};
|
|
11
8
|
return (computedTypesError.errors || []).reduce((acc, error) => {
|
|
12
|
-
|
|
13
|
-
const _path = path ? `${path}.${_currentPath}` : _currentPath;
|
|
14
|
-
|
|
15
|
-
acc[_path] = {
|
|
9
|
+
acc[error.path.join('.')] = {
|
|
16
10
|
type: error.error.name,
|
|
17
11
|
message: error.error.message,
|
|
18
12
|
};
|
|
19
13
|
|
|
20
|
-
parseErrorSchema(error.error, acc, _path);
|
|
21
|
-
|
|
22
14
|
return acc;
|
|
23
15
|
}, parsedErrors);
|
|
24
16
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hookform/resolvers",
|
|
3
3
|
"amdName": "hookformResolvers",
|
|
4
|
-
"version": "2.9.
|
|
4
|
+
"version": "2.9.4",
|
|
5
5
|
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types and Typanion",
|
|
6
6
|
"main": "dist/resolvers.js",
|
|
7
7
|
"module": "dist/resolvers.module.js",
|