@hookform/resolvers 3.1.0 → 3.1.1
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/package.json +1 -1
- package/yup/dist/index.d.ts +0 -1
- package/yup/dist/yup.d.ts +13 -2
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.mjs +1 -1
- package/yup/dist/yup.modern.mjs +1 -1
- package/yup/dist/yup.modern.mjs.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.map +1 -1
- package/yup/src/__tests__/Form.tsx +4 -4
- package/yup/src/__tests__/__fixtures__/data.ts +4 -3
- package/yup/src/index.ts +0 -1
- package/yup/src/yup.ts +23 -5
- package/yup/dist/types.d.ts +0 -15
- package/yup/src/types.ts +0 -24
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hookform/resolvers",
|
|
3
3
|
"amdName": "hookformResolvers",
|
|
4
|
-
"version": "3.1.
|
|
4
|
+
"version": "3.1.1",
|
|
5
5
|
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype and Typanion",
|
|
6
6
|
"main": "dist/resolvers.js",
|
|
7
7
|
"module": "dist/resolvers.module.js",
|
package/yup/dist/index.d.ts
CHANGED
package/yup/dist/yup.d.ts
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import * as Yup from 'yup';
|
|
2
|
+
import { FieldValues, Resolver } from 'react-hook-form';
|
|
3
|
+
export declare function yupResolver<TFieldValues extends FieldValues>(schema: Yup.ObjectSchema<TFieldValues>, schemaOptions?: Parameters<(typeof schema)['validate']>[1], resolverOptions?: {
|
|
4
|
+
/**
|
|
5
|
+
* @default async
|
|
6
|
+
*/
|
|
7
|
+
mode?: 'async' | 'sync';
|
|
8
|
+
/**
|
|
9
|
+
* Return the raw input values rather than the parsed values.
|
|
10
|
+
* @default false
|
|
11
|
+
*/
|
|
12
|
+
raw?: boolean;
|
|
13
|
+
}): Resolver<TFieldValues>;
|
package/yup/dist/yup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.js","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"yup.js","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n appendErrors,\n FieldError,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\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>>(\n (previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n },\n {},\n );\n};\n\nexport function yupResolver<TFieldValues extends FieldValues>(\n schema: Yup.ObjectSchema<TFieldValues>,\n schemaOptions: Parameters<(typeof schema)['validate']>[1] = {},\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'async' | 'sync';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<TFieldValues> {\n return 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 ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result,\n errors: {},\n };\n } catch (e: any) {\n if (!e.inner) {\n throw e;\n }\n\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","context","options","Promise","resolve","process","env","NODE_ENV","console","warn","mode","Object","assign","abortEarly","then","result","shouldUseNativeValidation","validateFieldsNatively","raw","errors","_catch","e","inner","toNestError","error","validateAllFieldCriteria","criteriaMode","reduce","previous","path","message","type","types","messages","appendErrors","concat","reject"],"mappings":"+FA6CEA,EACAC,EACAC,GAYA,gBAbAD,IAAAA,EAA4D,CAAE,YAC9DC,IAAAA,EAUI,aAEUC,EAAQC,EAASC,OAAWC,OAAAA,QAAAC,iCAElCN,EAAcG,SAAoC,gBAAzBI,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iGAEHN,QAAAC,QAEoBP,EACM,SAAzBE,EAAgBW,KAAkB,eAAiB,YAEnDV,EACAW,OAAOC,OAAO,CAAEC,YAAY,GAASf,EAAe,CAAEG,QAAAA,MACvDa,KAAA,SALKC,GASN,OAFAb,EAAQc,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEf,GAEzD,CACLF,OAAQD,EAAgBmB,IAAMlB,EAASe,EACvCI,OAAQ,CAAA,EACR,6DArBoCC,CACpC,EAqBH,SAAQC,GACP,IAAKA,EAAEC,MACL,MAAMD,EAGR,MAAO,CACLrB,OAAQ,CAAE,EACVmB,OAAQI,EAAAA,aA1EdC,EA4EUH,EA3EVI,GA4EWvB,EAAQc,2BACkB,QAAzBd,EAAQwB,cA3EZF,EAAMF,OAAS,IAAIK,OACzB,SAACC,EAAUJ,GAKT,GAJKI,EAASJ,EAAMK,QAClBD,EAASJ,EAAMK,MAAS,CAAEC,QAASN,EAAMM,QAASC,KAAMP,EAAMO,OAG5DN,EAA0B,CAC5B,IAAMO,EAAQJ,EAASJ,EAAMK,MAAOG,MAC9BC,EAAWD,GAASA,EAAMR,EAAMO,MAEtCH,EAASJ,EAAMK,MAASK,EAAAA,aACtBV,EAAMK,KACNJ,EACAG,EACAJ,EAAMO,KACNE,EACK,GAAgBE,OAAOF,EAAsBT,EAAMM,SACpDN,EAAMM,QAEb,CAED,OAAOF,CACT,EACA,KAsDM1B,IAjFe,IACvBsB,EACAC,CAkFG,GACH,CAAC,MAAAJ,GAAA,OAAAlB,QAAAiC,OAAAf,EACH,CAAA,CAAA"}
|
package/yup/dist/yup.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";
|
|
1
|
+
import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";function o(o,n,a){return void 0===n&&(n={}),void 0===a&&(a={}),function(s,i,c){try{return Promise.resolve(function(t,r){try{var u=(n.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(o["sync"===a.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},n,{context:i}))).then(function(t){return c.shouldUseNativeValidation&&e({},c),{values:a.raw?s:t,errors:{}}}))}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(e){if(!e.inner)throw e;return{values:{},errors:t((o=e,n=!c.shouldUseNativeValidation&&"all"===c.criteriaMode,(o.inner||[]).reduce(function(e,t){if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),n){var o=e[t.path].types,a=o&&o[t.type];e[t.path]=r(t.path,n,e,t.type,a?[].concat(a,t.message):t.message)}return e},{})),c)};var o,n}))}catch(e){return Promise.reject(e)}}}export{o as yupResolver};
|
|
2
2
|
//# sourceMappingURL=yup.module.js.map
|
package/yup/dist/yup.modern.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as o}from"react-hook-form";
|
|
1
|
+
import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as o}from"react-hook-form";function r(r,a={},s={}){return async(n,c,i)=>{try{a.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead");const t=await r["sync"===s.mode?"validateSync":"validate"](n,Object.assign({abortEarly:!1},a,{context:c}));return i.shouldUseNativeValidation&&e({},i),{values:s.raw?n:t,errors:{}}}catch(e){if(!e.inner)throw e;return{values:{},errors:t((p=e,l=!i.shouldUseNativeValidation&&"all"===i.criteriaMode,(p.inner||[]).reduce((e,t)=>{if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),l){const r=e[t.path].types,a=r&&r[t.type];e[t.path]=o(t.path,l,e,t.type,a?[].concat(a,t.message):t.message)}return e},{})),i)}}var p,l}}export{r as yupResolver};
|
|
2
2
|
//# sourceMappingURL=yup.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.modern.mjs","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"yup.modern.mjs","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n appendErrors,\n FieldError,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\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>>(\n (previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n },\n {},\n );\n};\n\nexport function yupResolver<TFieldValues extends FieldValues>(\n schema: Yup.ObjectSchema<TFieldValues>,\n schemaOptions: Parameters<(typeof schema)['validate']>[1] = {},\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'async' | 'sync';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<TFieldValues> {\n return 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 ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result,\n errors: {},\n };\n } catch (e: any) {\n if (!e.inner) {\n throw e;\n }\n\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n}\n"],"names":["yupResolver","schema","schemaOptions","resolverOptions","async","values","context","options","process","env","NODE_ENV","console","warn","result","mode","Object","assign","abortEarly","shouldUseNativeValidation","validateFieldsNatively","raw","errors","e","inner","toNestError","error","validateAllFieldCriteria","criteriaMode","reduce","previous","path","message","type","types","messages","appendErrors","concat","parseErrorSchema"],"mappings":"sIA4CgBA,EACdC,EACAC,EAA4D,CAAE,EAC9DC,EAUI,CAAA,GAEJ,OAAOC,MAAOC,EAAQC,EAASC,KAC7B,IACML,EAAcI,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iGAIJ,MAAMC,QAAeZ,EACM,SAAzBE,EAAgBW,KAAkB,eAAiB,YAEnDT,EACAU,OAAOC,OAAO,CAAEC,YAAY,GAASf,EAAe,CAAEI,aAKxD,OAFAC,EAAQW,2BAA6BC,EAAuB,CAAA,EAAIZ,GAEzD,CACLF,OAAQF,EAAgBiB,IAAMf,EAASQ,EACvCQ,OAAQ,GAEX,CAAC,MAAOC,GACP,IAAKA,EAAEC,MACL,MAAMD,EAGR,MAAO,CACLjB,OAAQ,CAAE,EACVgB,OAAQG,GA1EdC,EA4EUH,EA3EVI,GA4EWnB,EAAQW,2BACkB,QAAzBX,EAAQoB,cA3EZF,EAAMF,OAAS,IAAIK,OACzB,CAACC,EAAUJ,KAKT,GAJKI,EAASJ,EAAMK,QAClBD,EAASJ,EAAMK,MAAS,CAAEC,QAASN,EAAMM,QAASC,KAAMP,EAAMO,OAG5DN,EAA0B,CAC5B,MAAMO,EAAQJ,EAASJ,EAAMK,MAAOG,MAC9BC,EAAWD,GAASA,EAAMR,EAAMO,MAEtCH,EAASJ,EAAMK,MAASK,EACtBV,EAAMK,KACNJ,EACAG,EACAJ,EAAMO,KACNE,EACK,GAAgBE,OAAOF,EAAsBT,EAAMM,SACpDN,EAAMM,QAEb,CAED,OAAOF,GAET,CAAE,IAsDItB,GAGL,CApFoB8B,IACvBZ,EACAC,CAkFG,CAEL"}
|
package/yup/dist/yup.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";
|
|
1
|
+
import{validateFieldsNatively as e,toNestError as t}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";function o(o,n,a){return void 0===n&&(n={}),void 0===a&&(a={}),function(s,i,c){try{return Promise.resolve(function(t,r){try{var u=(n.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(o["sync"===a.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},n,{context:i}))).then(function(t){return c.shouldUseNativeValidation&&e({},c),{values:a.raw?s:t,errors:{}}}))}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(e){if(!e.inner)throw e;return{values:{},errors:t((o=e,n=!c.shouldUseNativeValidation&&"all"===c.criteriaMode,(o.inner||[]).reduce(function(e,t){if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),n){var o=e[t.path].types,a=o&&o[t.type];e[t.path]=r(t.path,n,e,t.type,a?[].concat(a,t.message):t.message)}return e},{})),c)};var o,n}))}catch(e){return Promise.reject(e)}}}export{o as yupResolver};
|
|
2
2
|
//# sourceMappingURL=yup.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.module.js","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"yup.module.js","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n appendErrors,\n FieldError,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\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>>(\n (previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n },\n {},\n );\n};\n\nexport function yupResolver<TFieldValues extends FieldValues>(\n schema: Yup.ObjectSchema<TFieldValues>,\n schemaOptions: Parameters<(typeof schema)['validate']>[1] = {},\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'async' | 'sync';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<TFieldValues> {\n return 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 ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result,\n errors: {},\n };\n } catch (e: any) {\n if (!e.inner) {\n throw e;\n }\n\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n}\n"],"names":["yupResolver","schema","schemaOptions","resolverOptions","values","context","options","Promise","resolve","process","env","NODE_ENV","console","warn","mode","Object","assign","abortEarly","then","result","shouldUseNativeValidation","validateFieldsNatively","raw","errors","_catch","e","inner","toNestError","error","validateAllFieldCriteria","criteriaMode","reduce","previous","path","message","type","types","messages","appendErrors","concat","reject"],"mappings":"sIA4CgBA,EACdC,EACAC,EACAC,GAYA,gBAbAD,IAAAA,EAA4D,CAAE,YAC9DC,IAAAA,EAUI,aAEUC,EAAQC,EAASC,OAAWC,OAAAA,QAAAC,iCAElCN,EAAcG,SAAoC,gBAAzBI,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iGAEHN,QAAAC,QAEoBP,EACM,SAAzBE,EAAgBW,KAAkB,eAAiB,YAEnDV,EACAW,OAAOC,OAAO,CAAEC,YAAY,GAASf,EAAe,CAAEG,QAAAA,MACvDa,KAAA,SALKC,GASN,OAFAb,EAAQc,2BAA6BC,EAAuB,CAAE,EAAEf,GAEzD,CACLF,OAAQD,EAAgBmB,IAAMlB,EAASe,EACvCI,OAAQ,CAAA,EACR,6DArBoCC,CACpC,EAqBH,SAAQC,GACP,IAAKA,EAAEC,MACL,MAAMD,EAGR,MAAO,CACLrB,OAAQ,CAAE,EACVmB,OAAQI,GA1EdC,EA4EUH,EA3EVI,GA4EWvB,EAAQc,2BACkB,QAAzBd,EAAQwB,cA3EZF,EAAMF,OAAS,IAAIK,OACzB,SAACC,EAAUJ,GAKT,GAJKI,EAASJ,EAAMK,QAClBD,EAASJ,EAAMK,MAAS,CAAEC,QAASN,EAAMM,QAASC,KAAMP,EAAMO,OAG5DN,EAA0B,CAC5B,IAAMO,EAAQJ,EAASJ,EAAMK,MAAOG,MAC9BC,EAAWD,GAASA,EAAMR,EAAMO,MAEtCH,EAASJ,EAAMK,MAASK,EACtBV,EAAMK,KACNJ,EACAG,EACAJ,EAAMO,KACNE,EACK,GAAgBE,OAAOF,EAAsBT,EAAMM,SACpDN,EAAMM,QAEb,CAED,OAAOF,CACT,EACA,KAsDM1B,IAjFe,IACvBsB,EACAC,CAkFG,GACH,CAAC,MAAAJ,GAAA,OAAAlB,QAAAiC,OAAAf,EACH,CAAA,CAAA"}
|
package/yup/dist/yup.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yup.umd.js","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"yup.umd.js","sources":["../src/yup.ts"],"sourcesContent":["import * as Yup from 'yup';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n appendErrors,\n FieldError,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\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>>(\n (previous, error) => {\n if (!previous[error.path!]) {\n previous[error.path!] = { message: error.message, type: error.type! };\n }\n\n if (validateAllFieldCriteria) {\n const types = previous[error.path!].types;\n const messages = types && types[error.type!];\n\n previous[error.path!] = appendErrors(\n error.path!,\n validateAllFieldCriteria,\n previous,\n error.type!,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return previous;\n },\n {},\n );\n};\n\nexport function yupResolver<TFieldValues extends FieldValues>(\n schema: Yup.ObjectSchema<TFieldValues>,\n schemaOptions: Parameters<(typeof schema)['validate']>[1] = {},\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'async' | 'sync';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<TFieldValues> {\n return 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 ](\n values,\n Object.assign({ abortEarly: false }, schemaOptions, { context }),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return {\n values: resolverOptions.raw ? values : result,\n errors: {},\n };\n } catch (e: any) {\n if (!e.inner) {\n throw e;\n }\n\n return {\n values: {},\n errors: toNestError(\n parseErrorSchema(\n e,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","context","options","Promise","resolve","process","env","NODE_ENV","console","warn","mode","Object","assign","abortEarly","then","result","shouldUseNativeValidation","validateFieldsNatively","raw","errors","_catch","e","inner","toNestError","error","validateAllFieldCriteria","criteriaMode","reduce","previous","path","message","type","types","messages","appendErrors","concat","reject"],"mappings":"+YA6CEA,EACAC,EACAC,GAYA,gBAbAD,IAAAA,EAA4D,CAAE,YAC9DC,IAAAA,EAUI,aAEUC,EAAQC,EAASC,OAAWC,OAAAA,QAAAC,iCAElCN,EAAcG,SAAoC,gBAAzBI,QAAQC,IAAIC,UAEvCC,QAAQC,KACN,iGAEHN,QAAAC,QAEoBP,EACM,SAAzBE,EAAgBW,KAAkB,eAAiB,YAEnDV,EACAW,OAAOC,OAAO,CAAEC,YAAY,GAASf,EAAe,CAAEG,QAAAA,MACvDa,KAAA,SALKC,GASN,OAFAb,EAAQc,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEf,GAEzD,CACLF,OAAQD,EAAgBmB,IAAMlB,EAASe,EACvCI,OAAQ,CAAA,EACR,6DArBoCC,CACpC,EAqBH,SAAQC,GACP,IAAKA,EAAEC,MACL,MAAMD,EAGR,MAAO,CACLrB,OAAQ,CAAE,EACVmB,OAAQI,EAAAA,aA1EdC,EA4EUH,EA3EVI,GA4EWvB,EAAQc,2BACkB,QAAzBd,EAAQwB,cA3EZF,EAAMF,OAAS,IAAIK,OACzB,SAACC,EAAUJ,GAKT,GAJKI,EAASJ,EAAMK,QAClBD,EAASJ,EAAMK,MAAS,CAAEC,QAASN,EAAMM,QAASC,KAAMP,EAAMO,OAG5DN,EAA0B,CAC5B,IAAMO,EAAQJ,EAASJ,EAAMK,MAAOG,MAC9BC,EAAWD,GAASA,EAAMR,EAAMO,MAEtCH,EAASJ,EAAMK,MAASK,EAAAA,aACtBV,EAAMK,KACNJ,EACAG,EACAJ,EAAMO,KACNE,EACK,GAAgBE,OAAOF,EAAsBT,EAAMM,SACpDN,EAAMM,QAEb,CAED,OAAOF,CACT,EACA,KAsDM1B,IAjFe,IACvBsB,EACAC,CAkFG,GACH,CAAC,MAAAJ,GAAA,OAAAlB,QAAAiC,OAAAf,EACH,CAAA,CAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { render, screen } from '@testing-library/react';
|
|
3
3
|
import user from '@testing-library/user-event';
|
|
4
|
-
import { useForm } from 'react-hook-form';
|
|
4
|
+
import { SubmitHandler, useForm } from 'react-hook-form';
|
|
5
5
|
import * as Yup from 'yup';
|
|
6
6
|
import { yupResolver } from '..';
|
|
7
7
|
|
|
@@ -10,10 +10,10 @@ const schema = Yup.object({
|
|
|
10
10
|
password: Yup.string().required(),
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
type FormData = Yup.InferType<typeof schema
|
|
13
|
+
type FormData = Yup.InferType<typeof schema>;
|
|
14
14
|
|
|
15
15
|
interface Props {
|
|
16
|
-
onSubmit:
|
|
16
|
+
onSubmit: SubmitHandler<FormData>;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
function TestComponent({ onSubmit }: Props) {
|
|
@@ -21,7 +21,7 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
21
21
|
register,
|
|
22
22
|
formState: { errors },
|
|
23
23
|
handleSubmit,
|
|
24
|
-
} = useForm
|
|
24
|
+
} = useForm({
|
|
25
25
|
resolver: yupResolver(schema), // Useful to check TypeScript regressions
|
|
26
26
|
});
|
|
27
27
|
|
|
@@ -35,7 +35,7 @@ export const schemaWithWhen = yup.object({
|
|
|
35
35
|
}),
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
-
export const validData
|
|
38
|
+
export const validData = {
|
|
39
39
|
username: 'Doe',
|
|
40
40
|
password: 'Password123_',
|
|
41
41
|
repeatPassword: 'Password123_',
|
|
@@ -50,14 +50,15 @@ export const validData: yup.InferType<typeof schema> = {
|
|
|
50
50
|
name: 'name',
|
|
51
51
|
},
|
|
52
52
|
],
|
|
53
|
-
}
|
|
53
|
+
} satisfies yup.InferType<typeof schema>;
|
|
54
54
|
|
|
55
55
|
export const invalidData = {
|
|
56
56
|
password: '___',
|
|
57
57
|
email: '',
|
|
58
58
|
birthYear: 'birthYear',
|
|
59
59
|
like: [{ id: 'z' }],
|
|
60
|
-
|
|
60
|
+
// Must be set to "unknown", otherwise typescript knows that it is invalid
|
|
61
|
+
} as unknown as Required<yup.InferType<typeof schema>>;
|
|
61
62
|
|
|
62
63
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
63
64
|
username: {
|
package/yup/src/index.ts
CHANGED
package/yup/src/yup.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import * as Yup from 'yup';
|
|
2
2
|
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
appendErrors,
|
|
5
|
+
FieldError,
|
|
6
|
+
FieldValues,
|
|
7
|
+
Resolver,
|
|
8
|
+
} from 'react-hook-form';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Why `path!` ? because it could be `undefined` in some case
|
|
@@ -38,9 +42,22 @@ const parseErrorSchema = (
|
|
|
38
42
|
);
|
|
39
43
|
};
|
|
40
44
|
|
|
41
|
-
export
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
export function yupResolver<TFieldValues extends FieldValues>(
|
|
46
|
+
schema: Yup.ObjectSchema<TFieldValues>,
|
|
47
|
+
schemaOptions: Parameters<(typeof schema)['validate']>[1] = {},
|
|
48
|
+
resolverOptions: {
|
|
49
|
+
/**
|
|
50
|
+
* @default async
|
|
51
|
+
*/
|
|
52
|
+
mode?: 'async' | 'sync';
|
|
53
|
+
/**
|
|
54
|
+
* Return the raw input values rather than the parsed values.
|
|
55
|
+
* @default false
|
|
56
|
+
*/
|
|
57
|
+
raw?: boolean;
|
|
58
|
+
} = {},
|
|
59
|
+
): Resolver<TFieldValues> {
|
|
60
|
+
return async (values, context, options) => {
|
|
44
61
|
try {
|
|
45
62
|
if (schemaOptions.context && process.env.NODE_ENV === 'development') {
|
|
46
63
|
// eslint-disable-next-line no-console
|
|
@@ -80,3 +97,4 @@ export const yupResolver: Resolver =
|
|
|
80
97
|
};
|
|
81
98
|
}
|
|
82
99
|
};
|
|
100
|
+
}
|
package/yup/dist/types.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
2
|
-
import * as Yup from 'yup';
|
|
3
|
-
type Options<T extends Yup.ObjectSchema<any>> = Parameters<T['validate']>[1];
|
|
4
|
-
export type Resolver = <T extends Yup.ObjectSchema<any>>(schema: T, schemaOptions?: Options<T>, factoryOptions?: {
|
|
5
|
-
/**
|
|
6
|
-
* @default async
|
|
7
|
-
*/
|
|
8
|
-
mode?: 'async' | 'sync';
|
|
9
|
-
/**
|
|
10
|
-
* Return the raw input values rather than the parsed values.
|
|
11
|
-
* @default false
|
|
12
|
-
*/
|
|
13
|
-
raw?: boolean;
|
|
14
|
-
}) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
|
15
|
-
export {};
|
package/yup/src/types.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
2
|
-
import * as Yup from 'yup';
|
|
3
|
-
|
|
4
|
-
type Options<T extends Yup.ObjectSchema<any>> = Parameters<T['validate']>[1];
|
|
5
|
-
|
|
6
|
-
export type Resolver = <T extends Yup.ObjectSchema<any>>(
|
|
7
|
-
schema: T,
|
|
8
|
-
schemaOptions?: Options<T>,
|
|
9
|
-
factoryOptions?: {
|
|
10
|
-
/**
|
|
11
|
-
* @default async
|
|
12
|
-
*/
|
|
13
|
-
mode?: 'async' | 'sync';
|
|
14
|
-
/**
|
|
15
|
-
* Return the raw input values rather than the parsed values.
|
|
16
|
-
* @default false
|
|
17
|
-
*/
|
|
18
|
-
raw?: boolean;
|
|
19
|
-
},
|
|
20
|
-
) => <TFieldValues extends FieldValues, TContext>(
|
|
21
|
-
values: TFieldValues,
|
|
22
|
-
context: TContext | undefined,
|
|
23
|
-
options: ResolverOptions<TFieldValues>,
|
|
24
|
-
) => Promise<ResolverResult<TFieldValues>>;
|