@hookform/resolvers 2.8.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/LICENSE +21 -0
- package/README.md +420 -0
- package/class-validator/dist/class-validator.d.ts +2 -0
- package/class-validator/dist/class-validator.js +2 -0
- package/class-validator/dist/class-validator.js.map +1 -0
- package/class-validator/dist/class-validator.mjs +2 -0
- package/class-validator/dist/class-validator.modern.js +2 -0
- package/class-validator/dist/class-validator.modern.js.map +1 -0
- package/class-validator/dist/class-validator.module.js +2 -0
- package/class-validator/dist/class-validator.module.js.map +1 -0
- package/class-validator/dist/class-validator.umd.js +2 -0
- package/class-validator/dist/class-validator.umd.js.map +1 -0
- package/class-validator/dist/index.d.ts +2 -0
- package/class-validator/dist/types.d.ts +8 -0
- package/class-validator/package.json +19 -0
- package/class-validator/src/__tests__/Form-native-validation.tsx +83 -0
- package/class-validator/src/__tests__/Form.tsx +55 -0
- package/class-validator/src/__tests__/__fixtures__/data.ts +88 -0
- package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +207 -0
- package/class-validator/src/__tests__/class-validator.ts +87 -0
- package/class-validator/src/class-validator.ts +62 -0
- package/class-validator/src/index.ts +2 -0
- package/class-validator/src/types.ts +22 -0
- package/computed-types/dist/computed-types.d.ts +2 -0
- package/computed-types/dist/computed-types.js +2 -0
- package/computed-types/dist/computed-types.js.map +1 -0
- package/computed-types/dist/computed-types.mjs +2 -0
- package/computed-types/dist/computed-types.modern.js +2 -0
- package/computed-types/dist/computed-types.modern.js.map +1 -0
- package/computed-types/dist/computed-types.module.js +2 -0
- package/computed-types/dist/computed-types.module.js.map +1 -0
- package/computed-types/dist/computed-types.umd.js +2 -0
- package/computed-types/dist/computed-types.umd.js.map +1 -0
- package/computed-types/dist/index.d.ts +2 -0
- package/computed-types/dist/types.d.ts +2 -0
- package/computed-types/package.json +17 -0
- package/computed-types/src/__tests__/Form-native-validation.tsx +85 -0
- package/computed-types/src/__tests__/Form.tsx +54 -0
- package/computed-types/src/__tests__/__fixtures__/data.ts +72 -0
- package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +55 -0
- package/computed-types/src/__tests__/computed-types.ts +24 -0
- package/computed-types/src/computed-types.ts +43 -0
- package/computed-types/src/index.ts +2 -0
- package/computed-types/src/types.ts +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/resolvers.js +2 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/resolvers.mjs +2 -0
- package/dist/resolvers.mjs.map +1 -0
- package/dist/resolvers.module.js +2 -0
- package/dist/resolvers.module.js.map +1 -0
- package/dist/resolvers.umd.js +2 -0
- package/dist/resolvers.umd.js.map +1 -0
- package/dist/toNestError.d.ts +2 -0
- package/dist/validateFieldsNatively.d.ts +2 -0
- package/io-ts/dist/arrayToPath.d.ts +3 -0
- package/io-ts/dist/errorsToRecord.d.ts +5 -0
- package/io-ts/dist/index.d.ts +2 -0
- package/io-ts/dist/io-ts.d.ts +2 -0
- package/io-ts/dist/io-ts.js +2 -0
- package/io-ts/dist/io-ts.js.map +1 -0
- package/io-ts/dist/io-ts.mjs +2 -0
- package/io-ts/dist/io-ts.modern.js +2 -0
- package/io-ts/dist/io-ts.modern.js.map +1 -0
- package/io-ts/dist/io-ts.module.js +2 -0
- package/io-ts/dist/io-ts.module.js.map +1 -0
- package/io-ts/dist/io-ts.umd.js +2 -0
- package/io-ts/dist/io-ts.umd.js.map +1 -0
- package/io-ts/dist/types.d.ts +7 -0
- package/io-ts/package.json +19 -0
- package/io-ts/src/__tests__/Form-native-validation.tsx +89 -0
- package/io-ts/src/__tests__/Form.tsx +65 -0
- package/io-ts/src/__tests__/__fixtures__/data.ts +129 -0
- package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +89 -0
- package/io-ts/src/__tests__/io-ts.ts +37 -0
- package/io-ts/src/arrayToPath.ts +18 -0
- package/io-ts/src/errorsToRecord.ts +131 -0
- package/io-ts/src/index.ts +2 -0
- package/io-ts/src/io-ts.ts +33 -0
- package/io-ts/src/types.ts +23 -0
- package/joi/dist/index.d.ts +2 -0
- package/joi/dist/joi.d.ts +2 -0
- package/joi/dist/joi.js +2 -0
- package/joi/dist/joi.js.map +1 -0
- package/joi/dist/joi.mjs +2 -0
- package/joi/dist/joi.modern.js +2 -0
- package/joi/dist/joi.modern.js.map +1 -0
- package/joi/dist/joi.module.js +2 -0
- package/joi/dist/joi.module.js.map +1 -0
- package/joi/dist/joi.umd.js +2 -0
- package/joi/dist/joi.umd.js.map +1 -0
- package/joi/dist/types.d.ts +5 -0
- package/joi/package.json +17 -0
- package/joi/src/__tests__/Form-native-validation.tsx +89 -0
- package/joi/src/__tests__/Form.tsx +61 -0
- package/joi/src/__tests__/__fixtures__/data.ts +85 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +293 -0
- package/joi/src/__tests__/joi.ts +98 -0
- package/joi/src/index.ts +2 -0
- package/joi/src/joi.ts +81 -0
- package/joi/src/types.ts +16 -0
- package/nope/dist/index.d.ts +2 -0
- package/nope/dist/nope.d.ts +2 -0
- package/nope/dist/nope.js +2 -0
- package/nope/dist/nope.js.map +1 -0
- package/nope/dist/nope.mjs +2 -0
- package/nope/dist/nope.modern.js +2 -0
- package/nope/dist/nope.modern.js.map +1 -0
- package/nope/dist/nope.module.js +2 -0
- package/nope/dist/nope.module.js.map +1 -0
- package/nope/dist/nope.umd.js +2 -0
- package/nope/dist/nope.umd.js.map +1 -0
- package/nope/dist/types.d.ts +6 -0
- package/nope/package.json +18 -0
- package/nope/src/__tests__/Form-native-validation.tsx +89 -0
- package/nope/src/__tests__/Form.tsx +57 -0
- package/nope/src/__tests__/__fixtures__/data.ts +70 -0
- package/nope/src/__tests__/__snapshots__/nope.ts.snap +43 -0
- package/nope/src/__tests__/nope.ts +28 -0
- package/nope/src/index.ts +2 -0
- package/nope/src/nope.ts +46 -0
- package/nope/src/types.ts +22 -0
- package/package.json +216 -0
- package/superstruct/dist/index.d.ts +2 -0
- package/superstruct/dist/superstruct.d.ts +2 -0
- package/superstruct/dist/superstruct.js +2 -0
- package/superstruct/dist/superstruct.js.map +1 -0
- package/superstruct/dist/superstruct.mjs +2 -0
- package/superstruct/dist/superstruct.modern.js +2 -0
- package/superstruct/dist/superstruct.modern.js.map +1 -0
- package/superstruct/dist/superstruct.module.js +2 -0
- package/superstruct/dist/superstruct.module.js.map +1 -0
- package/superstruct/dist/superstruct.umd.js +2 -0
- package/superstruct/dist/superstruct.umd.js.map +1 -0
- package/superstruct/dist/types.d.ts +5 -0
- package/superstruct/package.json +18 -0
- package/superstruct/src/__tests__/Form-native-validation.tsx +86 -0
- package/superstruct/src/__tests__/Form.tsx +62 -0
- package/superstruct/src/__tests__/__fixtures__/data.ts +75 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +64 -0
- package/superstruct/src/__tests__/superstruct.ts +24 -0
- package/superstruct/src/index.ts +2 -0
- package/superstruct/src/superstruct.ts +37 -0
- package/superstruct/src/types.ts +17 -0
- package/typanion/dist/index.d.ts +2 -0
- package/typanion/dist/typanion.d.ts +2 -0
- package/typanion/dist/typanion.js +2 -0
- package/typanion/dist/typanion.js.map +1 -0
- package/typanion/dist/typanion.mjs +2 -0
- package/typanion/dist/typanion.modern.js +2 -0
- package/typanion/dist/typanion.modern.js.map +1 -0
- package/typanion/dist/typanion.module.js +2 -0
- package/typanion/dist/typanion.module.js.map +1 -0
- package/typanion/dist/typanion.umd.js +2 -0
- package/typanion/dist/typanion.umd.js.map +1 -0
- package/typanion/dist/types.d.ts +6 -0
- package/typanion/package.json +18 -0
- package/typanion/src/__tests__/Form-native-validation.tsx +89 -0
- package/typanion/src/__tests__/Form.tsx +61 -0
- package/typanion/src/__tests__/__fixtures__/data.ts +77 -0
- package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +67 -0
- package/typanion/src/__tests__/typanion.ts +31 -0
- package/typanion/src/index.ts +2 -0
- package/typanion/src/typanion.ts +44 -0
- package/typanion/src/types.ts +28 -0
- package/vest/dist/index.d.ts +1 -0
- package/vest/dist/vest.d.ts +2 -0
- package/vest/dist/vest.js +2 -0
- package/vest/dist/vest.js.map +1 -0
- package/vest/dist/vest.mjs +2 -0
- package/vest/dist/vest.modern.js +2 -0
- package/vest/dist/vest.modern.js.map +1 -0
- package/vest/dist/vest.module.js +2 -0
- package/vest/dist/vest.module.js.map +1 -0
- package/vest/dist/vest.umd.js +2 -0
- package/vest/dist/vest.umd.js.map +1 -0
- package/vest/package.json +18 -0
- package/vest/src/__tests__/Form-native-validation.tsx +93 -0
- package/vest/src/__tests__/Form.tsx +64 -0
- package/vest/src/__tests__/__fixtures__/data.ts +67 -0
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +141 -0
- package/vest/src/__tests__/vest.ts +71 -0
- package/vest/src/index.ts +1 -0
- package/vest/src/types.ts +20 -0
- package/vest/src/vest.ts +51 -0
- package/yup/dist/index.d.ts +2 -0
- package/yup/dist/types.d.ts +8 -0
- package/yup/dist/yup.d.ts +2 -0
- package/yup/dist/yup.js +2 -0
- package/yup/dist/yup.js.map +1 -0
- package/yup/dist/yup.mjs +2 -0
- package/yup/dist/yup.modern.js +2 -0
- package/yup/dist/yup.modern.js.map +1 -0
- package/yup/dist/yup.module.js +2 -0
- package/yup/dist/yup.module.js.map +1 -0
- package/yup/dist/yup.umd.js +2 -0
- package/yup/dist/yup.umd.js.map +1 -0
- package/yup/package.json +17 -0
- package/yup/src/__tests__/Form-native-validation.tsx +85 -0
- package/yup/src/__tests__/Form.tsx +54 -0
- package/yup/src/__tests__/__fixtures__/data.ts +72 -0
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +231 -0
- package/yup/src/__tests__/yup.ts +174 -0
- package/yup/src/index.ts +2 -0
- package/yup/src/types.ts +21 -0
- package/yup/src/yup.ts +75 -0
- package/zod/dist/index.d.ts +2 -0
- package/zod/dist/types.d.ts +5 -0
- package/zod/dist/zod.d.ts +2 -0
- package/zod/dist/zod.js +2 -0
- package/zod/dist/zod.js.map +1 -0
- package/zod/dist/zod.mjs +2 -0
- package/zod/dist/zod.modern.js +2 -0
- package/zod/dist/zod.modern.js.map +1 -0
- package/zod/dist/zod.module.js +2 -0
- package/zod/dist/zod.module.js.map +1 -0
- package/zod/dist/zod.umd.js +2 -0
- package/zod/dist/zod.umd.js.map +1 -0
- package/zod/package.json +17 -0
- package/zod/src/__tests__/Form-native-validation.tsx +85 -0
- package/zod/src/__tests__/Form.tsx +54 -0
- package/zod/src/__tests__/__fixtures__/data.ts +81 -0
- package/zod/src/__tests__/__snapshots__/zod.ts.snap +377 -0
- package/zod/src/__tests__/zod.ts +77 -0
- package/zod/src/index.ts +2 -0
- package/zod/src/types.ts +16 -0
- package/zod/src/zod.ts +85 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
3
|
+
import { validate, Struct } from 'superstruct';
|
|
4
|
+
|
|
5
|
+
type Options = Parameters<typeof validate>[2];
|
|
6
|
+
|
|
7
|
+
export type Resolver = <
|
|
8
|
+
TFieldValues extends FieldValues,
|
|
9
|
+
TContext extends object = object,
|
|
10
|
+
>(
|
|
11
|
+
schema: Struct<TFieldValues, any>,
|
|
12
|
+
factoryOptions?: Options,
|
|
13
|
+
) => (
|
|
14
|
+
values: unknown,
|
|
15
|
+
context: TContext | undefined,
|
|
16
|
+
options: ResolverOptions<TFieldValues>,
|
|
17
|
+
) => ResolverResult<TFieldValues>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("@hookform/resolvers");exports.typanionResolver=function(e,o){return void 0===o&&(o={}),function(s,t,i){var n=[],a=e(s,Object.assign({},{errors:n},o)),u=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.split(":");return r[o[0].slice(1)]={message:o[1].trim()},r},e)}(n);return a?(i.shouldUseNativeValidation&&r.validateFieldsNatively(u,i),{values:s,errors:{}}):{values:{},errors:r.toNestError(u,i)}}};
|
|
2
|
+
//# sourceMappingURL=typanion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typanion.js","sources":["../src/typanion.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const e = error.split(':');\n\n acc[e[0].slice(1)] = {\n message: e[1].trim(),\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} as FieldErrors<any> };\n }\n\n return {\n values: {},\n errors: toNestError(parsedErrors, options) as FieldErrors<any>,\n };\n };\n"],"names":["validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","e","split","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestError"],"mappings":"8DAiBE,SAACA,EAAWC,mBAAAA,IAAAA,EAAmB,aAC9BC,EAAQC,EAAGC,GACV,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA1BU,SAACD,EAAkBC,GACrC,gBADqCA,IAAAA,EAA4B,IAC1DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAID,EAAME,MAAM,KAMtB,OAJAH,EAAIE,EAAE,GAAGE,MAAM,IAAM,CACnBC,QAASH,EAAE,GAAGI,QAGTN,GACNF,GAiBoBS,CAAYd,GAEjC,OAAIC,GACFF,EAAQgB,2BACNC,yBAAuBX,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,KAGpB,CACLP,OAAQ,GACRO,OAAQa,cAAYZ,EAAcN"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";var o=function(o,s){return void 0===s&&(s={}),function(t,i,n){var u=[],a=o(t,Object.assign({},{errors:u},s)),v=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.split(":");return r[o[0].slice(1)]={message:o[1].trim()},r},e)}(u);return a?(n.shouldUseNativeValidation&&r(v,n),{values:t,errors:{}}):{values:{},errors:e(v,n)}}};export{o as typanionResolver};
|
|
2
|
+
//# sourceMappingURL=typanion.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";const s=(s,o={})=>(t,i,a)=>{const l=[],n=s(t,Object.assign({},{errors:l},o)),c=((r,e={})=>r.reduce((r,e)=>{const s=e.split(":");return r[s[0].slice(1)]={message:s[1].trim()},r},e))(l);return n?(a.shouldUseNativeValidation&&r(c,a),{values:t,errors:{}}):{values:{},errors:e(c,a)}};export{s as typanionResolver};
|
|
2
|
+
//# sourceMappingURL=typanion.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typanion.modern.js","sources":["../src/typanion.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const e = error.split(':');\n\n acc[e[0].slice(1)] = {\n message: e[1].trim(),\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} as FieldErrors<any> };\n }\n\n return {\n values: {},\n errors: toNestError(parsedErrors, options) as FieldErrors<any>,\n };\n };\n"],"names":["typanionResolver","validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","e","split","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestError"],"mappings":"8EAIA,MAYaA,EACX,CAACC,EAAWC,EAAmB,KAC/B,CAACC,EAAQC,EAAGC,KACV,MAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA1BU,EAACD,EAAkBC,EAA4B,KAC1DD,EAAOE,OAAO,CAACC,EAAKC,KACzB,MAAMC,EAAID,EAAME,MAAM,KAMtB,OAJAH,EAAIE,EAAE,GAAGE,MAAM,IAAM,CACnBC,QAASH,EAAE,GAAGI,QAGTN,GACNF,GAiBoBS,CAAYd,GAEjC,OAAIC,GACFF,EAAQgB,2BACNC,EAAuBX,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,KAGpB,CACLP,OAAQ,GACRO,OAAQa,EAAYZ,EAAcN"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{validateFieldsNatively as r,toNestError as e}from"@hookform/resolvers";var o=function(o,s){return void 0===s&&(s={}),function(t,i,n){var u=[],a=o(t,Object.assign({},{errors:u},s)),v=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.split(":");return r[o[0].slice(1)]={message:o[1].trim()},r},e)}(u);return a?(n.shouldUseNativeValidation&&r(v,n),{values:t,errors:{}}):{values:{},errors:e(v,n)}}};export{o as typanionResolver};
|
|
2
|
+
//# sourceMappingURL=typanion.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typanion.module.js","sources":["../src/typanion.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const e = error.split(':');\n\n acc[e[0].slice(1)] = {\n message: e[1].trim(),\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} as FieldErrors<any> };\n }\n\n return {\n values: {},\n errors: toNestError(parsedErrors, options) as FieldErrors<any>,\n };\n };\n"],"names":["typanionResolver","validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","e","split","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestError"],"mappings":"8EAIA,IAYaA,EACX,SAACC,EAAWC,mBAAAA,IAAAA,EAAmB,aAC9BC,EAAQC,EAAGC,GACV,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA1BU,SAACD,EAAkBC,GACrC,gBADqCA,IAAAA,EAA4B,IAC1DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAID,EAAME,MAAM,KAMtB,OAJAH,EAAIE,EAAE,GAAGE,MAAM,IAAM,CACnBC,QAASH,EAAE,GAAGI,QAGTN,GACNF,GAiBoBS,CAAYd,GAEjC,OAAIC,GACFF,EAAQgB,2BACNC,EAAuBX,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,KAGpB,CACLP,OAAQ,GACRO,OAAQa,EAAYZ,EAAcN"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],o):o((e||self).hookformResolversTypanion={},e.hookformResolvers)}(this,function(e,o){e.typanionResolver=function(e,r){return void 0===r&&(r={}),function(n,s,i){var t=[],f=e(n,Object.assign({},{errors:t},r)),l=function(e,o){return void 0===o&&(o={}),e.reduce(function(e,o){var r=o.split(":");return e[r[0].slice(1)]={message:r[1].trim()},e},o)}(t);return f?(i.shouldUseNativeValidation&&o.validateFieldsNatively(l,i),{values:n,errors:{}}):{values:{},errors:o.toNestError(l,i)}}}});
|
|
2
|
+
//# sourceMappingURL=typanion.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typanion.umd.js","sources":["../src/typanion.ts"],"sourcesContent":["import type { FieldErrors } from 'react-hook-form';\nimport { toNestError, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const e = error.split(':');\n\n acc[e[0].slice(1)] = {\n message: e[1].trim(),\n };\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} as FieldErrors<any> };\n }\n\n return {\n values: {},\n errors: toNestError(parsedErrors, options) as FieldErrors<any>,\n };\n };\n"],"names":["validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","e","split","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestError"],"mappings":"iVAiBE,SAACA,EAAWC,mBAAAA,IAAAA,EAAmB,aAC9BC,EAAQC,EAAGC,GACV,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA1BU,SAACD,EAAkBC,GACrC,gBADqCA,IAAAA,EAA4B,IAC1DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAID,EAAME,MAAM,KAMtB,OAJAH,EAAIE,EAAE,GAAGE,MAAM,IAAM,CACnBC,QAASH,EAAE,GAAGI,QAGTN,GACNF,GAiBoBS,CAAYd,GAEjC,OAAIC,GACFF,EAAQgB,2BACNC,yBAAuBX,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,KAGpB,CACLP,OAAQ,GACRO,OAAQa,cAAYZ,EAAcN"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
2
|
+
import { ValidationState, StrictValidator } from 'typanion';
|
|
3
|
+
declare type ValidateOptions = Pick<ValidationState, 'coercions' | 'coercion'>;
|
|
4
|
+
declare type RHFResolver<TFieldValues extends FieldValues, TInput = unknown, TContext extends Object = object> = (values: TInput, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => ResolverResult<TFieldValues>;
|
|
5
|
+
export declare type Resolver = <TInput extends FieldValues = FieldValues, TFieldValues extends TInput = TInput, TContext extends object = object>(validator: StrictValidator<TInput, TFieldValues>, validatorOptions?: ValidateOptions) => RHFResolver<TFieldValues, TInput, TContext>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typanion",
|
|
3
|
+
"amdName": "hookformResolversTypanion",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": true,
|
|
6
|
+
"description": "React Hook Form validation resolver: typanion",
|
|
7
|
+
"main": "dist/typanion.js",
|
|
8
|
+
"module": "dist/typanion.module.js",
|
|
9
|
+
"umd:main": "dist/typanion.umd.js",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react-hook-form": "^7.0.0",
|
|
15
|
+
"@hookform/resolvers": "^2.0.0",
|
|
16
|
+
"typanion": "^3.3.2"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen, act } from '@testing-library/react';
|
|
3
|
+
import user from '@testing-library/user-event';
|
|
4
|
+
import { useForm } from 'react-hook-form';
|
|
5
|
+
import * as t from 'typanion';
|
|
6
|
+
import { typanionResolver } from '..';
|
|
7
|
+
|
|
8
|
+
const ERROR_MESSAGE =
|
|
9
|
+
'Expected to have a length of at least 1 elements (got 0)';
|
|
10
|
+
|
|
11
|
+
const schema = t.isObject({
|
|
12
|
+
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
|
|
13
|
+
password: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
interface FormData {
|
|
17
|
+
unusedProperty: string;
|
|
18
|
+
username: string;
|
|
19
|
+
password: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Props {
|
|
23
|
+
onSubmit: (data: FormData) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function TestComponent({ onSubmit }: Props) {
|
|
27
|
+
const { register, handleSubmit } = useForm({
|
|
28
|
+
resolver: typanionResolver(schema),
|
|
29
|
+
shouldUseNativeValidation: true,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
34
|
+
<input {...register('username')} placeholder="username" />
|
|
35
|
+
|
|
36
|
+
<input {...register('password')} placeholder="password" />
|
|
37
|
+
|
|
38
|
+
<button type="submit">submit</button>
|
|
39
|
+
</form>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
test("form's native validation with Typanion", async () => {
|
|
44
|
+
const handleSubmit = jest.fn();
|
|
45
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
46
|
+
|
|
47
|
+
// username
|
|
48
|
+
let usernameField = screen.getByPlaceholderText(
|
|
49
|
+
/username/i,
|
|
50
|
+
) as HTMLInputElement;
|
|
51
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
52
|
+
expect(usernameField.validationMessage).toBe('');
|
|
53
|
+
|
|
54
|
+
// password
|
|
55
|
+
let passwordField = screen.getByPlaceholderText(
|
|
56
|
+
/password/i,
|
|
57
|
+
) as HTMLInputElement;
|
|
58
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
59
|
+
expect(passwordField.validationMessage).toBe('');
|
|
60
|
+
|
|
61
|
+
await act(async () => {
|
|
62
|
+
user.click(screen.getByText(/submit/i));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// username
|
|
66
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
67
|
+
expect(usernameField.validity.valid).toBe(false);
|
|
68
|
+
expect(usernameField.validationMessage).toBe(ERROR_MESSAGE);
|
|
69
|
+
|
|
70
|
+
// password
|
|
71
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
72
|
+
expect(passwordField.validity.valid).toBe(false);
|
|
73
|
+
expect(passwordField.validationMessage).toBe(ERROR_MESSAGE);
|
|
74
|
+
|
|
75
|
+
await act(async () => {
|
|
76
|
+
user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
77
|
+
user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// username
|
|
81
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
82
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
83
|
+
expect(usernameField.validationMessage).toBe('');
|
|
84
|
+
|
|
85
|
+
// password
|
|
86
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
87
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
88
|
+
expect(passwordField.validationMessage).toBe('');
|
|
89
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen, act } from '@testing-library/react';
|
|
3
|
+
import user from '@testing-library/user-event';
|
|
4
|
+
import { useForm } from 'react-hook-form';
|
|
5
|
+
import * as t from 'typanion';
|
|
6
|
+
import { typanionResolver } from '..';
|
|
7
|
+
|
|
8
|
+
const schema = t.isObject({
|
|
9
|
+
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
|
|
10
|
+
password: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
interface FormData {
|
|
14
|
+
unusedProperty: string;
|
|
15
|
+
username: string;
|
|
16
|
+
password: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Props {
|
|
20
|
+
onSubmit: (data: FormData) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function TestComponent({ onSubmit }: Props) {
|
|
24
|
+
const {
|
|
25
|
+
register,
|
|
26
|
+
formState: { errors },
|
|
27
|
+
handleSubmit,
|
|
28
|
+
} = useForm({
|
|
29
|
+
resolver: typanionResolver(schema), // Useful to check TypeScript regressions
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
34
|
+
<input {...register('username')} />
|
|
35
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
36
|
+
|
|
37
|
+
<input {...register('password')} />
|
|
38
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
39
|
+
|
|
40
|
+
<button type="submit">submit</button>
|
|
41
|
+
</form>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
test("form's validation with Typanion and TypeScript's integration", async () => {
|
|
46
|
+
const handleSubmit = jest.fn();
|
|
47
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
48
|
+
|
|
49
|
+
expect(screen.queryAllByRole(/alert/i)).toHaveLength(0);
|
|
50
|
+
|
|
51
|
+
await act(async () => {
|
|
52
|
+
user.click(screen.getByText(/submit/i));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
expect(
|
|
56
|
+
screen.getAllByText(
|
|
57
|
+
'Expected to have a length of at least 1 elements (got 0)',
|
|
58
|
+
),
|
|
59
|
+
).toHaveLength(2);
|
|
60
|
+
expect(handleSubmit).not.toHaveBeenCalled();
|
|
61
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import * as t from 'typanion';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export const isSchema = t.isObject({
|
|
7
|
+
username: t.applyCascade(t.isString(), [
|
|
8
|
+
t.matchesRegExp(/^\w+$/),
|
|
9
|
+
t.hasMinLength(2),
|
|
10
|
+
t.hasMaxLength(30),
|
|
11
|
+
]),
|
|
12
|
+
password: t.applyCascade(t.isString(), [
|
|
13
|
+
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character
|
|
14
|
+
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character
|
|
15
|
+
t.matchesRegExp(new RegExp('.*\\d.*')), // one number
|
|
16
|
+
t.matchesRegExp(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*')), // one special character
|
|
17
|
+
t.hasMinLength(8), // Must be at least 8 characters in length
|
|
18
|
+
]),
|
|
19
|
+
repeatPassword: t.applyCascade(t.isString(), [
|
|
20
|
+
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character
|
|
21
|
+
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character
|
|
22
|
+
t.matchesRegExp(new RegExp('.*\\d.*')), // one number
|
|
23
|
+
t.matchesRegExp(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*')), // one special character
|
|
24
|
+
t.hasMinLength(8), // Must be at least 8 characters in length
|
|
25
|
+
]),
|
|
26
|
+
accessToken: t.isString(),
|
|
27
|
+
birthYear: t.applyCascade(t.isNumber(), [t.isInteger(), t.isInInclusiveRange(1900, 2013)]),
|
|
28
|
+
email: t.applyCascade(t.isString(), [t.matchesRegExp(/^\S+@\S+$/)]),
|
|
29
|
+
tags: t.isArray(t.isString()),
|
|
30
|
+
enabled: t.isBoolean(),
|
|
31
|
+
like: t.isObject({
|
|
32
|
+
id: t.applyCascade(t.isNumber(), [t.isInteger(), t.isPositive()]),
|
|
33
|
+
name: t.applyCascade(t.isString(), [t.hasMinLength(4)])
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export const validData = {
|
|
38
|
+
username: 'Doe',
|
|
39
|
+
password: 'Password123_',
|
|
40
|
+
repeatPassword: 'Password123_',
|
|
41
|
+
birthYear: 2000,
|
|
42
|
+
email: 'john@doe.com',
|
|
43
|
+
tags: ['tag1', 'tag2'],
|
|
44
|
+
enabled: true,
|
|
45
|
+
accessToken: 'accessToken',
|
|
46
|
+
like: {
|
|
47
|
+
id: 1,
|
|
48
|
+
name: 'name',
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const invalidData = {
|
|
53
|
+
password: '___',
|
|
54
|
+
email: '',
|
|
55
|
+
birthYear: 'birthYear',
|
|
56
|
+
like: { id: 'z' },
|
|
57
|
+
tags: [1, 2, 3],
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
61
|
+
username: {
|
|
62
|
+
ref: { name: 'username' },
|
|
63
|
+
name: 'username',
|
|
64
|
+
},
|
|
65
|
+
password: {
|
|
66
|
+
ref: { name: 'password' },
|
|
67
|
+
name: 'password',
|
|
68
|
+
},
|
|
69
|
+
email: {
|
|
70
|
+
ref: { name: 'email' },
|
|
71
|
+
name: 'email',
|
|
72
|
+
},
|
|
73
|
+
birthday: {
|
|
74
|
+
ref: { name: 'birthday' },
|
|
75
|
+
name: 'birthday',
|
|
76
|
+
},
|
|
77
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`typanionResolver should return a single error from typanionResolver when validation fails 1`] = `
|
|
4
|
+
Object {
|
|
5
|
+
"errors": Object {
|
|
6
|
+
"accessToken": Object {
|
|
7
|
+
"message": "Expected a string (got undefined)",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
},
|
|
10
|
+
"birthYear": Object {
|
|
11
|
+
"message": "Expected a number (got \\"birthYear\\")",
|
|
12
|
+
"ref": undefined,
|
|
13
|
+
},
|
|
14
|
+
"email": Object {
|
|
15
|
+
"message": "Expected to match the pattern /^\\\\S+@\\\\S+$/ (got an empty string)",
|
|
16
|
+
"ref": Object {
|
|
17
|
+
"name": "email",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
"enabled": Object {
|
|
21
|
+
"message": "Expected a boolean (got undefined)",
|
|
22
|
+
"ref": undefined,
|
|
23
|
+
},
|
|
24
|
+
"like": Object {
|
|
25
|
+
"id": Object {
|
|
26
|
+
"message": "Expected a number (got \\"z\\")",
|
|
27
|
+
"ref": undefined,
|
|
28
|
+
},
|
|
29
|
+
"name": Object {
|
|
30
|
+
"message": "Expected a string (got undefined)",
|
|
31
|
+
"ref": undefined,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
"password": Object {
|
|
35
|
+
"message": "Expected to match the pattern /.*[A-Z].*/ (got \\"___\\")",
|
|
36
|
+
"ref": Object {
|
|
37
|
+
"name": "password",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
"repeatPassword": Object {
|
|
41
|
+
"message": "Expected a string (got undefined)",
|
|
42
|
+
"ref": undefined,
|
|
43
|
+
},
|
|
44
|
+
"tags": Array [
|
|
45
|
+
Object {
|
|
46
|
+
"message": "Expected a string (got 1)",
|
|
47
|
+
"ref": undefined,
|
|
48
|
+
},
|
|
49
|
+
Object {
|
|
50
|
+
"message": "Expected a string (got 2)",
|
|
51
|
+
"ref": undefined,
|
|
52
|
+
},
|
|
53
|
+
Object {
|
|
54
|
+
"message": "Expected a string (got 3)",
|
|
55
|
+
"ref": undefined,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
"username": Object {
|
|
59
|
+
"message": "Expected a string (got undefined)",
|
|
60
|
+
"ref": Object {
|
|
61
|
+
"name": "username",
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
"values": Object {},
|
|
66
|
+
}
|
|
67
|
+
`;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { typanionResolver } from '..';
|
|
2
|
+
import { isSchema, validData, fields, invalidData } from './__fixtures__/data';
|
|
3
|
+
|
|
4
|
+
const tmpObj = {
|
|
5
|
+
validate: isSchema
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const shouldUseNativeValidation = false;
|
|
9
|
+
|
|
10
|
+
describe('typanionResolver', () => {
|
|
11
|
+
it('should return values from typanionResolver when validation pass', async () => {
|
|
12
|
+
const schemaSpy = jest.spyOn(tmpObj, 'validate')
|
|
13
|
+
|
|
14
|
+
const result = await typanionResolver(schemaSpy as any)(validData, undefined, {
|
|
15
|
+
fields,
|
|
16
|
+
shouldUseNativeValidation,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
|
20
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should return a single error from typanionResolver when validation fails', async () => {
|
|
24
|
+
const result = await typanionResolver(isSchema)(invalidData, undefined, {
|
|
25
|
+
fields,
|
|
26
|
+
shouldUseNativeValidation,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(result).toMatchSnapshot();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { FieldErrors } from 'react-hook-form';
|
|
2
|
+
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
|
|
3
|
+
import type { Resolver } from './types';
|
|
4
|
+
|
|
5
|
+
const parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {
|
|
6
|
+
return errors.reduce((acc, error) => {
|
|
7
|
+
const e = error.split(':');
|
|
8
|
+
|
|
9
|
+
acc[e[0].slice(1)] = {
|
|
10
|
+
message: e[1].trim(),
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
return acc;
|
|
14
|
+
}, parsedErrors);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const typanionResolver: Resolver =
|
|
18
|
+
(validator, validatorOptions = {}) =>
|
|
19
|
+
(values, _, options) => {
|
|
20
|
+
const rawErrors: string[] = [];
|
|
21
|
+
const isValid = validator(
|
|
22
|
+
values,
|
|
23
|
+
Object.assign(
|
|
24
|
+
{},
|
|
25
|
+
{
|
|
26
|
+
errors: rawErrors,
|
|
27
|
+
},
|
|
28
|
+
validatorOptions,
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
const parsedErrors = parseErrors(rawErrors);
|
|
32
|
+
|
|
33
|
+
if (isValid) {
|
|
34
|
+
options.shouldUseNativeValidation &&
|
|
35
|
+
validateFieldsNatively(parsedErrors, options);
|
|
36
|
+
|
|
37
|
+
return { values, errors: {} as FieldErrors<any> };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
values: {},
|
|
42
|
+
errors: toNestError(parsedErrors, options) as FieldErrors<any>,
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
import type {
|
|
3
|
+
FieldValues,
|
|
4
|
+
ResolverOptions,
|
|
5
|
+
ResolverResult,
|
|
6
|
+
} from 'react-hook-form';
|
|
7
|
+
import { ValidationState, StrictValidator } from 'typanion';
|
|
8
|
+
|
|
9
|
+
type ValidateOptions = Pick<ValidationState, 'coercions' | 'coercion'>;
|
|
10
|
+
|
|
11
|
+
type RHFResolver<
|
|
12
|
+
TFieldValues extends FieldValues,
|
|
13
|
+
TInput = unknown,
|
|
14
|
+
TContext extends Object = object,
|
|
15
|
+
> = (
|
|
16
|
+
values: TInput,
|
|
17
|
+
context: TContext | undefined,
|
|
18
|
+
options: ResolverOptions<TFieldValues>,
|
|
19
|
+
) => ResolverResult<TFieldValues>;
|
|
20
|
+
|
|
21
|
+
export type Resolver = <
|
|
22
|
+
TInput extends FieldValues = FieldValues,
|
|
23
|
+
TFieldValues extends TInput = TInput,
|
|
24
|
+
TContext extends object = object,
|
|
25
|
+
>(
|
|
26
|
+
validator: StrictValidator<TInput, TFieldValues>,
|
|
27
|
+
validatorOptions?: ValidateOptions,
|
|
28
|
+
) => RHFResolver<TFieldValues, TInput, TContext>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vest';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=require("@hookform/resolvers");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=r(require("vest/promisify")),o=function(e,r){var t={};for(var o in e)t[o]||(t[o]={message:e[o][0],type:""}),r&&(t[o].types=e[o].reduce(function(e,r,t){return(e[t]=r)&&e},{}));return t};exports.vestResolver=function(r,s,i){return void 0===i&&(i={}),function(s,n,a){try{var u=function(r){return r.hasErrors()?{values:{},errors:e.toNestError(o(r.getErrors(),!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)}:(a.shouldUseNativeValidation&&e.validateFieldsNatively({},a),{values:s,errors:{}})};return Promise.resolve("sync"===i.mode?u(r(s)):Promise.resolve(t.default(r)(s)).then(u))}catch(e){return Promise.reject(e)}}};
|
|
2
|
+
//# sourceMappingURL=vest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vest.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError, validateFieldsNatively } 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, _, resolverOptions = {}) =>\n async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestError(\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","path","message","type","types","reduce","acc","index","schema","_","resolverOptions","values","_context","options","result","hasErrors","toNestError","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","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,wBAIP,SAACQ,EAAQC,EAAGC,mBAAAA,IAAAA,EAAkB,aACvBC,EAAQC,EAAUC,sBACjBC,GAKN,OAAIA,EAAOC,YACF,CACLJ,OAAQ,GACRX,OAAQgB,cACNnB,EACEiB,EAAOG,aACNJ,EAAQK,2BACkB,QAAzBL,EAAQM,cAEZN,KAKNA,EAAQK,2BAA6BE,yBAAuB,GAAIP,GAEzD,CAAEF,OAAAA,EAAQX,OAAQ,6BApBE,SAAzBU,EAAgBW,OACZb,EAAOG,oBACDW,UAAUd,EAAVc,CAAkBX,aALhC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{toNestError as r,validateFieldsNatively as e}from"@hookform/resolvers";import o from"vest/promisify";var t=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},s=function(s,i,n){return void 0===n&&(n={}),function(i,a,u){try{var v=function(o){return o.hasErrors()?{values:{},errors:r(t(o.getErrors(),!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)}:(u.shouldUseNativeValidation&&e({},u),{values:i,errors:{}})};return Promise.resolve("sync"===n.mode?v(s(i)):Promise.resolve(o(s)(i)).then(v))}catch(r){return Promise.reject(r)}}};export{s as vestResolver};
|
|
2
|
+
//# sourceMappingURL=vest.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{toNestError as r,validateFieldsNatively as o}from"@hookform/resolvers";import e from"vest/promisify";const s=(r,o)=>{const e={};for(const s in r)e[s]||(e[s]={message:r[s][0],type:""}),o&&(e[s].types=r[s].reduce((r,o,e)=>(r[e]=o)&&r,{}));return e},t=(t,a,i={})=>async(a,n,l)=>{const c="sync"===i.mode?t(a):await e(t)(a);return c.hasErrors()?{values:{},errors:r(s(c.getErrors(),!l.shouldUseNativeValidation&&"all"===l.criteriaMode),l)}:(l.shouldUseNativeValidation&&o({},l),{values:a,errors:{}})};export{t as vestResolver};
|
|
2
|
+
//# sourceMappingURL=vest.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vest.modern.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError, validateFieldsNatively } 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, _, resolverOptions = {}) =>\n async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestError(\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","path","message","type","types","reduce","acc","index","vestResolver","schema","_","resolverOptions","async","values","_context","options","result","mode","promisify","hasErrors","toNestError","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively"],"mappings":"4GAKA,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,EACX,CAACC,EAAQC,EAAGC,EAAkB,KAC9BC,MAAOC,EAAQC,EAAUC,KACvB,MAAMC,EACqB,SAAzBL,EAAgBM,KACZR,EAAOI,SACDK,EAAUT,EAAVS,CAAkBL,GAE9B,OAAIG,EAAOG,YACF,CACLN,OAAQ,GACRb,OAAQoB,EACNvB,EACEmB,EAAOK,aACNN,EAAQO,2BACkB,QAAzBP,EAAQQ,cAEZR,KAKNA,EAAQO,2BAA6BE,EAAuB,GAAIT,GAEzD,CAAEF,OAAAA,EAAQb,OAAQ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{toNestError as r,validateFieldsNatively as e}from"@hookform/resolvers";import o from"vest/promisify";var t=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},s=function(s,i,n){return void 0===n&&(n={}),function(i,a,u){try{var v=function(o){return o.hasErrors()?{values:{},errors:r(t(o.getErrors(),!u.shouldUseNativeValidation&&"all"===u.criteriaMode),u)}:(u.shouldUseNativeValidation&&e({},u),{values:i,errors:{}})};return Promise.resolve("sync"===n.mode?v(s(i)):Promise.resolve(o(s)(i)).then(v))}catch(r){return Promise.reject(r)}}};export{s as vestResolver};
|
|
2
|
+
//# sourceMappingURL=vest.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vest.module.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError, validateFieldsNatively } 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, _, resolverOptions = {}) =>\n async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestError(\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","path","message","type","types","reduce","acc","index","vestResolver","schema","_","resolverOptions","values","_context","options","result","hasErrors","toNestError","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","mode","promisify"],"mappings":"4GAKA,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,EACX,SAACC,EAAQC,EAAGC,mBAAAA,IAAAA,EAAkB,aACvBC,EAAQC,EAAUC,sBACjBC,GAKN,OAAIA,EAAOC,YACF,CACLJ,OAAQ,GACRZ,OAAQiB,EACNpB,EACEkB,EAAOG,aACNJ,EAAQK,2BACkB,QAAzBL,EAAQM,cAEZN,KAKNA,EAAQK,2BAA6BE,EAAuB,GAAIP,GAEzD,CAAEF,OAAAA,EAAQZ,OAAQ,6BApBE,SAAzBW,EAAgBW,OACZb,EAAOG,oBACDW,EAAUd,EAAVc,CAAkBX,aALhC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
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),i=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,n,f){try{var u=function(e){return e.hasErrors()?{values:{},errors:r.toNestError(i(e.getErrors(),!f.shouldUseNativeValidation&&"all"===f.criteriaMode),f)}:(f.shouldUseNativeValidation&&r.validateFieldsNatively({},f),{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
|
+
//# sourceMappingURL=vest.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vest.umd.js","sources":["../src/vest.ts"],"sourcesContent":["import { toNestError, validateFieldsNatively } 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, _, resolverOptions = {}) =>\n async (values, _context, options) => {\n const result =\n resolverOptions.mode === 'sync'\n ? schema(values)\n : await promisify(schema)(values);\n\n if (result.hasErrors()) {\n return {\n values: {},\n errors: toNestError(\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","path","message","type","types","reduce","acc","index","schema","_","resolverOptions","values","_context","options","result","hasErrors","toNestError","getErrors","shouldUseNativeValidation","criteriaMode","validateFieldsNatively","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,kBAIP,SAACQ,EAAQC,EAAGC,mBAAAA,IAAAA,EAAkB,aACvBC,EAAQC,EAAUC,sBACjBC,GAKN,OAAIA,EAAOC,YACF,CACLJ,OAAQ,GACRX,OAAQgB,cACNnB,EACEiB,EAAOG,aACNJ,EAAQK,2BACkB,QAAzBL,EAAQM,cAEZN,KAKNA,EAAQK,2BAA6BE,yBAAuB,GAAIP,GAEzD,CAAEF,OAAAA,EAAQX,OAAQ,6BApBE,SAAzBU,EAAgBW,OACZb,EAAOG,oBACDW,UAAUd,EAAVc,CAAkBX,aALhC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vest",
|
|
3
|
+
"amdName": "hookformResolversVest",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": true,
|
|
6
|
+
"description": "React Hook Form validation resolver: vest",
|
|
7
|
+
"main": "dist/vest.js",
|
|
8
|
+
"module": "dist/vest.module.js",
|
|
9
|
+
"umd:main": "dist/vest.umd.js",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react-hook-form": "^7.0.0",
|
|
15
|
+
"@hookform/resolvers": "^2.0.0",
|
|
16
|
+
"vest": ">=3.0.0"
|
|
17
|
+
}
|
|
18
|
+
}
|