@hookform/resolvers 5.7.0 → 5.8.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/ata-validator/package.json +1 -1
- package/package.json +6 -6
- package/vest/dist/types.d.ts +2 -2
- 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.mjs +1 -1
- package/vest/dist/vest.modern.mjs.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/src/__tests__/Form-native-validation.tsx +1 -1
- package/vest/src/__tests__/Form.tsx +1 -1
- package/vest/src/__tests__/__fixtures__/data.ts +1 -1
- package/vest/src/__tests__/special-path-names.ts +6 -4
- package/vest/src/__tests__/vest.ts +4 -4
- package/vest/src/types.ts +5 -7
- package/vest/src/vest.ts +2 -3
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hookform/resolvers",
|
|
3
3
|
"amdName": "hookformResolvers",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.8.0",
|
|
5
5
|
"description": "React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype, Typanion, Effect-TS and VineJS",
|
|
6
6
|
"main": "dist/resolvers.js",
|
|
7
7
|
"module": "dist/resolvers.module.js",
|
|
@@ -216,7 +216,7 @@
|
|
|
216
216
|
"build:joi": "microbundle --cwd joi --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm",
|
|
217
217
|
"build:superstruct": "microbundle --cwd superstruct --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm",
|
|
218
218
|
"build:io-ts": "microbundle --cwd io-ts --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm,fp-ts/Either=Either,fp-ts/function=_function,fp-ts/Option=Option,fp-ts/ReadonlyArray=ReadonlyArray,fp-ts/Semigroup=Semigroup,fp-ts/ReadonlyRecord=ReadonlyRecord",
|
|
219
|
-
"build:vest": "microbundle --cwd vest --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm
|
|
219
|
+
"build:vest": "microbundle --cwd vest --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm",
|
|
220
220
|
"build:class-validator": "microbundle --cwd class-validator --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm",
|
|
221
221
|
"build:nope": "microbundle --cwd nope --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm",
|
|
222
222
|
"build:computed-types": "microbundle --cwd computed-types --globals @hookform/resolvers=hookformResolvers,react-hook-form=ReactHookForm",
|
|
@@ -291,10 +291,10 @@
|
|
|
291
291
|
"@vinejs/vine": "^4.4.0",
|
|
292
292
|
"@vitejs/plugin-react": "^4.7.0",
|
|
293
293
|
"ajv": "^8.20.0",
|
|
294
|
+
"ata-validator": "^1.2.0",
|
|
294
295
|
"ajv-errors": "^3.0.0",
|
|
295
296
|
"ajv-formats": "^2.1.1",
|
|
296
297
|
"arktype": "2.0.4",
|
|
297
|
-
"ata-validator": "^0.7.3",
|
|
298
298
|
"check-export-map": "^1.3.1",
|
|
299
299
|
"class-transformer": "^0.5.1",
|
|
300
300
|
"class-validator": "^0.14.4",
|
|
@@ -321,7 +321,7 @@
|
|
|
321
321
|
"typanion": "^3.14.0",
|
|
322
322
|
"typescript": "^6.0.3",
|
|
323
323
|
"valibot": "1.0.0-beta.12",
|
|
324
|
-
"vest": "^
|
|
324
|
+
"vest": "^6.3.2",
|
|
325
325
|
"vite": "^6.4.3",
|
|
326
326
|
"vite-tsconfig-paths": "^5.1.4",
|
|
327
327
|
"vitest": "^3.2.7",
|
|
@@ -337,7 +337,7 @@
|
|
|
337
337
|
"ajv-errors": "^3.0.0",
|
|
338
338
|
"ajv-formats": "^2.1.1",
|
|
339
339
|
"arktype": "^2.0.0",
|
|
340
|
-
"ata-validator": "^
|
|
340
|
+
"ata-validator": "^1.2.0",
|
|
341
341
|
"class-transformer": ">=0.4.0",
|
|
342
342
|
"class-validator": ">=0.12.0",
|
|
343
343
|
"computed-types": "^1.0.0",
|
|
@@ -351,7 +351,7 @@
|
|
|
351
351
|
"superstruct": ">=0.12.0",
|
|
352
352
|
"typanion": "^3.3.2",
|
|
353
353
|
"valibot": ">=0.31.0 || ^1.0.0-beta.4 || ^1.0.0-rc",
|
|
354
|
-
"vest": ">=
|
|
354
|
+
"vest": ">=6.0.0",
|
|
355
355
|
"yup": "^1.0.0",
|
|
356
356
|
"zod": "^3.25.0 || ^4.0.0"
|
|
357
357
|
},
|
package/vest/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FieldName, FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
2
|
-
import
|
|
3
|
-
export type ICreateResult<TValues extends FieldValues = FieldValues, TContext = any> =
|
|
2
|
+
import type { Suite } from 'vest';
|
|
3
|
+
export type ICreateResult<TValues extends FieldValues = FieldValues, TContext = any> = Suite<any, any, (values: TValues, names?: FieldName<TValues>[], context?: TContext) => void>;
|
|
4
4
|
export type Resolver = <TValues extends FieldValues, TContext>(schema: ICreateResult<TValues, TContext>, schemaOptions?: never, factoryOptions?: {
|
|
5
5
|
mode?: 'async' | 'sync';
|
|
6
6
|
rawValues?: boolean;
|
package/vest/dist/vest.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=require("@hookform/resolvers"),e=function(r,e){var t=Object.create(null);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(t,o,s){return void 0===s&&(s={}),function(o,n,a){try{var i=function(t){return t.hasErrors()?{values:{},errors:r.toNestErrors(e(t.getErrors(),!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)}:(a.shouldUseNativeValidation&&r.validateFieldsNatively({},a),{values:o,errors:{}})};return Promise.resolve("sync"===s.mode?i(t.run(o,a.names,n)):Promise.resolve(t.run(o,a.names,n)).then(i))}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 { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport
|
|
1
|
+
{"version":3,"file":"vest.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport type { Resolver, VestErrors } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n // See the matching comment in the zod resolver's `parseZod3Issues`.\n const errors: Record<string, FieldError> = Object.create(null);\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\n/**\n * Creates a resolver for react-hook-form using Vest validation\n * @param {Function} schema - The Vest validation schema\n * @param {Object} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = vest.create((data) => {\n * if (!data.name) {\n * return 'Name is required';\n * }\n * });\n *\n * useForm({\n * resolver: vestResolver(schema)\n * });\n */\nexport const vestResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema.run(values, options.names, context)\n : await schema.run(values, options.names, context);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.getErrors(),\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","Object","create","path","message","type","types","reduce","acc","index","schema","_","resolverOptions","values","context","options","_temp2","result","hasErrors","toNestErrors","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","Promise","resolve","mode","run","names","then","e","reject"],"mappings":"qCAIMA,EAAmB,SACvBC,EACAC,GAGA,IAAMC,EAAqCC,OAAOC,OAAO,MACzD,IAAK,IAAMC,KAAQL,EACZE,EAAOG,KACVH,EAAOG,GAAQ,CAAEC,QAASN,EAAUK,GAAM,GAAIE,KAAM,KAGlDN,IACFC,EAAOG,GAAMG,MAAQR,EAAUK,GAAMI,OACnC,SAACC,EAAKJ,EAASK,GAAK,OAAMD,EAAIC,GAASL,IAAYI,CAAG,EACtD,CAAA,IAIN,OAAOR,CACT,uBAqBE,SAACU,EAAQC,EAAGC,GAAoB,YAAL,IAAfA,IAAAA,EAAkB,CAAE,GAAA,SACzBC,EAAQC,EAASC,GAAO,IAAIC,IAAAA,EAAAA,SAC3BC,GAKN,OAAIA,EAAOC,YACF,CACLL,OAAQ,CAAE,EACVb,OAAQmB,eACNtB,EACEoB,EAAOG,aACNL,EAAQM,2BACkB,QAAzBN,EAAQO,cAEZP,KAKNA,EAAQM,2BAA6BE,EAAsBA,uBAAC,CAAE,EAAER,GAEzD,CAAEF,OAAAA,EAAQb,OAAQ,IAAK,EApBG,OAAAwB,QAAAC,QAAN,SAAzBb,EAAgBc,KAAeV,EAC3BN,EAAOiB,IAAId,EAAQE,EAAQa,MAAOd,IAAQU,QAAAC,QACpCf,EAAOiB,IAAId,EAAQE,EAAQa,MAAOd,IAAQe,KAAAb,GAmBxD,CAAC,MAAAc,GAAAN,OAAAA,QAAAO,OAAAD,EAAA,CAAA,CAAA"}
|
package/vest/dist/vest.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";var o=function(r,e){var o=Object.create(null);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,n,s){return void 0===s&&(s={}),function(n,a,i){try{var u=function(t){return t.hasErrors()?{values:{},errors:r(o(t.getErrors(),!i.shouldUseNativeValidation&&"all"===i.criteriaMode),i)}:(i.shouldUseNativeValidation&&e({},i),{values:n,errors:{}})};return Promise.resolve("sync"===s.mode?u(t.run(n,i.names,a)):Promise.resolve(t.run(n,i.names,a)).then(u))}catch(r){return Promise.reject(r)}}};export{t as vestResolver};
|
|
2
2
|
//# sourceMappingURL=vest.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";const s=(r,e)=>{const s=Object.create(null);for(const o in r)s[o]||(s[o]={message:r[o][0],type:""}),e&&(s[o].types=r[o].reduce((r,e,s)=>(r[s]=e)&&r,{}));return s},o=(o,t,a={})=>async(t,n,i)=>{const l="sync"===a.mode?o.run(t,i.names,n):await o.run(t,i.names,n);return l.hasErrors()?{values:{},errors:r(s(l.getErrors(),!i.shouldUseNativeValidation&&"all"===i.criteriaMode),i)}:(i.shouldUseNativeValidation&&e({},i),{values:t,errors:{}})};export{o as vestResolver};
|
|
2
2
|
//# sourceMappingURL=vest.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vest.modern.mjs","sources":["../src/vest.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport
|
|
1
|
+
{"version":3,"file":"vest.modern.mjs","sources":["../src/vest.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport type { Resolver, VestErrors } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n // See the matching comment in the zod resolver's `parseZod3Issues`.\n const errors: Record<string, FieldError> = Object.create(null);\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\n/**\n * Creates a resolver for react-hook-form using Vest validation\n * @param {Function} schema - The Vest validation schema\n * @param {Object} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = vest.create((data) => {\n * if (!data.name) {\n * return 'Name is required';\n * }\n * });\n *\n * useForm({\n * resolver: vestResolver(schema)\n * });\n */\nexport const vestResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema.run(values, options.names, context)\n : await schema.run(values, options.names, context);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.getErrors(),\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","Object","create","path","message","type","types","reduce","acc","index","vestResolver","schema","_","resolverOptions","async","values","context","options","result","mode","run","names","hasErrors","toNestErrors","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively"],"mappings":"+EAIA,MAAMA,EAAmBA,CACvBC,EACAC,KAGA,MAAMC,EAAqCC,OAAOC,OAAO,MACzD,IAAK,MAAMC,KAAQL,EACZE,EAAOG,KACVH,EAAOG,GAAQ,CAAEC,QAASN,EAAUK,GAAM,GAAIE,KAAM,KAGlDN,IACFC,EAAOG,GAAMG,MAAQR,EAAUK,GAAMI,OACnC,CAACC,EAAKJ,EAASK,KAAWD,EAAIC,GAASL,IAAYI,EACnD,CAAE,IAIR,OAAOR,CAAM,EAqBFU,EACXA,CAACC,EAAQC,EAAGC,EAAkB,CAAA,IAC9BC,MAAOC,EAAQC,EAASC,KACtB,MAAMC,EACqB,SAAzBL,EAAgBM,KACZR,EAAOS,IAAIL,EAAQE,EAAQI,MAAOL,SAC5BL,EAAOS,IAAIL,EAAQE,EAAQI,MAAOL,GAE9C,OAAIE,EAAOI,YACF,CACLP,OAAQ,CAAE,EACVf,OAAQuB,EACN1B,EACEqB,EAAOM,aACNP,EAAQQ,2BACkB,QAAzBR,EAAQS,cAEZT,KAKNA,EAAQQ,2BAA6BE,EAAuB,CAAA,EAAIV,GAEzD,CAAEF,SAAQf,OAAQ,IAAI"}
|
package/vest/dist/vest.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";var o=function(r,e){var o=Object.create(null);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,n,s){return void 0===s&&(s={}),function(n,a,i){try{var u=function(t){return t.hasErrors()?{values:{},errors:r(o(t.getErrors(),!i.shouldUseNativeValidation&&"all"===i.criteriaMode),i)}:(i.shouldUseNativeValidation&&e({},i),{values:n,errors:{}})};return Promise.resolve("sync"===s.mode?u(t.run(n,i.names,a)):Promise.resolve(t.run(n,i.names,a)).then(u))}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 { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport
|
|
1
|
+
{"version":3,"file":"vest.module.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport type { Resolver, VestErrors } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n // See the matching comment in the zod resolver's `parseZod3Issues`.\n const errors: Record<string, FieldError> = Object.create(null);\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\n/**\n * Creates a resolver for react-hook-form using Vest validation\n * @param {Function} schema - The Vest validation schema\n * @param {Object} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = vest.create((data) => {\n * if (!data.name) {\n * return 'Name is required';\n * }\n * });\n *\n * useForm({\n * resolver: vestResolver(schema)\n * });\n */\nexport const vestResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema.run(values, options.names, context)\n : await schema.run(values, options.names, context);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.getErrors(),\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","Object","create","path","message","type","types","reduce","acc","index","vestResolver","schema","_","resolverOptions","values","context","options","_temp2","result","hasErrors","toNestErrors","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","Promise","resolve","mode","run","names","then","e","reject"],"mappings":"+EAIA,IAAMA,EAAmB,SACvBC,EACAC,GAGA,IAAMC,EAAqCC,OAAOC,OAAO,MACzD,IAAK,IAAMC,KAAQL,EACZE,EAAOG,KACVH,EAAOG,GAAQ,CAAEC,QAASN,EAAUK,GAAM,GAAIE,KAAM,KAGlDN,IACFC,EAAOG,GAAMG,MAAQR,EAAUK,GAAMI,OACnC,SAACC,EAAKJ,EAASK,GAAK,OAAMD,EAAIC,GAASL,IAAYI,CAAG,EACtD,CAAA,IAIN,OAAOR,CACT,EAoBaU,EACX,SAACC,EAAQC,EAAGC,GAAoB,YAAL,IAAfA,IAAAA,EAAkB,CAAE,GAAA,SACzBC,EAAQC,EAASC,GAAO,IAAIC,IAAAA,EAAAA,SAC3BC,GAKN,OAAIA,EAAOC,YACF,CACLL,OAAQ,CAAE,EACVd,OAAQoB,EACNvB,EACEqB,EAAOG,aACNL,EAAQM,2BACkB,QAAzBN,EAAQO,cAEZP,KAKNA,EAAQM,2BAA6BE,EAAuB,CAAE,EAAER,GAEzD,CAAEF,OAAAA,EAAQd,OAAQ,IAAK,EApBG,OAAAyB,QAAAC,QAAN,SAAzBb,EAAgBc,KAAeV,EAC3BN,EAAOiB,IAAId,EAAQE,EAAQa,MAAOd,IAAQU,QAAAC,QACpCf,EAAOiB,IAAId,EAAQE,EAAQa,MAAOd,IAAQe,KAAAb,GAmBxD,CAAC,MAAAc,GAAAN,OAAAA,QAAAO,OAAAD,EAAA,CAAA,CAAA"}
|
package/vest/dist/vest.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers")
|
|
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).hookformResolversVest={},e.hookformResolvers)}(this,function(e,r){var o=function(e,r){var o=Object.create(null);for(var s in e)o[s]||(o[s]={message:e[s][0],type:""}),r&&(o[s].types=e[s].reduce(function(e,r,o){return(e[o]=r)&&e},{}));return o};e.vestResolver=function(e,s,t){return void 0===t&&(t={}),function(s,n,i){try{var a=function(e){return e.hasErrors()?{values:{},errors:r.toNestErrors(o(e.getErrors(),!i.shouldUseNativeValidation&&"all"===i.criteriaMode),i)}:(i.shouldUseNativeValidation&&r.validateFieldsNatively({},i),{values:s,errors:{}})};return Promise.resolve("sync"===t.mode?a(e.run(s,i.names,n)):Promise.resolve(e.run(s,i.names,n)).then(a))}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 { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport
|
|
1
|
+
{"version":3,"file":"vest.umd.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { FieldError } from 'react-hook-form';\nimport type { Resolver, VestErrors } from './types';\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n // See the matching comment in the zod resolver's `parseZod3Issues`.\n const errors: Record<string, FieldError> = Object.create(null);\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\n/**\n * Creates a resolver for react-hook-form using Vest validation\n * @param {Function} schema - The Vest validation schema\n * @param {Object} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = vest.create((data) => {\n * if (!data.name) {\n * return 'Name is required';\n * }\n * });\n *\n * useForm({\n * resolver: vestResolver(schema)\n * });\n */\nexport const vestResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema.run(values, options.names, context)\n : await schema.run(values, options.names, context);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.getErrors(),\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n }\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return { values, errors: {} };\n };\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","errors","Object","create","path","message","type","types","reduce","acc","index","schema","_","resolverOptions","values","context","options","_temp2","result","hasErrors","toNestErrors","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","Promise","resolve","mode","run","names","then","e","reject"],"mappings":"0TAIA,IAAMA,EAAmB,SACvBC,EACAC,GAGA,IAAMC,EAAqCC,OAAOC,OAAO,MACzD,IAAK,IAAMC,KAAQL,EACZE,EAAOG,KACVH,EAAOG,GAAQ,CAAEC,QAASN,EAAUK,GAAM,GAAIE,KAAM,KAGlDN,IACFC,EAAOG,GAAMG,MAAQR,EAAUK,GAAMI,OACnC,SAACC,EAAKJ,EAASK,GAAK,OAAMD,EAAIC,GAASL,IAAYI,CAAG,EACtD,CAAA,IAIN,OAAOR,CACT,iBAqBE,SAACU,EAAQC,EAAGC,GAAoB,YAAL,IAAfA,IAAAA,EAAkB,CAAE,GAAA,SACzBC,EAAQC,EAASC,GAAO,IAAIC,IAAAA,EAAAA,SAC3BC,GAKN,OAAIA,EAAOC,YACF,CACLL,OAAQ,CAAE,EACVb,OAAQmB,eACNtB,EACEoB,EAAOG,aACNL,EAAQM,2BACkB,QAAzBN,EAAQO,cAEZP,KAKNA,EAAQM,2BAA6BE,EAAsBA,uBAAC,CAAE,EAAER,GAEzD,CAAEF,OAAAA,EAAQb,OAAQ,IAAK,EApBG,OAAAwB,QAAAC,QAAN,SAAzBb,EAAgBc,KAAeV,EAC3BN,EAAOiB,IAAId,EAAQE,EAAQa,MAAOd,IAAQU,QAAAC,QACpCf,EAAOiB,IAAId,EAAQE,EAAQa,MAAOd,IAAQe,KAAAb,GAmBxD,CAAC,MAAAc,GAAAN,OAAAA,QAAAO,OAAAD,EAAA,CAAA,CAAA"}
|
|
@@ -13,7 +13,7 @@ interface FormData {
|
|
|
13
13
|
password: string;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
const validationSuite = vest.create(
|
|
16
|
+
const validationSuite = vest.create((data: FormData) => {
|
|
17
17
|
vest.test('username', USERNAME_REQUIRED_MESSAGE, () => {
|
|
18
18
|
vest.enforce(data.username).isNotEmpty();
|
|
19
19
|
});
|
|
@@ -10,7 +10,7 @@ interface FormData {
|
|
|
10
10
|
password: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
const validationSuite = vest.create(
|
|
13
|
+
const validationSuite = vest.create((data: FormData) => {
|
|
14
14
|
vest.test('username', 'Username is required', () => {
|
|
15
15
|
vest.enforce(data.username).isNotEmpty();
|
|
16
16
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
2
|
import * as vest from 'vest';
|
|
3
3
|
|
|
4
|
-
export const validationSuite = vest.create(
|
|
4
|
+
export const validationSuite = vest.create((data: any = {}) => {
|
|
5
5
|
vest.test('username', 'Username is required', () => {
|
|
6
6
|
vest.enforce(data.username).isNotEmpty();
|
|
7
7
|
});
|
|
@@ -12,10 +12,12 @@ describe('vestResolver special path names', () => {
|
|
|
12
12
|
// unreachable through this resolver's public API regardless of this
|
|
13
13
|
// fix. A fake schema function isolates the resolver's own
|
|
14
14
|
// path-tracking logic instead.
|
|
15
|
-
const schema =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
const schema = {
|
|
16
|
+
run: () => ({
|
|
17
|
+
hasErrors: () => true,
|
|
18
|
+
getErrors: () => ({ [name]: [`${name} is required`] }),
|
|
19
|
+
}),
|
|
20
|
+
};
|
|
19
21
|
|
|
20
22
|
const result = await vestResolver(schema as any, undefined, {
|
|
21
23
|
mode: 'sync',
|
|
@@ -70,9 +70,9 @@ describe('vestResolver', () => {
|
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
it('should call a suite with values, validated field names and a context as arguments', async () => {
|
|
73
|
-
const
|
|
73
|
+
const run = vi.spyOn(validationSuite, 'run');
|
|
74
74
|
|
|
75
|
-
await vestResolver(
|
|
75
|
+
await vestResolver(validationSuite)(
|
|
76
76
|
validData,
|
|
77
77
|
{ some: 'context' },
|
|
78
78
|
{
|
|
@@ -82,8 +82,8 @@ describe('vestResolver', () => {
|
|
|
82
82
|
},
|
|
83
83
|
);
|
|
84
84
|
|
|
85
|
-
expect(
|
|
86
|
-
expect(
|
|
85
|
+
expect(run).toHaveBeenCalledTimes(1);
|
|
86
|
+
expect(run).toHaveBeenCalledWith(validData, ['username'], {
|
|
87
87
|
some: 'context',
|
|
88
88
|
});
|
|
89
89
|
});
|
package/vest/src/types.ts
CHANGED
|
@@ -4,17 +4,15 @@ import {
|
|
|
4
4
|
ResolverOptions,
|
|
5
5
|
ResolverResult,
|
|
6
6
|
} from 'react-hook-form';
|
|
7
|
-
import
|
|
7
|
+
import type { Suite } from 'vest';
|
|
8
8
|
|
|
9
9
|
export type ICreateResult<
|
|
10
10
|
TValues extends FieldValues = FieldValues,
|
|
11
11
|
TContext = any,
|
|
12
|
-
> =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
(values: TValues, names?: FieldName<TValues>[], context?: TContext) => void
|
|
17
|
-
>
|
|
12
|
+
> = Suite<
|
|
13
|
+
any,
|
|
14
|
+
any,
|
|
15
|
+
(values: TValues, names?: FieldName<TValues>[], context?: TContext) => void
|
|
18
16
|
>;
|
|
19
17
|
|
|
20
18
|
export type Resolver = <TValues extends FieldValues, TContext>(
|
package/vest/src/vest.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
2
|
import { FieldError } from 'react-hook-form';
|
|
3
|
-
import promisify from 'vest/promisify';
|
|
4
3
|
import type { Resolver, VestErrors } from './types';
|
|
5
4
|
|
|
6
5
|
const parseErrorSchema = (
|
|
@@ -47,8 +46,8 @@ export const vestResolver: Resolver =
|
|
|
47
46
|
async (values, context, options) => {
|
|
48
47
|
const result =
|
|
49
48
|
resolverOptions.mode === 'sync'
|
|
50
|
-
? schema(values, options.names, context)
|
|
51
|
-
: await
|
|
49
|
+
? schema.run(values, options.names, context)
|
|
50
|
+
: await schema.run(values, options.names, context);
|
|
52
51
|
|
|
53
52
|
if (result.hasErrors()) {
|
|
54
53
|
return {
|