@hookform/resolvers 1.3.4 → 1.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -5
- package/dist/convertArrayToPathName.d.ts +1 -0
- package/dist/ie11/joi/index.d.ts +1 -0
- package/dist/ie11/{joi.d.ts → joi/joi.d.ts} +0 -0
- package/dist/ie11/joi/joi.js +2 -0
- package/dist/ie11/joi/joi.js.map +1 -0
- package/dist/ie11/superstruct/index.d.ts +1 -0
- package/dist/ie11/{superstruct.d.ts → superstruct/superstruct.d.ts} +0 -0
- package/dist/ie11/superstruct/superstruct.js +2 -0
- package/dist/ie11/superstruct/superstruct.js.map +1 -0
- package/dist/ie11/vest/index.d.ts +1 -0
- package/dist/ie11/{vest.d.ts → vest/vest.d.ts} +0 -0
- package/dist/ie11/vest/vest.js +2 -0
- package/dist/ie11/vest/vest.js.map +1 -0
- package/dist/ie11/yup/index.d.ts +1 -0
- package/dist/{yup.d.ts → ie11/yup/yup.d.ts} +6 -5
- package/dist/ie11/yup/yup.js +2 -0
- package/dist/ie11/yup/yup.js.map +1 -0
- package/dist/ie11/zod/index.d.ts +1 -0
- package/dist/ie11/{zod.d.ts → zod/zod.d.ts} +0 -0
- package/dist/ie11/zod/zod.js +2 -0
- package/dist/ie11/zod/zod.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/resolvers.js +2 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/resolvers.mjs +2 -0
- package/dist/resolvers.modern.js +2 -0
- package/dist/resolvers.modern.js.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/joi/dist/index.d.ts +1 -0
- package/{dist → joi/dist}/joi.d.ts +3 -3
- 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/package.json +17 -0
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +13 -0
- package/joi/src/__tests__/joi.ts +49 -0
- package/joi/src/index.ts +1 -0
- package/joi/src/joi.ts +76 -0
- package/package.json +83 -39
- package/superstruct/dist/index.d.ts +1 -0
- package/{dist → superstruct/dist}/superstruct.d.ts +5 -5
- 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/package.json +17 -0
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +33 -0
- package/superstruct/src/__tests__/superstruct.ts +45 -0
- package/superstruct/src/index.ts +1 -0
- package/superstruct/src/superstruct.ts +68 -0
- package/vest/dist/index.d.ts +1 -0
- package/{dist → vest/dist}/vest.d.ts +5 -5
- 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 +17 -0
- package/vest/src/__tests__/vest.ts +120 -0
- package/vest/src/index.ts +1 -0
- package/vest/src/vest.ts +59 -0
- package/yup/dist/index.d.ts +1 -0
- package/{dist/ie11 → yup/dist}/yup.d.ts +2 -1
- 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__/__snapshots__/yup.ts.snap +119 -0
- package/yup/src/__tests__/yup.ts +311 -0
- package/yup/src/index.ts +1 -0
- package/yup/src/yup.ts +96 -0
- package/zod/dist/index.d.ts +1 -0
- package/{dist → zod/dist}/zod.d.ts +4 -4
- 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__/__snapshots__/zod.ts.snap +194 -0
- package/zod/src/__tests__/zod.ts +131 -0
- package/zod/src/index.ts +1 -0
- package/zod/src/zod.ts +84 -0
- package/CHANGELOG.md +0 -7
- package/dist/ie11/_virtual/_tslib.js +0 -2
- package/dist/ie11/_virtual/_tslib.js.map +0 -1
- package/dist/ie11/joi.js +0 -2
- package/dist/ie11/joi.js.map +0 -1
- package/dist/ie11/superstruct.js +0 -2
- package/dist/ie11/superstruct.js.map +0 -1
- package/dist/ie11/utils/convertArrayToPathName.d.ts +0 -2
- package/dist/ie11/utils/convertArrayToPathName.js +0 -2
- package/dist/ie11/utils/convertArrayToPathName.js.map +0 -1
- package/dist/ie11/vest.js +0 -2
- package/dist/ie11/vest.js.map +0 -1
- package/dist/ie11/yup.js +0 -2
- package/dist/ie11/yup.js.map +0 -1
- package/dist/ie11/zod.js +0 -2
- package/dist/ie11/zod.js.map +0 -1
- package/dist/joi.js +0 -38
- package/dist/joi.js.map +0 -1
- package/dist/superstruct.js +0 -36
- package/dist/superstruct.js.map +0 -1
- package/dist/umd/index.js +0 -2
- package/dist/umd/index.js.map +0 -1
- package/dist/utils/convertArrayToPathName.d.ts +0 -2
- package/dist/utils/convertArrayToPathName.js +0 -6
- package/dist/utils/convertArrayToPathName.js.map +0 -1
- package/dist/vest.js +0 -26
- package/dist/vest.js.map +0 -1
- package/dist/yup.js +0 -59
- package/dist/yup.js.map +0 -1
- package/dist/zod.js +0 -44
- package/dist/zod.js.map +0 -1
- package/joi.d.ts +0 -1
- package/joi.js +0 -1
- package/superstruct.d.ts +0 -1
- package/superstruct.js +0 -1
- package/vest.d.ts +0 -1
- package/vest.js +0 -1
- package/yup.d.ts +0 -1
- package/yup.js +0 -1
- package/zod.d.ts +0 -1
- package/zod.js +0 -1
package/README.md
CHANGED
|
@@ -65,6 +65,8 @@ const App = () => {
|
|
|
65
65
|
</form>
|
|
66
66
|
);
|
|
67
67
|
};
|
|
68
|
+
|
|
69
|
+
export default App;
|
|
68
70
|
```
|
|
69
71
|
|
|
70
72
|
### [Zod](https://github.com/vriad/zod)
|
|
@@ -115,11 +117,11 @@ A simple and composable way to validate data in JavaScript (or TypeScript).
|
|
|
115
117
|
import React from 'react';
|
|
116
118
|
import { useForm } from 'react-hook-form';
|
|
117
119
|
import { superstructResolver } from '@hookform/resolvers/superstruct';
|
|
118
|
-
import {
|
|
120
|
+
import { object, string, number } from 'superstruct';
|
|
119
121
|
|
|
120
|
-
const schema =
|
|
121
|
-
name:
|
|
122
|
-
age:
|
|
122
|
+
const schema = object({
|
|
123
|
+
name: string(),
|
|
124
|
+
age: number(),
|
|
123
125
|
});
|
|
124
126
|
|
|
125
127
|
const App = () => {
|
|
@@ -130,11 +132,13 @@ const App = () => {
|
|
|
130
132
|
return (
|
|
131
133
|
<form onSubmit={handleSubmit((d) => console.log(d))}>
|
|
132
134
|
<input name="name" ref={register} />
|
|
133
|
-
<input name="age" type="number" ref={register} />
|
|
135
|
+
<input name="age" type="number" ref={register({ valueAsNumber: true })} />
|
|
134
136
|
<input type="submit" />
|
|
135
137
|
</form>
|
|
136
138
|
);
|
|
137
139
|
};
|
|
140
|
+
|
|
141
|
+
export default App;
|
|
138
142
|
```
|
|
139
143
|
|
|
140
144
|
### [Joi](https://github.com/sideway/joi)
|
|
@@ -166,6 +170,8 @@ const App = () => {
|
|
|
166
170
|
</form>
|
|
167
171
|
);
|
|
168
172
|
};
|
|
173
|
+
|
|
174
|
+
export default App;
|
|
169
175
|
```
|
|
170
176
|
|
|
171
177
|
### [Vest](https://github.com/ealush/vest)
|
|
@@ -219,6 +225,8 @@ const App = () => {
|
|
|
219
225
|
</form>
|
|
220
226
|
);
|
|
221
227
|
};
|
|
228
|
+
|
|
229
|
+
export default App;
|
|
222
230
|
```
|
|
223
231
|
|
|
224
232
|
## Backers
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const convertArrayToPathName: (paths: (string | number)[]) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './joi';
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("react-hook-form/dist/index.ie11"),e=require("@hookform/resolvers");function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}var n=function(n,o){return Array.isArray(n.details)?n.details.reduce(function(n,a){var s,i,u,c=a.path,v=a.message,f=void 0===v?"":v,l=a.type,y=e.convertArrayToPathName(c);return t({},n,c?n[y]&&o?((s={})[y]=r.appendErrors(y,o,n,l,f),s):((u={})[y]=n[y]||t({message:f,type:l},o?{types:(i={},i[l]=f||!0,i)}:{}),u):{})},{}):[]};exports.joiResolver=function(e,o){return void 0===o&&(o={abortEarly:!1}),function(a,s,i){void 0===i&&(i=!1);try{return Promise.resolve(function(r,n){try{var i=Promise.resolve(e.validateAsync(a,t({},o,{context:s}))).then(function(r){return{values:r,errors:{}}})}catch(r){return n(r)}return i&&i.then?i.then(void 0,n):i}(0,function(e){return{values:{},errors:r.transformToNestObject(n(e,i))}}))}catch(r){return Promise.reject(r)}}};
|
|
2
|
+
//# sourceMappingURL=joi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"joi.js","sources":["../src/joi.ts"],"sourcesContent":["import {\n appendErrors,\n transformToNestObject,\n Resolver,\n FieldValues,\n} from 'react-hook-form';\nimport * as Joi from 'joi';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n error: Joi.ValidationError,\n validateAllFieldCriteria: boolean,\n) =>\n Array.isArray(error.details)\n ? error.details.reduce(\n (previous: Record<string, any>, { path, message = '', type }) => {\n const currentPath = convertArrayToPathName(path);\n\n return {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n },\n {},\n )\n : [];\n\nexport const joiResolver = <TFieldValues extends FieldValues>(\n schema: Joi.Schema,\n options: Joi.AsyncValidationOptions = {\n abortEarly: false,\n },\n): Resolver<TFieldValues> => async (\n values,\n context,\n validateAllFieldCriteria = false,\n) => {\n try {\n return {\n values: await schema.validateAsync(values, {\n ...options,\n context,\n }),\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(e, validateAllFieldCriteria),\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","error","validateAllFieldCriteria","Array","isArray","details","reduce","previous","path","message","type","currentPath","convertArrayToPathName","appendErrors","types","schema","options","abortEarly","values","context","validateAsync","errors","e","transformToNestObject"],"mappings":"iSASA,IAAMA,EAAmB,SACvBC,EACAC,UAEAC,MAAMC,QAAQH,EAAMI,SAChBJ,EAAMI,QAAQC,OACZ,SAACC,eAAiCC,IAAAA,SAAMC,QAAAA,aAAU,KAAIC,IAAAA,KAC9CC,EAAcC,yBAAuBJ,GAE3C,YACKD,EACCC,EACAD,EAASI,IAAgBT,UAEpBS,GAAcE,eACbF,EACAT,EACAK,EACAG,EACAD,cAIDE,GAAcJ,EAASI,OACtBF,QAAAA,EACAC,KAAAA,GACIR,EACA,CACEY,cAAUJ,GAAOD,IAAW,MAE9B,OAGV,KAGR,IAEF,wBAEqB,SACzBM,EACAC,mBAAAA,IAAAA,EAAsC,CACpCC,YAAY,aAGdC,EACAC,EACAjB,YAAAA,IAAAA,GAA2B,sEAITa,EAAOK,cAAcF,OAC9BF,GACHG,QAAAA,uBAHJ,MAAO,CACLD,SAIAG,OAAQ,2EAEHC,GACP,MAAO,CACLJ,OAAQ,GACRG,OAAQE,wBACNvB,EAAiBsB,EAAGpB,QAtBD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './superstruct';
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("react-hook-form/dist/index.ie11"),e=require("@hookform/resolvers");function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o])}return r}).apply(this,arguments)}class o extends TypeError{constructor(r,e){let t;const{message:o,...n}=r,{path:s}=r;super(0===s.length?o:"At path: "+s.join(".")+" -- "+o),Object.assign(this,n),this.name=this.constructor.name,this.failures=()=>{var o;return null!=(o=t)?o:t=[r,...e()]}}}function*n(r,e,t={}){const{path:o=[],branch:s=[r],coerce:i=!1}=t,a={path:o,branch:s};i&&(r=e.coercer(r,a));let c=!0;for(const t of e.validator(r,a))c=!1,yield[t,void 0];if(c)for(const t of e.refiner(r,a))c=!1,yield[t,void 0];for(let[t,f,l]of e.entries(r,a)){const e=n(f,l,{path:void 0===t?o:[...o,t],branch:void 0===t?s:[...s,f],coerce:i});for(const o of e)o[0]?(c=!1,yield[o[0],void 0]):i&&(f=o[1],void 0===t?r=f:r instanceof Map?r.set(t,f):r instanceof Set?r.add(f):"object"==typeof(u=r)&&null!=u&&(r[t]=f))}var u;c&&(yield[void 0,r])}function s(r,e,t={}){const s=n(r,e,t),i=function(r){const{done:e,value:t}=r.next();return e?void 0:t}(s);return i[0]?[new o(i[0],function*(){for(const r of s)r[0]&&(yield r[0])}),void 0]:[void 0,i[1]]}var i=function(o,n){return o.failures().reduce(function(o,s){var i,a,c,u=s.path,f=s.message,l=void 0===f?"":f,v=s.type,d=e.convertArrayToPathName(u);return t({},o,u?o[d]&&n?((i={})[d]=r.appendErrors(d,n,o,v||"",l),i):((c={})[d]=o[d]||t({message:l,type:v},n?{types:(a={},a[v||""]=l||!0,a)}:{}),c):{})},{})};exports.superstructResolver=function(e,t){return function(o,n,a){void 0===a&&(a=!1);var c=s(o,e,t),u=c[0],f=c[1];return null!=u?{values:{},errors:r.transformToNestObject(i(u,a))}:{values:f,errors:{}}}};
|
|
2
|
+
//# sourceMappingURL=superstruct.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"superstruct.js","sources":["../../node_modules/superstruct/lib/index.es.js","../src/superstruct.ts"],"sourcesContent":["/**\n * A `StructFailure` represents a single specific failure in validation.\n */\n\n/**\n * `StructError` objects are thrown (or returned) when validation fails.\n *\n * Validation logic is design to exit early for maximum performance. The error\n * represents the first error encountered during validation. For more detail,\n * the `error.failures` property is a generator function that can be run to\n * continue validation and receive all the failures in the data.\n */\nclass StructError extends TypeError {\n constructor(failure, failures) {\n let cached;\n const {\n message,\n ...rest\n } = failure;\n const {\n path\n } = failure;\n const msg = path.length === 0 ? message : \"At path: \" + path.join('.') + \" -- \" + message;\n super(msg);\n Object.assign(this, rest);\n this.name = this.constructor.name;\n\n this.failures = () => {\n var _cached;\n\n return (_cached = cached) != null ? _cached : cached = [failure, ...failures()];\n };\n }\n\n}\n\n/**\n * Check if a value is an iterator.\n */\nfunction isIterable(x) {\n return isObject(x) && typeof x[Symbol.iterator] === 'function';\n}\n/**\n * Check if a value is a plain object.\n */\n\n\nfunction isObject(x) {\n return typeof x === 'object' && x != null;\n}\n/**\n * Check if a value is a plain object.\n */\n\nfunction isPlainObject(x) {\n if (Object.prototype.toString.call(x) !== '[object Object]') {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(x);\n return prototype === null || prototype === Object.prototype;\n}\n/**\n * Return a value as a printable string.\n */\n\nfunction print(value) {\n return typeof value === 'string' ? JSON.stringify(value) : \"\" + value;\n}\n/**\n * Shifts (removes and returns) the first value from the `input` iterator.\n * Like `Array.prototype.shift()` but for an `Iterator`.\n */\n\nfunction shiftIterator(input) {\n const {\n done,\n value\n } = input.next();\n return done ? undefined : value;\n}\n/**\n * Convert a single validation result to a failure.\n */\n\nfunction toFailure(result, context, struct, value) {\n if (result === true) {\n return;\n } else if (result === false) {\n result = {};\n } else if (typeof result === 'string') {\n result = {\n message: result\n };\n }\n\n const {\n path,\n branch\n } = context;\n const {\n type\n } = struct;\n const {\n refinement,\n message = \"Expected a value of type `\" + type + \"`\" + (refinement ? \" with refinement `\" + refinement + \"`\" : '') + \", but received: `\" + print(value) + \"`\"\n } = result;\n return {\n value,\n type,\n refinement,\n key: path[path.length - 1],\n path,\n branch,\n ...result,\n message\n };\n}\n/**\n * Convert a validation result to an iterable of failures.\n */\n\nfunction* toFailures(result, context, struct, value) {\n if (!isIterable(result)) {\n result = [result];\n }\n\n for (const r of result) {\n const failure = toFailure(r, context, struct, value);\n\n if (failure) {\n yield failure;\n }\n }\n}\n/**\n * Check a value against a struct, traversing deeply into nested values, and\n * returning an iterator of failures or success.\n */\n\nfunction* run(value, struct, options = {}) {\n const {\n path = [],\n branch = [value],\n coerce = false\n } = options;\n const ctx = {\n path,\n branch\n };\n\n if (coerce) {\n value = struct.coercer(value, ctx);\n }\n\n let valid = true;\n\n for (const failure of struct.validator(value, ctx)) {\n valid = false;\n yield [failure, undefined];\n }\n\n if (valid) {\n for (const failure of struct.refiner(value, ctx)) {\n valid = false;\n yield [failure, undefined];\n }\n }\n\n for (let [k, v, s] of struct.entries(value, ctx)) {\n const ts = run(v, s, {\n path: k === undefined ? path : [...path, k],\n branch: k === undefined ? branch : [...branch, v],\n coerce\n });\n\n for (const t of ts) {\n if (t[0]) {\n valid = false;\n yield [t[0], undefined];\n } else if (coerce) {\n v = t[1];\n\n if (k === undefined) {\n value = v;\n } else if (value instanceof Map) {\n value.set(k, v);\n } else if (value instanceof Set) {\n value.add(v);\n } else if (isObject(value)) {\n value[k] = v;\n }\n }\n }\n }\n\n if (valid) {\n yield [undefined, value];\n }\n}\n\nfunction assign(...Structs) {\n const schemas = Structs.map(s => s.schema);\n const schema = Object.assign({}, ...schemas);\n return object(schema);\n}\n/**\n * Define a new struct type with a custom validation function.\n */\n\nfunction define(name, validator) {\n return new Struct({\n type: name,\n schema: null,\n validator\n });\n}\n/**\n * Create a struct with dynamic validation logic.\n *\n * The callback will receive the value currently being validated, and must\n * return a struct object to validate it with. This can be useful to model\n * validation logic that changes based on its input.\n */\n\nfunction dynamic(fn) {\n return new Struct({\n type: 'dynamic',\n schema: null,\n\n *entries(value, ctx) {\n const struct = fn(value, ctx);\n yield* struct.entries(value, ctx);\n },\n\n validator(value, ctx) {\n const struct = fn(value, ctx);\n return struct.validator(value, ctx);\n },\n\n coercer(value, ctx) {\n const struct = fn(value, ctx);\n return struct.coercer(value, ctx);\n }\n\n });\n}\n/**\n * Create a struct with lazily evaluated validation logic.\n *\n * The first time validation is run with the struct, the callback will be called\n * and must return a struct object to use. This is useful for cases where you\n * want to have self-referential structs for nested data structures to avoid a\n * circular definition problem.\n */\n\nfunction lazy(fn) {\n let struct;\n return new Struct({\n type: 'lazy',\n schema: null,\n\n *entries(value, ctx) {\n var _struct;\n\n (_struct = struct) != null ? _struct : struct = fn();\n yield* struct.entries(value, ctx);\n },\n\n validator(value, ctx) {\n var _struct2;\n\n (_struct2 = struct) != null ? _struct2 : struct = fn();\n return struct.validator(value, ctx);\n },\n\n coercer(value, ctx) {\n var _struct3;\n\n (_struct3 = struct) != null ? _struct3 : struct = fn();\n return struct.coercer(value, ctx);\n }\n\n });\n}\n/**\n * Create a new struct based on an existing object struct, but excluding\n * specific properties.\n *\n * Like TypeScript's `Omit` utility.\n */\n\nfunction omit(struct, keys) {\n const {\n schema\n } = struct;\n const subschema = { ...schema\n };\n\n for (const key of keys) {\n delete subschema[key];\n }\n\n return object(subschema);\n}\n/**\n * Create a new struct based on an existing object struct, but with all of its\n * properties allowed to be `undefined`.\n *\n * Like TypeScript's `Partial` utility.\n */\n\nfunction partial(struct) {\n const schema = struct instanceof Struct ? { ...struct.schema\n } : { ...struct\n };\n\n for (const key in schema) {\n schema[key] = optional(schema[key]);\n }\n\n return object(schema);\n}\n/**\n * Create a new struct based on an existing object struct, but only including\n * specific properties.\n *\n * Like TypeScript's `Pick` utility.\n */\n\nfunction pick(struct, keys) {\n const {\n schema\n } = struct;\n const subschema = {};\n\n for (const key of keys) {\n subschema[key] = schema[key];\n }\n\n return object(subschema);\n}\n/**\n * Define a new struct type with a custom validation function.\n *\n * @deprecated This function has been renamed to `define`.\n */\n\nfunction struct(name, validator) {\n console.warn('superstruct@0.11 - The `struct` helper has been renamed to `define`.');\n return define(name, validator);\n}\n\n/**\n * Ensure that any value passes validation.\n */\n\nfunction any() {\n return define('any', () => true);\n}\nfunction array(Element) {\n return new Struct({\n type: 'array',\n schema: Element,\n\n *entries(value) {\n if (Element && Array.isArray(value)) {\n for (const [i, v] of value.entries()) {\n yield [i, v, Element];\n }\n }\n },\n\n coercer(value) {\n return Array.isArray(value) ? value.slice() : value;\n },\n\n validator(value) {\n return Array.isArray(value) || \"Expected an array value, but received: \" + print(value);\n }\n\n });\n}\n/**\n * Ensure that a value is a boolean.\n */\n\nfunction boolean() {\n return define('boolean', value => {\n return typeof value === 'boolean';\n });\n}\n/**\n * Ensure that a value is a valid `Date`.\n *\n * Note: this also ensures that the value is *not* an invalid `Date` object,\n * which can occur when parsing a date fails but still returns a `Date`.\n */\n\nfunction date() {\n return define('date', value => {\n return value instanceof Date && !isNaN(value.getTime()) || \"Expected a valid `Date` object, but received: \" + print(value);\n });\n}\nfunction enums(values) {\n const schema = {};\n const description = values.map(v => print(v)).join();\n\n for (const key of values) {\n schema[key] = key;\n }\n\n return new Struct({\n type: 'enums',\n schema,\n\n validator(value) {\n return values.includes(value) || \"Expected one of `\" + description + \"`, but received: \" + print(value);\n }\n\n });\n}\n/**\n * Ensure that a value is a function.\n */\n\nfunction func() {\n return define('func', value => {\n return typeof value === 'function' || \"Expected a function, but received: \" + print(value);\n });\n}\n/**\n * Ensure that a value is an instance of a specific class.\n */\n\nfunction instance(Class) {\n return define('instance', value => {\n return value instanceof Class || \"Expected a `\" + Class.name + \"` instance, but received: \" + print(value);\n });\n}\n/**\n * Ensure that a value is an integer.\n */\n\nfunction integer() {\n return define('integer', value => {\n return typeof value === 'number' && !isNaN(value) && Number.isInteger(value) || \"Expected an integer, but received: \" + print(value);\n });\n}\nfunction intersection(Structs) {\n return new Struct({\n type: 'intersection',\n schema: null,\n\n *entries(value, ctx) {\n for (const S of Structs) {\n yield* S.entries(value, ctx);\n }\n },\n\n *validator(value, ctx) {\n for (const S of Structs) {\n yield* S.validator(value, ctx);\n }\n }\n\n });\n}\nfunction literal(constant) {\n const description = print(constant);\n return define('literal', value => {\n return value === constant || \"Expected the literal `\" + description + \"`, but received: \" + print(value);\n });\n}\nfunction map(Key, Value) {\n return new Struct({\n type: 'map',\n schema: null,\n\n *entries(value) {\n if (Key && Value && value instanceof Map) {\n for (const [k, v] of value.entries()) {\n yield [k, k, Key];\n yield [k, v, Value];\n }\n }\n },\n\n coercer(value) {\n return value instanceof Map ? new Map(value) : value;\n },\n\n validator(value) {\n return value instanceof Map || \"Expected a `Map` object, but received: \" + print(value);\n }\n\n });\n}\n/**\n * Ensure that no value ever passes validation.\n */\n\nfunction never() {\n return define('never', () => false);\n}\n/**\n * Augment an existing struct to allow `null` values.\n */\n\nfunction nullable(struct) {\n return new Struct({ ...struct,\n validator: (value, ctx) => value === null || struct.validator(value, ctx),\n refiner: (value, ctx) => value === null || struct.refiner(value, ctx)\n });\n}\n/**\n * Ensure that a value is a number.\n */\n\nfunction number() {\n return define('number', value => {\n return typeof value === 'number' && !isNaN(value) || \"Expected a number, but received: \" + print(value);\n });\n}\nfunction object(schema) {\n const knowns = schema ? Object.keys(schema) : [];\n const Never = never();\n return new Struct({\n type: 'object',\n schema: schema ? schema : null,\n\n *entries(value) {\n if (schema && isObject(value)) {\n const unknowns = new Set(Object.keys(value));\n\n for (const key of knowns) {\n unknowns.delete(key);\n yield [key, value[key], schema[key]];\n }\n\n for (const key of unknowns) {\n yield [key, value[key], Never];\n }\n }\n },\n\n validator(value) {\n return isObject(value) || \"Expected an object, but received: \" + print(value);\n },\n\n coercer(value) {\n return isObject(value) ? { ...value\n } : value;\n }\n\n });\n}\n/**\n * Augment a struct to allow `undefined` values.\n */\n\nfunction optional(struct) {\n return new Struct({ ...struct,\n validator: (value, ctx) => value === undefined || struct.validator(value, ctx),\n refiner: (value, ctx) => value === undefined || struct.refiner(value, ctx)\n });\n}\n/**\n * Ensure that a value is an object with keys and values of specific types, but\n * without ensuring any specific shape of properties.\n *\n * Like TypeScript's `Record` utility.\n */\n\nfunction record(Key, Value) {\n return new Struct({\n type: 'record',\n schema: null,\n\n *entries(value) {\n if (isObject(value)) {\n for (const k in value) {\n const v = value[k];\n yield [k, k, Key];\n yield [k, v, Value];\n }\n }\n },\n\n validator(value) {\n return isObject(value) || \"Expected an object, but received: \" + print(value);\n }\n\n });\n}\n/**\n * Ensure that a value is a `RegExp`.\n *\n * Note: this does not test the value against the regular expression! For that\n * you need to use the `pattern()` refinement.\n */\n\nfunction regexp() {\n return define('regexp', value => {\n return value instanceof RegExp;\n });\n}\nfunction set(Element) {\n return new Struct({\n type: 'set',\n schema: null,\n\n *entries(value) {\n if (Element && value instanceof Set) {\n for (const v of value) {\n yield [v, v, Element];\n }\n }\n },\n\n coercer(value) {\n return value instanceof Set ? new Set(value) : value;\n },\n\n validator(value) {\n return value instanceof Set || \"Expected a `Set` object, but received: \" + print(value);\n }\n\n });\n}\n/**\n * Ensure that a value is a string.\n */\n\nfunction string() {\n return define('string', value => {\n return typeof value === 'string' || \"Expected a string, but received: \" + print(value);\n });\n}\nfunction tuple(Elements) {\n const Never = never();\n return new Struct({\n type: 'tuple',\n schema: null,\n\n *entries(value) {\n if (Array.isArray(value)) {\n const length = Math.max(Elements.length, value.length);\n\n for (let i = 0; i < length; i++) {\n yield [i, value[i], Elements[i] || Never];\n }\n }\n },\n\n validator(value) {\n return Array.isArray(value) || \"Expected an array, but received: \" + print(value);\n }\n\n });\n}\n/**\n * Ensure that a value has a set of known properties of specific types.\n *\n * Note: Unrecognized properties are allowed and untouched. This is similar to\n * how TypeScript's structural typing works.\n */\n\nfunction type(schema) {\n const keys = Object.keys(schema);\n return new Struct({\n type: 'type',\n schema,\n\n *entries(value) {\n if (isObject(value)) {\n for (const k of keys) {\n yield [k, value[k], schema[k]];\n }\n }\n },\n\n validator(value) {\n return isObject(value) || \"Expected an object, but received: \" + print(value);\n }\n\n });\n}\nfunction union(Structs) {\n const description = Structs.map(s => s.type).join(' | ');\n return new Struct({\n type: 'union',\n schema: null,\n\n validator(value, ctx) {\n const failures = [];\n\n for (const S of Structs) {\n const [...tuples] = run(value, S, ctx);\n const [first] = tuples;\n\n if (!first[0]) {\n return [];\n } else {\n for (const [failure] of tuples) {\n if (failure) {\n failures.push(failure);\n }\n }\n }\n }\n\n return [\"Expected the value to satisfy a union of `\" + description + \"`, but received: \" + print(value), ...failures];\n }\n\n });\n}\n/**\n * Ensure that any value passes validation, without widening its type to `any`.\n */\n\nfunction unknown() {\n return define('unknown', () => true);\n}\n\n/**\n * Augment a `Struct` to add an additional coercion step to its input.\n *\n * This allows you to transform input data before validating it, to increase the\n * likelihood that it passes validation—for example for default values, parsing\n * different formats, etc.\n *\n * Note: You must use `create(value, Struct)` on the value to have the coercion\n * take effect! Using simply `assert()` or `is()` will not use coercion.\n */\n\nfunction coerce(struct, condition, coercer) {\n return new Struct({ ...struct,\n coercer: (value, ctx) => {\n return is(value, condition) ? struct.coercer(coercer(value, ctx), ctx) : struct.coercer(value, ctx);\n }\n });\n}\n/**\n * Augment a struct to replace `undefined` values with a default.\n *\n * Note: You must use `create(value, Struct)` on the value to have the coercion\n * take effect! Using simply `assert()` or `is()` will not use coercion.\n */\n\nfunction defaulted(struct, fallback, options = {}) {\n return coerce(struct, unknown(), x => {\n const f = typeof fallback === 'function' ? fallback() : fallback;\n\n if (x === undefined) {\n return f;\n }\n\n if (!options.strict && isPlainObject(x) && isPlainObject(f)) {\n const ret = { ...x\n };\n let changed = false;\n\n for (const key in f) {\n if (ret[key] === undefined) {\n ret[key] = f[key];\n changed = true;\n }\n }\n\n if (changed) {\n return ret;\n }\n }\n\n return x;\n });\n}\n/**\n * Augment a struct to mask its input to only properties defined in the struct.\n *\n * Note: You must use `create(value, Struct)` on the value to have the coercion\n * take effect! Using simply `assert()` or `is()` will not use coercion.\n */\n\nfunction masked(struct) {\n return coerce(struct, unknown(), x => {\n if (typeof struct.schema !== 'object' || struct.schema == null || typeof x !== 'object' || x == null) {\n return x;\n } else {\n const ret = {};\n\n for (const key in struct.schema) {\n if (key in x) {\n ret[key] = x[key];\n }\n }\n\n return ret;\n }\n });\n}\n/**\n * Augment a struct to trim string inputs.\n *\n * Note: You must use `create(value, Struct)` on the value to have the coercion\n * take effect! Using simply `assert()` or `is()` will not use coercion.\n */\n\nfunction trimmed(struct) {\n return coerce(struct, string(), x => x.trim());\n}\n\n/**\n * `Struct` objects encapsulate the validation logic for a specific type of\n * values. Once constructed, you use the `assert`, `is` or `validate` helpers to\n * validate unknown input data against the struct.\n */\n\nclass Struct {\n constructor(props) {\n const {\n type,\n schema,\n validator,\n refiner,\n coercer = value => value,\n entries = function* () {}\n } = props;\n this.type = type;\n this.schema = schema;\n this.entries = entries;\n this.coercer = coercer;\n\n if (validator) {\n this.validator = (value, context) => {\n const result = validator(value, context);\n return toFailures(result, context, this, value);\n };\n } else {\n this.validator = () => [];\n }\n\n if (refiner) {\n this.refiner = (value, context) => {\n const result = refiner(value, context);\n return toFailures(result, context, this, value);\n };\n } else {\n this.refiner = () => [];\n }\n }\n /**\n * Assert that a value passes the struct's validation, throwing if it doesn't.\n */\n\n\n assert(value) {\n return assert(value, this);\n }\n /**\n * Create a value with the struct's coercion logic, then validate it.\n */\n\n\n create(value) {\n return create(value, this);\n }\n /**\n * Check if a value passes the struct's validation.\n */\n\n\n is(value) {\n return is(value, this);\n }\n /**\n * Mask a value, coercing and validating it, but returning only the subset of\n * properties defined by the struct's schema.\n */\n\n\n mask(value) {\n return mask(value, this);\n }\n /**\n * Validate a value with the struct's validation logic, returning a tuple\n * representing the result.\n *\n * You may optionally pass `true` for the `withCoercion` argument to coerce\n * the value before attempting to validate it. If you do, the result will\n * contain the coerced result when successful.\n */\n\n\n validate(value, options = {}) {\n return validate(value, this, options);\n }\n\n}\n/**\n * Assert that a value passes a struct, throwing if it doesn't.\n */\n\nfunction assert(value, struct) {\n const result = validate(value, struct);\n\n if (result[0]) {\n throw result[0];\n }\n}\n/**\n * Create a value with the coercion logic of struct and validate it.\n */\n\nfunction create(value, struct) {\n const result = validate(value, struct, {\n coerce: true\n });\n\n if (result[0]) {\n throw result[0];\n } else {\n return result[1];\n }\n}\n/**\n * Mask a value, returning only the subset of properties defined by a struct.\n */\n\nfunction mask(value, struct) {\n const M = masked(struct);\n const ret = create(value, M);\n return ret;\n}\n/**\n * Check if a value passes a struct.\n */\n\nfunction is(value, struct) {\n const result = validate(value, struct);\n return !result[0];\n}\n/**\n * Validate a value against a struct, returning an error if invalid, or the\n * value (with potential coercion) if valid.\n */\n\nfunction validate(value, struct, options = {}) {\n const tuples = run(value, struct, options);\n const tuple = shiftIterator(tuples);\n\n if (tuple[0]) {\n const error = new StructError(tuple[0], function* () {\n for (const t of tuples) {\n if (t[0]) {\n yield t[0];\n }\n }\n });\n return [error, undefined];\n } else {\n const v = tuple[1];\n return [undefined, v];\n }\n}\n\n/**\n * Ensure that a string, array, map, or set is empty.\n */\n\nfunction empty(struct) {\n const expected = \"Expected an empty \" + struct.type;\n return refine(struct, 'empty', value => {\n if (value instanceof Map || value instanceof Set) {\n const {\n size\n } = value;\n return size === 0 || expected + \" but received one with a size of `\" + size + \"`\";\n } else {\n const {\n length\n } = value;\n return length === 0 || expected + \" but received one with a length of `\" + length + \"`\";\n }\n });\n}\n/**\n * Ensure that a number or date is below a threshold.\n */\n\nfunction max(struct, threshold, options = {}) {\n const {\n exclusive\n } = options;\n return refine(struct, 'max', value => {\n return exclusive ? value < threshold : value <= threshold || \"Expected a \" + struct.type + \" greater than \" + (exclusive ? '' : 'or equal to ') + threshold + \" but received `\" + value + \"`\";\n });\n}\n/**\n * Ensure that a number or date is above a threshold.\n */\n\nfunction min(struct, threshold, options = {}) {\n const {\n exclusive\n } = options;\n return refine(struct, 'min', value => {\n return exclusive ? value > threshold : value >= threshold || \"Expected a \" + struct.type + \" greater than \" + (exclusive ? '' : 'or equal to ') + threshold + \" but received `\" + value + \"`\";\n });\n}\n/**\n * Ensure that a string matches a regular expression.\n */\n\nfunction pattern(struct, regexp) {\n return refine(struct, 'pattern', value => {\n return regexp.test(value) || \"Expected a \" + struct.type + \" matching `/\" + regexp.source + \"/` but received \\\"\" + value + \"\\\"\";\n });\n}\n/**\n * Ensure that a string, array, number, date, map, or set has a size (or length, or time) between `min` and `max`.\n */\n\nfunction size(struct, min, max = min) {\n const expected = \"Expected a \" + struct.type;\n const of = min === max ? \"of `\" + min + \"`\" : \"between `\" + min + \"` and `\" + max + \"`\";\n return refine(struct, 'size', value => {\n if (typeof value === 'number' || value instanceof Date) {\n return min <= value && value <= max || expected + \" \" + of + \" but received `\" + value + \"`\";\n } else if (value instanceof Map || value instanceof Set) {\n const {\n size\n } = value;\n return min <= size && size <= max || expected + \" with a size \" + of + \" but received one with a size of `\" + size + \"`\";\n } else {\n const {\n length\n } = value;\n return min <= length && length <= max || expected + \" with a length \" + of + \" but received one with a length of `\" + length + \"`\";\n }\n });\n}\n/**\n * Augment a `Struct` to add an additional refinement to the validation.\n *\n * The refiner function is guaranteed to receive a value of the struct's type,\n * because the struct's existing validation will already have passed. This\n * allows you to layer additional validation on top of existing structs.\n */\n\nfunction refine(struct, name, refiner) {\n return new Struct({ ...struct,\n\n *refiner(value, ctx) {\n yield* struct.refiner(value, ctx);\n const result = refiner(value, ctx);\n const failures = toFailures(result, ctx, struct, value);\n\n for (const failure of failures) {\n yield { ...failure,\n refinement: name\n };\n }\n }\n\n });\n}\n\nexport { Struct, StructError, any, array, assert, assign, boolean, coerce, create, date, defaulted, define, dynamic, empty, enums, func, instance, integer, intersection, is, lazy, literal, map, mask, masked, max, min, never, nullable, number, object, omit, optional, partial, pattern, pick, record, refine, regexp, set, size, string, struct, trimmed, tuple, type, union, unknown, validate };\n//# sourceMappingURL=index.es.js.map\n","import {\n appendErrors,\n transformToNestObject,\n Resolver,\n ResolverSuccess,\n ResolverError,\n} from 'react-hook-form';\nimport { StructError, validate, Struct, Infer } from 'superstruct';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n error: StructError,\n validateAllFieldCriteria: boolean,\n) =>\n error\n .failures()\n .reduce((previous: Record<string, any>, { path, message = '', type }) => {\n const currentPath = convertArrayToPathName(path);\n return {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type || '',\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type || '']: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n }, {});\n\ntype Options = Parameters<typeof validate>[2];\n\nexport const superstructResolver = <T extends Struct<any, any>>(\n schema: T,\n options?: Options,\n): Resolver<Infer<T>> => (values, _, validateAllFieldCriteria = false) => {\n const [errors, result] = validate(values, schema, options);\n\n if (errors != null) {\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(errors, validateAllFieldCriteria),\n ),\n } as ResolverError<Infer<T>>;\n }\n\n return {\n values: result,\n errors: {},\n } as ResolverSuccess<Infer<T>>;\n};\n"],"names":["StructError","TypeError","[object Object]","failure","failures","cached","message","rest","path","super","length","join","Object","assign","this","name","constructor","_cached","run","value","struct","options","branch","coerce","ctx","coercer","valid","validator","undefined","refiner","k","v","s","entries","ts","t","Map","set","Set","add","x","validate","tuples","tuple","input","done","next","shiftIterator","parseErrorSchema","error","validateAllFieldCriteria","reduce","previous","type","currentPath","convertArrayToPathName","appendErrors","types","schema","values","_","errors","result","transformToNestObject"],"mappings":"iSAYA,MAAMA,UAAoBC,UACxBC,YAAYC,EAASC,GACnB,IAAIC,EACJ,MAAMC,QACJA,KACGC,GACDJ,GACEK,KACJA,GACEL,EAEJM,MAD4B,IAAhBD,EAAKE,OAAeJ,EAAU,YAAcE,EAAKG,KAAK,KAAO,OAASL,GAElFM,OAAOC,OAAOC,KAAMP,GACpBO,KAAKC,KAAOD,KAAKE,YAAYD,KAE7BD,KAAKV,SAAW,KACd,IAAIa,EAEJ,OAA6B,OAArBA,EAAUZ,GAAkBY,EAAUZ,EAAS,CAACF,KAAYC,OA8G1E,SAAUc,EAAIC,EAAOC,EAAQC,EAAU,IACrC,MAAMb,KACJA,EAAO,GAAEc,OACTA,EAAS,CAACH,GAAMI,OAChBA,GAAS,GACPF,EACEG,EAAM,CACVhB,KAAAA,EACAc,OAAAA,GAGEC,IACFJ,EAAQC,EAAOK,QAAQN,EAAOK,IAGhC,IAAIE,GAAQ,EAEZ,IAAK,MAAMvB,KAAWiB,EAAOO,UAAUR,EAAOK,GAC5CE,GAAQ,OACF,CAACvB,OAASyB,GAGlB,GAAIF,EACF,IAAK,MAAMvB,KAAWiB,EAAOS,QAAQV,EAAOK,GAC1CE,GAAQ,OACF,CAACvB,OAASyB,GAIpB,IAAK,IAAKE,EAAGC,EAAGC,KAAMZ,EAAOa,QAAQd,EAAOK,GAAM,CAChD,MAAMU,EAAKhB,EAAIa,EAAGC,EAAG,CACnBxB,UAAYoB,IAANE,EAAkBtB,EAAO,IAAIA,EAAMsB,GACzCR,YAAcM,IAANE,EAAkBR,EAAS,IAAIA,EAAQS,GAC/CR,OAAAA,IAGF,IAAK,MAAMY,KAAKD,EACVC,EAAE,IACJT,GAAQ,OACF,CAACS,EAAE,QAAIP,IACJL,IACTQ,EAAII,EAAE,QAEIP,IAANE,EACFX,EAAQY,EACCZ,aAAiBiB,IAC1BjB,EAAMkB,IAAIP,EAAGC,GACJZ,aAAiBmB,IAC1BnB,EAAMoB,IAAIR,GA5IE,iBADJS,EA8IUrB,IA7IW,MAALqB,IA8IxBrB,EAAMW,GAAKC,IA/IrB,IAAkBS,EAqJZd,SACI,MAACE,EAAWT,IA+uBtB,SAASsB,EAAStB,EAAOC,EAAQC,EAAU,IACzC,MAAMqB,EAASxB,EAAIC,EAAOC,EAAQC,GAC5BsB,EA52BR,SAAuBC,GACrB,MAAMC,KACJA,EAAI1B,MACJA,GACEyB,EAAME,OACV,OAAOD,OAAOjB,EAAYT,EAu2BZ4B,CAAcL,GAE5B,OAAIC,EAAM,GAQD,CAPO,IAAI3C,EAAY2C,EAAM,GAAI,YACtC,IAAK,MAAMR,KAAKO,EACVP,EAAE,WACEA,EAAE,WAICP,GAGR,MAACA,EADEe,EAAM,ICx7BpB,IAAMK,EAAmB,SACvBC,EACAC,UAEAD,EACG7C,WACA+C,OAAO,SAACC,eAAiC5C,IAAAA,SAAMF,QAAAA,aAAU,KAAI+C,IAAAA,KACtDC,EAAcC,yBAAuB/C,GAC3C,YACK4C,EACC5C,EACA4C,EAASE,IAAgBJ,UAEpBI,GAAcE,eACbF,EACAJ,EACAE,EACAC,GAAQ,GACR/C,cAIDgD,GAAcF,EAASE,OACtBhD,QAAAA,EACA+C,KAAAA,GACIH,EACA,CACEO,cAAUJ,GAAQ,IAAK/C,IAAW,MAEpC,OAGV,KAEL,iCAI4B,SACjCoD,EACArC,mBACwBsC,EAAQC,EAAGV,YAAAA,IAAAA,GAA2B,SACrCT,EAASkB,EAAQD,EAAQrC,GAA3CwC,OAAQC,OAEf,OAAc,MAAVD,EACK,CACLF,OAAQ,GACRE,OAAQE,wBACNf,EAAiBa,EAAQX,KAKxB,CACLS,OAAQG,EACRD,OAAQ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vest';
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("react-hook-form/dist/index.ie11");function e(){return(e=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}var t=function(r,t){return Object.entries(r).reduce(function(r,n){var o,s=n[1];return e({},r,((o={})[n[0]]=e({type:"",message:s[0]},t?{types:s.reduce(function(r,t,n){var o;return e({},r,((o={})[n]=t,o))},{})}:{}),o))},{})};exports.vestResolver=function(e,n,o){return void 0===o&&(o=!1),function(n){try{var s=(u=e,function(){var r=arguments;return new Promise(function(e){return u.apply(void 0,[].slice.call(r)).done(e)})});return Promise.resolve(s(n)).then(function(e){var s=e.getErrors();return e.hasErrors()?{values:{},errors:r.transformToNestObject(t(s,o))}:{values:n,errors:{}}})}catch(r){return Promise.reject(r)}var u}};
|
|
2
|
+
//# sourceMappingURL=vest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vest.js","sources":["../src/vest.ts"],"sourcesContent":["import { FieldValues, Resolver, transformToNestObject } from 'react-hook-form';\nimport * as Vest from 'vest';\n\ntype VestErrors = Record<string, string[]>;\n\ntype ICreateResult = ReturnType<typeof Vest.create>;\n\ntype Promisify = <T extends ICreateResult, K>(\n fn: T,\n) => (args: K) => Promise<Vest.IVestResult>;\n\nconst promisify: Promisify = (validatorFn) => (...args) =>\n new Promise((resolve) => validatorFn(...args).done(resolve as Vest.DoneCB));\n\nconst parseErrorSchema = (\n vestError: VestErrors,\n validateAllFieldCriteria: boolean,\n) => {\n return Object.entries(vestError).reduce((prev, [key, value]) => {\n return {\n ...prev,\n [key]: {\n type: '',\n message: value[0],\n ...(validateAllFieldCriteria\n ? {\n types: value.reduce((prev, message, index) => {\n return {\n ...prev,\n [index]: message,\n };\n }, {}),\n }\n : {}),\n },\n };\n }, {});\n};\n\nexport const vestResolver = <TFieldValues extends FieldValues>(\n schema: ICreateResult,\n _: any = {},\n validateAllFieldCriteria = false,\n): Resolver<TFieldValues> => async (values) => {\n const validateSchema = promisify(schema);\n const result = await validateSchema(values);\n const errors = result.getErrors();\n\n if (!result.hasErrors()) {\n return { values: values as any, errors: {} };\n }\n\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(errors, validateAllFieldCriteria),\n ),\n };\n};\n"],"names":["parseErrorSchema","vestError","validateAllFieldCriteria","Object","entries","reduce","prev","value","type","message","types","index","schema","_","values","validateSchema","validatorFn","Promise","resolve","done","result","errors","getErrors","hasErrors","transformToNestObject"],"mappings":"gQAWA,IAGMA,EAAmB,SACvBC,EACAC,GAEA,OAAOC,OAAOC,QAAQH,GAAWI,OAAO,SAACC,WAAYC,OACnD,YACKD,mBAEDE,KAAM,GACNC,QAASF,EAAM,IACXL,EACA,CACEQ,MAAOH,EAAMF,OAAO,SAACC,EAAMG,EAASE,SAClC,YACKL,UACFK,GAAQF,OAEV,KAEL,SAGP,0BAGuB,SAC1BG,EACAC,EACAX,mBAAAA,IAAAA,GAA2B,YACOY,OAClC,IAAMC,GAjCsBC,EAiCKJ,wCAhC7BK,QAAQ,SAACC,UAAYF,iCAAqBG,KAAKD,8BAiC9BH,EAAeD,kBAA9BM,GACN,IAAMC,EAASD,EAAOE,mBAEjBF,EAAOG,YAIL,CACLT,OAAQ,GACRO,OAAQG,wBACNxB,EAAiBqB,EAAQnB,KANpB,CAAEY,OAAQA,EAAeO,OAAQ,MAVhB,kCA5BC,IAACL"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './yup';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Resolver } from 'react-hook-form';
|
|
2
|
-
import Yup from 'yup';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
1
|
+
import { Resolver } from 'react-hook-form';
|
|
2
|
+
import Yup from 'yup';
|
|
3
|
+
import Lazy from 'yup/lib/Lazy';
|
|
4
|
+
declare type ValidateOptions<T extends Yup.AnyObjectSchema> = Parameters<T['validate']>[1];
|
|
5
|
+
export declare const yupResolver: <TFieldValues extends Record<string, any>>(schema: Yup.AnyObjectSchema | Lazy<any, any>, options?: ValidateOptions<Yup.AnyObjectSchema>) => Resolver<TFieldValues, object>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=require("react-hook-form/dist/index.ie11");function r(){return(r=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}exports.yupResolver=function(t,n){return void 0===n&&(n={abortEarly:!1}),function(o,s,a){void 0===a&&(a=!1);try{return Promise.resolve(function(e,a){try{var c=(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(t.validate(o,r({},n,{context:s}))).then(function(e){return{values:e,errors:{}}}))}catch(e){return a(e)}return c&&c.then?c.then(void 0,a):c}(0,function(t){if(!t.inner)throw t;var n=function(e,t){var n;return Array.isArray(e.inner)&&e.inner.length?e.inner.reduce(function(e,n){var o,s,a=n.path,c=n.message,i=n.type,u=e[a]&&e[a].types||{},p=a||i;return r({},e,p?((s={})[p]=r({},e[p]||{message:c,type:i},t?{types:r({},u,(o={},o[i]=u[i]?[].concat([].concat(u[i]),[c]):c,o))}:{}),s):{})},{}):((n={})[e.path]={message:e.message,type:e.type},n)}(t,a);return{values:{},errors:e.transformToNestObject(n)}}))}catch(e){return Promise.reject(e)}}};
|
|
2
|
+
//# sourceMappingURL=yup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"yup.js","sources":["../src/yup.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/ban-ts-comment */\nimport { Resolver, transformToNestObject, FieldValues } from 'react-hook-form';\nimport Yup from 'yup';\nimport Lazy from 'yup/lib/Lazy';\n\n/**\n * From 0.32.0, Yup add TypeScript support and `path` typing is optional that's why we have `@ts-expect-error`\n * FYI: `path`: a string, indicating where there error was thrown. `path` is empty at the root level.\n * react-hook-form's values are object so path is defined\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 Array.isArray(error.inner) && error.inner.length\n ? error.inner.reduce(\n (previous: Record<string, any>, { path, message, type }) => {\n // @ts-expect-error\n const previousTypes = (previous[path] && previous[path].types) || {};\n const key = path || type;\n\n return {\n ...previous,\n ...(key\n ? {\n [key]: {\n ...(previous[key] || {\n message,\n type,\n }),\n ...(validateAllFieldCriteria\n ? {\n types: {\n ...previousTypes,\n // @ts-expect-error\n [type]: previousTypes[type]\n ? // @ts-expect-error\n [...[].concat(previousTypes[type]), message]\n : message,\n },\n }\n : {}),\n },\n }\n : {}),\n };\n },\n {},\n )\n : {\n // @ts-expect-error\n [error.path]: { message: error.message, type: error.type },\n };\n};\n\ntype ValidateOptions<T extends Yup.AnyObjectSchema> = Parameters<\n T['validate']\n>[1];\n\nexport const yupResolver = <TFieldValues extends FieldValues>(\n schema: Yup.AnyObjectSchema | Lazy<any, any>,\n options: ValidateOptions<Yup.AnyObjectSchema> = {\n abortEarly: false,\n },\n): Resolver<TFieldValues> => async (\n values,\n context,\n validateAllFieldCriteria = false,\n) => {\n try {\n if (options.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 return {\n values: await schema.validate(values, {\n ...options,\n context,\n }),\n errors: {},\n };\n } catch (e: any) {\n if (!e.inner) {\n throw e;\n }\n\n const parsedErrors = parseErrorSchema(e, validateAllFieldCriteria);\n return {\n values: {},\n errors: transformToNestObject(parsedErrors),\n };\n }\n};\n"],"names":["schema","options","abortEarly","values","context","validateAllFieldCriteria","process","env","NODE_ENV","console","warn","validate","errors","e","inner","parsedErrors","error","Array","isArray","length","reduce","previous","path","message","type","previousTypes","types","key","concat","parseErrorSchema","transformToNestObject"],"mappings":"oRA4D2B,SACzBA,EACAC,mBAAAA,IAAAA,EAAgD,CAC9CC,YAAY,aAGdC,EACAC,EACAC,YAAAA,IAAAA,GAA2B,uDAGrBJ,EAAQG,SAAoC,gBAAzBE,QAAQC,IAAIC,UAEjCC,QAAQC,KACN,iHAIYV,EAAOW,SAASR,OACzBF,GACHG,QAAAA,uBAHJ,MAAO,CACLD,SAIAS,OAAQ,4EAEHC,GACP,IAAKA,EAAEC,MACL,MAAMD,EAGR,IAAME,EA9Ee,SACvBC,EACAX,SAEA,OAAOY,MAAMC,QAAQF,EAAMF,QAAUE,EAAMF,MAAMK,OAC7CH,EAAMF,MAAMM,OACV,SAACC,aAAiCC,IAAAA,KAAMC,IAAAA,QAASC,IAAAA,KAEzCC,EAAiBJ,EAASC,IAASD,EAASC,GAAMI,OAAU,GAC5DC,EAAML,GAAQE,EAEpB,YACKH,EACCM,UAEGA,QACKN,EAASM,IAAQ,CACnBJ,QAAAA,EACAC,KAAAA,GAEEnB,EACA,CACEqB,WACKD,UAEFD,GAAOC,EAAcD,aAEd,GAAGI,OAAOH,EAAcD,KAAQD,IACpCA,OAGR,OAGR,KAGR,YAICP,EAAMM,MAAO,CAAEC,QAASP,EAAMO,QAASC,KAAMR,EAAMQ,SAqCnCK,CAAiBhB,EAAGR,GACzC,MAAO,CACLF,OAAQ,GACRS,OAAQkB,wBAAsBf,OAhCT"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './zod';
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("react-hook-form/dist/index.ie11"),e=require("@hookform/resolvers");function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function o(r,e){var t;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=r[Symbol.iterator]()).next.bind(t)}var a=function(n,a){if(n.isEmpty)return{};for(var i,s=[].concat(n.errors),u={},c=o(s);!(i=c()).done;){var l,f,v,p=i.value,y=p.path,m=p.message,d=p.code,h=e.convertArrayToPathName(y);if("unionErrors"in p)for(var b,g=o(p.unionErrors.map(function(r){return r.errors}));!(b=g()).done;)s.push.apply(s,b.value);u=t({},u,y?u[h]&&a?((l={})[h]=r.appendErrors(h,a,u,d,m),l):((v={})[h]=u[h]||t({message:m,type:d},a?{types:(f={},f[d]=m||!0,f)}:{}),v):{})}return u};exports.zodResolver=function(e,t){return function(n,o,i){void 0===i&&(i=!1);try{var s=e.safeParse(n,t);return Promise.resolve(s.success?{values:s.data,errors:{}}:{values:{},errors:r.transformToNestObject(a(s.error,i))})}catch(r){return Promise.reject(r)}}};
|
|
2
|
+
//# sourceMappingURL=zod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import {\n appendErrors,\n Resolver,\n ResolverError,\n ResolverSuccess,\n transformToNestObject,\n} from 'react-hook-form';\nimport * as z from 'zod';\nimport { ParseParams } from 'zod/lib/src/parser';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n zodError: z.ZodError,\n validateAllFieldCriteria: boolean,\n) => {\n if (zodError.isEmpty) {\n return {};\n }\n\n const errors = [...zodError.errors];\n let previous: Record<string, any> = {};\n\n for (const error of errors) {\n const { path, message, code: type } = error;\n const currentPath = convertArrayToPathName(path);\n\n if ('unionErrors' in error) {\n for (const subErrors of error.unionErrors.map((e) => e.errors)) {\n errors.push(...subErrors);\n }\n }\n\n previous = {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n }\n\n return previous;\n};\n\nexport const zodResolver = <T extends z.ZodSchema<any, any>>(\n schema: T,\n options?: ParseParams,\n): Resolver<z.infer<T>> => async (\n values,\n _,\n validateAllFieldCriteria = false,\n) => {\n const result = schema.safeParse(values, options);\n\n if (result.success) {\n return { values: result.data, errors: {} } as ResolverSuccess<z.infer<T>>;\n }\n\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(result.error, validateAllFieldCriteria),\n ),\n } as ResolverError<z.infer<T>>;\n};\n"],"names":["parseErrorSchema","zodError","validateAllFieldCriteria","isEmpty","errors","previous","error","path","message","type","code","currentPath","convertArrayToPathName","unionErrors","map","e","push","appendErrors","types","schema","options","values","_","result","safeParse","success","data","transformToNestObject"],"mappings":"slCAWA,IAAMA,EAAmB,SACvBC,EACAC,GAEA,GAAID,EAASE,QACX,MAAO,GAMT,IAHA,MAAMC,YAAaH,EAASG,QACxBC,EAAgC,OAEhBD,kBAAQ,WAAjBE,UACDC,EAA8BD,EAA9BC,KAAMC,EAAwBF,EAAxBE,QAAeC,EAASH,EAAfI,KACjBC,EAAcC,yBAAuBL,GAE3C,GAAI,gBAAiBD,EACnB,cAAwBA,EAAMO,YAAYC,IAAI,SAACC,UAAMA,EAAEX,yBACrDA,EAAOY,WAAPZ,WAIJC,OACKA,EACCE,EACAF,EAASM,IAAgBT,UAEpBS,GAAcM,eACbN,EACAT,EACAG,EACAI,EACAD,cAIDG,GAAcN,EAASM,OACtBH,QAAAA,EACAC,KAAAA,GACIP,EACA,CACEgB,cAAUT,GAAOD,IAAW,MAE9B,OAGV,IAIR,OAAOH,uBAGkB,SACzBc,EACAC,mBAEAC,EACAC,EACApB,YAAAA,IAAAA,GAA2B,OAE3B,IAAMqB,EAASJ,EAAOK,UAAUH,EAAQD,GAExC,uBAAIG,EAAOE,QACF,CAAEJ,OAAQE,EAAOG,KAAMtB,OAAQ,IAGjC,CACLiB,OAAQ,GACRjB,OAAQuB,wBACN3B,EAAiBuB,EAAOjB,MAAOJ,MAjBV"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './convertArrayToPathName';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolvers.js","sources":["../src/convertArrayToPathName.ts"],"sourcesContent":["export const convertArrayToPathName = (paths: (string | number)[]): string =>\n paths\n .reduce(\n (previous, path: string | number, index): string =>\n `${previous}${\n typeof path === 'string'\n ? `${index > 0 ? '.' : ''}${path}`\n : `[${path}]`\n }`,\n '',\n )\n .toString();\n"],"names":["paths","reduce","previous","path","index","toString"],"mappings":"+BAAsC,SAACA,UACrCA,EACGC,OACC,SAACC,EAAUC,EAAuBC,UAC7BF,GACe,iBAATC,GACAC,EAAQ,EAAI,IAAM,IAAKD,MACtBA,QAEZ,IAEDE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolvers.modern.js","sources":["../src/convertArrayToPathName.ts"],"sourcesContent":["export const convertArrayToPathName = (paths: (string | number)[]): string =>\n paths\n .reduce(\n (previous, path: string | number, index): string =>\n `${previous}${\n typeof path === 'string'\n ? `${index > 0 ? '.' : ''}${path}`\n : `[${path}]`\n }`,\n '',\n )\n .toString();\n"],"names":["convertArrayToPathName","paths","reduce","previous","path","index","toString"],"mappings":"AAAaA,MAAAA,EAA0BC,GACrCA,EACGC,OACC,CAACC,EAAUC,EAAuBC,OAC7BF,IACe,iBAATC,KACAC,EAAQ,EAAI,IAAM,KAAKD,QACtBA,OAEZ,IAEDE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolvers.module.js","sources":["../src/convertArrayToPathName.ts"],"sourcesContent":["export const convertArrayToPathName = (paths: (string | number)[]): string =>\n paths\n .reduce(\n (previous, path: string | number, index): string =>\n `${previous}${\n typeof path === 'string'\n ? `${index > 0 ? '.' : ''}${path}`\n : `[${path}]`\n }`,\n '',\n )\n .toString();\n"],"names":["convertArrayToPathName","paths","reduce","previous","path","index","toString"],"mappings":"AAAaA,IAAAA,EAAyB,SAACC,UACrCA,EACGC,OACC,SAACC,EAAUC,EAAuBC,UAC7BF,GACe,iBAATC,GACAC,EAAQ,EAAI,IAAM,IAAKD,MACtBA,QAEZ,IAEDE"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e||self).hookformResolvers={})}(this,function(e){e.convertArrayToPathName=function(e){return e.reduce(function(e,o,n){return e+("string"==typeof o?(n>0?".":"")+o:"["+o+"]")},"").toString()}});
|
|
2
|
+
//# sourceMappingURL=resolvers.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolvers.umd.js","sources":["../src/convertArrayToPathName.ts"],"sourcesContent":["export const convertArrayToPathName = (paths: (string | number)[]): string =>\n paths\n .reduce(\n (previous, path: string | number, index): string =>\n `${previous}${\n typeof path === 'string'\n ? `${index > 0 ? '.' : ''}${path}`\n : `[${path}]`\n }`,\n '',\n )\n .toString();\n"],"names":["paths","reduce","previous","path","index","toString"],"mappings":"oQAAsC,SAACA,UACrCA,EACGC,OACC,SAACC,EAAUC,EAAuBC,UAC7BF,GACe,iBAATC,GACAC,EAAQ,EAAI,IAAM,IAAKD,MACtBA,QAEZ,IAEDE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './joi';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Resolver } from 'react-hook-form';
|
|
2
|
-
import * as Joi from 'joi';
|
|
3
|
-
export declare const joiResolver: <TFieldValues extends Record<string, any>>(schema: Joi.Schema, options?: Joi.AsyncValidationOptions) => Resolver<TFieldValues, object>;
|
|
1
|
+
import { Resolver } from 'react-hook-form';
|
|
2
|
+
import * as Joi from 'joi';
|
|
3
|
+
export declare const joiResolver: <TFieldValues extends Record<string, any>>(schema: Joi.Schema, options?: Joi.AsyncValidationOptions) => Resolver<TFieldValues, object>;
|
package/joi/dist/joi.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("react-hook-form"),e=require("@hookform/resolvers");function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o])}return r}).apply(this,arguments)}var o=function(o,n){return Array.isArray(o.details)?o.details.reduce(function(o,a){var s,i,u,c=a.path,v=a.message,f=void 0===v?"":v,l=a.type,y=e.convertArrayToPathName(c);return t({},o,c?o[y]&&n?((s={})[y]=r.appendErrors(y,n,o,l,f),s):((u={})[y]=o[y]||t({message:f,type:l},n?{types:(i={},i[l]=f||!0,i)}:{}),u):{})},{}):[]};exports.joiResolver=function(e,n){return void 0===n&&(n={abortEarly:!1}),function(a,s,i){void 0===i&&(i=!1);try{return Promise.resolve(function(r,o){try{var i=Promise.resolve(e.validateAsync(a,t({},n,{context:s}))).then(function(r){return{values:r,errors:{}}})}catch(r){return o(r)}return i&&i.then?i.then(void 0,o):i}(0,function(e){return{values:{},errors:r.transformToNestObject(o(e,i))}}))}catch(r){return Promise.reject(r)}}};
|
|
2
|
+
//# sourceMappingURL=joi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"joi.js","sources":["../src/joi.ts"],"sourcesContent":["import {\n appendErrors,\n transformToNestObject,\n Resolver,\n FieldValues,\n} from 'react-hook-form';\nimport * as Joi from 'joi';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n error: Joi.ValidationError,\n validateAllFieldCriteria: boolean,\n) =>\n Array.isArray(error.details)\n ? error.details.reduce(\n (previous: Record<string, any>, { path, message = '', type }) => {\n const currentPath = convertArrayToPathName(path);\n\n return {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n },\n {},\n )\n : [];\n\nexport const joiResolver = <TFieldValues extends FieldValues>(\n schema: Joi.Schema,\n options: Joi.AsyncValidationOptions = {\n abortEarly: false,\n },\n): Resolver<TFieldValues> => async (\n values,\n context,\n validateAllFieldCriteria = false,\n) => {\n try {\n return {\n values: await schema.validateAsync(values, {\n ...options,\n context,\n }),\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(e, validateAllFieldCriteria),\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","error","validateAllFieldCriteria","Array","isArray","details","reduce","previous","path","message","type","currentPath","convertArrayToPathName","appendErrors","types","schema","options","abortEarly","values","context","validateAsync","errors","e","transformToNestObject"],"mappings":"iRASA,IAAMA,EAAmB,SACvBC,EACAC,UAEAC,MAAMC,QAAQH,EAAMI,SAChBJ,EAAMI,QAAQC,OACZ,SAACC,eAAiCC,IAAAA,SAAMC,QAAAA,aAAU,KAAIC,IAAAA,KAC9CC,EAAcC,yBAAuBJ,GAE3C,YACKD,EACCC,EACAD,EAASI,IAAgBT,UAEpBS,GAAcE,eACbF,EACAT,EACAK,EACAG,EACAD,cAIDE,GAAcJ,EAASI,OACtBF,QAAAA,EACAC,KAAAA,GACIR,EACA,CACEY,cAAUJ,GAAOD,IAAW,MAE9B,OAGV,KAGR,IAEF,wBAEqB,SACzBM,EACAC,mBAAAA,IAAAA,EAAsC,CACpCC,YAAY,aAGdC,EACAC,EACAjB,YAAAA,IAAAA,GAA2B,sEAITa,EAAOK,cAAcF,OAC9BF,GACHG,QAAAA,uBAHJ,MAAO,CACLD,SAIAG,OAAQ,2EAEHC,GACP,MAAO,CACLJ,OAAQ,GACRG,OAAQE,wBACNvB,EAAiBsB,EAAGpB,QAtBD"}
|
package/joi/dist/joi.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{transformToNestObject as r,appendErrors as e}from"react-hook-form";import{convertArrayToPathName as t}from"@hookform/resolvers";function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o])}return r}).apply(this,arguments)}var n=function(r,n){return Array.isArray(r.details)?r.details.reduce(function(r,a){var i,s,u,c=a.path,v=a.message,f=void 0===v?"":v,l=a.type,p=t(c);return o({},r,c?r[p]&&n?((i={})[p]=e(p,n,r,l,f),i):((u={})[p]=r[p]||o({message:f,type:l},n?{types:(s={},s[l]=f||!0,s)}:{}),u):{})},{}):[]},a=function(e,t){return void 0===t&&(t={abortEarly:!1}),function(a,i,s){void 0===s&&(s=!1);try{return Promise.resolve(function(r,n){try{var s=Promise.resolve(e.validateAsync(a,o({},t,{context:i}))).then(function(r){return{values:r,errors:{}}})}catch(r){return n(r)}return s&&s.then?s.then(void 0,n):s}(0,function(e){return{values:{},errors:r(n(e,s))}}))}catch(r){return Promise.reject(r)}}};export{a as joiResolver};
|
|
2
|
+
//# sourceMappingURL=joi.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{transformToNestObject as r,appendErrors as t}from"react-hook-form";import{convertArrayToPathName as e}from"@hookform/resolvers";function a(){return(a=Object.assign||function(r){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&(r[a]=e[a])}return r}).apply(this,arguments)}const o=(r,o)=>Array.isArray(r.details)?r.details.reduce((r,{path:s,message:n="",type:c})=>{const i=e(s);return a({},r,s?r[i]&&o?{[i]:t(i,o,r,c,n)}:{[i]:r[i]||a({message:n,type:c},o?{types:{[c]:n||!0}}:{})}:{})},{}):[],s=(t,e={abortEarly:!1})=>async(s,n,c=!1)=>{try{return{values:await t.validateAsync(s,a({},e,{context:n})),errors:{}}}catch(t){return{values:{},errors:r(o(t,c))}}};export{s as joiResolver};
|
|
2
|
+
//# sourceMappingURL=joi.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"joi.modern.js","sources":["../src/joi.ts"],"sourcesContent":["import {\n appendErrors,\n transformToNestObject,\n Resolver,\n FieldValues,\n} from 'react-hook-form';\nimport * as Joi from 'joi';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n error: Joi.ValidationError,\n validateAllFieldCriteria: boolean,\n) =>\n Array.isArray(error.details)\n ? error.details.reduce(\n (previous: Record<string, any>, { path, message = '', type }) => {\n const currentPath = convertArrayToPathName(path);\n\n return {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n },\n {},\n )\n : [];\n\nexport const joiResolver = <TFieldValues extends FieldValues>(\n schema: Joi.Schema,\n options: Joi.AsyncValidationOptions = {\n abortEarly: false,\n },\n): Resolver<TFieldValues> => async (\n values,\n context,\n validateAllFieldCriteria = false,\n) => {\n try {\n return {\n values: await schema.validateAsync(values, {\n ...options,\n context,\n }),\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(e, validateAllFieldCriteria),\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","error","validateAllFieldCriteria","Array","isArray","details","reduce","previous","path","message","type","currentPath","convertArrayToPathName","[object Object]","appendErrors","types","joiResolver","schema","options","abortEarly","async","values","context","validateAsync","errors","e","transformToNestObject"],"mappings":"sVASA,MAAMA,EAAmB,CACvBC,EACAC,IAEAC,MAAMC,QAAQH,EAAMI,SAChBJ,EAAMI,QAAQC,OACZ,CAACC,GAAiCC,KAAAA,EAAMC,QAAAA,EAAU,GAAIC,KAAAA,MACpD,MAAMC,EAAcC,EAAuBJ,GAE3C,YACKD,EACCC,EACAD,EAASI,IAAgBT,EACvB,CACEW,CAACF,GAAcG,EACbH,EACAT,EACAK,EACAG,EACAD,IAGJ,CACEI,CAACF,GAAcJ,EAASI,OACtBF,QAAAA,EACAC,KAAAA,GACIR,EACA,CACEa,MAAO,CAAEF,CAACH,GAAOD,IAAW,IAE9B,KAGV,KAGR,IAEF,GAEOO,EAAc,CACzBC,EACAC,EAAsC,CACpCC,YAAY,KAEaC,MAC3BC,EACAC,EACApB,GAA2B,KAE3B,IACE,MAAO,CACLmB,aAAcJ,EAAOM,cAAcF,OAC9BH,GACHI,QAAAA,KAEFE,OAAQ,IAEV,MAAOC,GACP,MAAO,CACLJ,OAAQ,GACRG,OAAQE,EACN1B,EAAiByB,EAAGvB"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{transformToNestObject as r,appendErrors as e}from"react-hook-form";import{convertArrayToPathName as t}from"@hookform/resolvers";function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o])}return r}).apply(this,arguments)}var n=function(r,n){return Array.isArray(r.details)?r.details.reduce(function(r,a){var i,s,u,c=a.path,v=a.message,f=void 0===v?"":v,l=a.type,p=t(c);return o({},r,c?r[p]&&n?((i={})[p]=e(p,n,r,l,f),i):((u={})[p]=r[p]||o({message:f,type:l},n?{types:(s={},s[l]=f||!0,s)}:{}),u):{})},{}):[]},a=function(e,t){return void 0===t&&(t={abortEarly:!1}),function(a,i,s){void 0===s&&(s=!1);try{return Promise.resolve(function(r,n){try{var s=Promise.resolve(e.validateAsync(a,o({},t,{context:i}))).then(function(r){return{values:r,errors:{}}})}catch(r){return n(r)}return s&&s.then?s.then(void 0,n):s}(0,function(e){return{values:{},errors:r(n(e,s))}}))}catch(r){return Promise.reject(r)}}};export{a as joiResolver};
|
|
2
|
+
//# sourceMappingURL=joi.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"joi.module.js","sources":["../src/joi.ts"],"sourcesContent":["import {\n appendErrors,\n transformToNestObject,\n Resolver,\n FieldValues,\n} from 'react-hook-form';\nimport * as Joi from 'joi';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n error: Joi.ValidationError,\n validateAllFieldCriteria: boolean,\n) =>\n Array.isArray(error.details)\n ? error.details.reduce(\n (previous: Record<string, any>, { path, message = '', type }) => {\n const currentPath = convertArrayToPathName(path);\n\n return {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n },\n {},\n )\n : [];\n\nexport const joiResolver = <TFieldValues extends FieldValues>(\n schema: Joi.Schema,\n options: Joi.AsyncValidationOptions = {\n abortEarly: false,\n },\n): Resolver<TFieldValues> => async (\n values,\n context,\n validateAllFieldCriteria = false,\n) => {\n try {\n return {\n values: await schema.validateAsync(values, {\n ...options,\n context,\n }),\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(e, validateAllFieldCriteria),\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","error","validateAllFieldCriteria","Array","isArray","details","reduce","previous","path","message","type","currentPath","convertArrayToPathName","appendErrors","types","joiResolver","schema","options","abortEarly","values","context","validateAsync","errors","e","transformToNestObject"],"mappings":"sVASA,IAAMA,EAAmB,SACvBC,EACAC,UAEAC,MAAMC,QAAQH,EAAMI,SAChBJ,EAAMI,QAAQC,OACZ,SAACC,eAAiCC,IAAAA,SAAMC,QAAAA,aAAU,KAAIC,IAAAA,KAC9CC,EAAcC,EAAuBJ,GAE3C,YACKD,EACCC,EACAD,EAASI,IAAgBT,UAEpBS,GAAcE,EACbF,EACAT,EACAK,EACAG,EACAD,cAIDE,GAAcJ,EAASI,OACtBF,QAAAA,EACAC,KAAAA,GACIR,EACA,CACEY,cAAUJ,GAAOD,IAAW,MAE9B,OAGV,KAGR,IAEF,IAEOM,EAAc,SACzBC,EACAC,mBAAAA,IAAAA,EAAsC,CACpCC,YAAY,aAGdC,EACAC,EACAlB,YAAAA,IAAAA,GAA2B,sEAITc,EAAOK,cAAcF,OAC9BF,GACHG,QAAAA,uBAHJ,MAAO,CACLD,SAIAG,OAAQ,2EAEHC,GACP,MAAO,CACLJ,OAAQ,GACRG,OAAQE,EACNxB,EAAiBuB,EAAGrB,QAtBD"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react-hook-form"),require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","react-hook-form","@hookform/resolvers"],r):r((e||self).hookformResolversJoi={},e.reactHookForm,e.hookformResolvers)}(this,function(e,r,o){function t(){return(t=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var o=arguments[r];for(var t in o)Object.prototype.hasOwnProperty.call(o,t)&&(e[t]=o[t])}return e}).apply(this,arguments)}var n=function(e,n){return Array.isArray(e.details)?e.details.reduce(function(e,s){var i,a,f,u=s.path,c=s.message,l=void 0===c?"":c,v=s.type,h=o.convertArrayToPathName(u);return t({},e,u?e[h]&&n?((i={})[h]=r.appendErrors(h,n,e,v,l),i):((f={})[h]=e[h]||t({message:l,type:v},n?{types:(a={},a[v]=l||!0,a)}:{}),f):{})},{}):[]};e.joiResolver=function(e,o){return void 0===o&&(o={abortEarly:!1}),function(s,i,a){void 0===a&&(a=!1);try{return Promise.resolve(function(r,n){try{var a=Promise.resolve(e.validateAsync(s,t({},o,{context:i}))).then(function(e){return{values:e,errors:{}}})}catch(e){return n(e)}return a&&a.then?a.then(void 0,n):a}(0,function(e){return{values:{},errors:r.transformToNestObject(n(e,a))}}))}catch(e){return Promise.reject(e)}}}});
|
|
2
|
+
//# sourceMappingURL=joi.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"joi.umd.js","sources":["../src/joi.ts"],"sourcesContent":["import {\n appendErrors,\n transformToNestObject,\n Resolver,\n FieldValues,\n} from 'react-hook-form';\nimport * as Joi from 'joi';\nimport { convertArrayToPathName } from '@hookform/resolvers';\n\nconst parseErrorSchema = (\n error: Joi.ValidationError,\n validateAllFieldCriteria: boolean,\n) =>\n Array.isArray(error.details)\n ? error.details.reduce(\n (previous: Record<string, any>, { path, message = '', type }) => {\n const currentPath = convertArrayToPathName(path);\n\n return {\n ...previous,\n ...(path\n ? previous[currentPath] && validateAllFieldCriteria\n ? {\n [currentPath]: appendErrors(\n currentPath,\n validateAllFieldCriteria,\n previous,\n type,\n message,\n ),\n }\n : {\n [currentPath]: previous[currentPath] || {\n message,\n type,\n ...(validateAllFieldCriteria\n ? {\n types: { [type]: message || true },\n }\n : {}),\n },\n }\n : {}),\n };\n },\n {},\n )\n : [];\n\nexport const joiResolver = <TFieldValues extends FieldValues>(\n schema: Joi.Schema,\n options: Joi.AsyncValidationOptions = {\n abortEarly: false,\n },\n): Resolver<TFieldValues> => async (\n values,\n context,\n validateAllFieldCriteria = false,\n) => {\n try {\n return {\n values: await schema.validateAsync(values, {\n ...options,\n context,\n }),\n errors: {},\n };\n } catch (e) {\n return {\n values: {},\n errors: transformToNestObject(\n parseErrorSchema(e, validateAllFieldCriteria),\n ),\n };\n }\n};\n"],"names":["parseErrorSchema","error","validateAllFieldCriteria","Array","isArray","details","reduce","previous","path","message","type","currentPath","convertArrayToPathName","appendErrors","types","schema","options","abortEarly","values","context","validateAsync","errors","e","transformToNestObject"],"mappings":"ukBASA,IAAMA,EAAmB,SACvBC,EACAC,UAEAC,MAAMC,QAAQH,EAAMI,SAChBJ,EAAMI,QAAQC,OACZ,SAACC,eAAiCC,IAAAA,SAAMC,QAAAA,aAAU,KAAIC,IAAAA,KAC9CC,EAAcC,yBAAuBJ,GAE3C,YACKD,EACCC,EACAD,EAASI,IAAgBT,UAEpBS,GAAcE,eACbF,EACAT,EACAK,EACAG,EACAD,cAIDE,GAAcJ,EAASI,OACtBF,QAAAA,EACAC,KAAAA,GACIR,EACA,CACEY,cAAUJ,GAAOD,IAAW,MAE9B,OAGV,KAGR,IAEF,kBAEqB,SACzBM,EACAC,mBAAAA,IAAAA,EAAsC,CACpCC,YAAY,aAGdC,EACAC,EACAjB,YAAAA,IAAAA,GAA2B,sEAITa,EAAOK,cAAcF,OAC9BF,GACHG,QAAAA,uBAHJ,MAAO,CACLD,SAIAG,OAAQ,2EAEHC,GACP,MAAO,CACLJ,OAAQ,GACRG,OAAQE,wBACNvB,EAAiBsB,EAAGpB,QAtBD"}
|
package/joi/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "joi",
|
|
3
|
+
"amdName": "hookformResolversJoi",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": true,
|
|
6
|
+
"description": "React Hook Form validation resolver: joi",
|
|
7
|
+
"main": "dist/joi.js",
|
|
8
|
+
"module": "dist/joi.module.js",
|
|
9
|
+
"umd:main": "dist/joi.umd.js",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react-hook-form": ">=6.6.0",
|
|
15
|
+
"@hookform/resolvers": "^1.0.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`joiResolver should return errors 1`] = `
|
|
4
|
+
Object {
|
|
5
|
+
"errors": Object {
|
|
6
|
+
"username": Object {
|
|
7
|
+
"message": "\\"username\\" is required",
|
|
8
|
+
"type": "any.required",
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
"values": Object {},
|
|
12
|
+
}
|
|
13
|
+
`;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as Joi from 'joi';
|
|
2
|
+
import { joiResolver } from '..';
|
|
3
|
+
|
|
4
|
+
const schema = Joi.object({
|
|
5
|
+
username: Joi.string().alphanum().min(3).max(30).required(),
|
|
6
|
+
|
|
7
|
+
password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
|
|
8
|
+
|
|
9
|
+
repeatPassword: Joi.ref('password'),
|
|
10
|
+
|
|
11
|
+
accessToken: [Joi.string(), Joi.number()],
|
|
12
|
+
|
|
13
|
+
birthYear: Joi.number().integer().min(1900).max(2013),
|
|
14
|
+
|
|
15
|
+
email: Joi.string().email({
|
|
16
|
+
minDomainSegments: 2,
|
|
17
|
+
tlds: { allow: ['com', 'net'] },
|
|
18
|
+
}),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('joiResolver', () => {
|
|
22
|
+
it('should return correct value', async () => {
|
|
23
|
+
const data = { username: 'abc', birthYear: 1994 };
|
|
24
|
+
expect(await joiResolver(schema)(data)).toEqual({
|
|
25
|
+
values: data,
|
|
26
|
+
errors: {},
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should return errors', async () => {
|
|
31
|
+
expect(await joiResolver(schema)({})).toMatchSnapshot();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should validate with context', async () => {
|
|
35
|
+
const schemaSpy = jest.spyOn(schema, 'validateAsync');
|
|
36
|
+
const context = { value: 'context' };
|
|
37
|
+
|
|
38
|
+
const data = { username: 'abc', birthYear: 1994 };
|
|
39
|
+
expect(await joiResolver(schema)(data, context)).toEqual({
|
|
40
|
+
values: data,
|
|
41
|
+
errors: {},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(schemaSpy).toHaveBeenCalledWith(data, {
|
|
45
|
+
abortEarly: false,
|
|
46
|
+
context,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
package/joi/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './joi';
|
package/joi/src/joi.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {
|
|
2
|
+
appendErrors,
|
|
3
|
+
transformToNestObject,
|
|
4
|
+
Resolver,
|
|
5
|
+
FieldValues,
|
|
6
|
+
} from 'react-hook-form';
|
|
7
|
+
import * as Joi from 'joi';
|
|
8
|
+
import { convertArrayToPathName } from '@hookform/resolvers';
|
|
9
|
+
|
|
10
|
+
const parseErrorSchema = (
|
|
11
|
+
error: Joi.ValidationError,
|
|
12
|
+
validateAllFieldCriteria: boolean,
|
|
13
|
+
) =>
|
|
14
|
+
Array.isArray(error.details)
|
|
15
|
+
? error.details.reduce(
|
|
16
|
+
(previous: Record<string, any>, { path, message = '', type }) => {
|
|
17
|
+
const currentPath = convertArrayToPathName(path);
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
...previous,
|
|
21
|
+
...(path
|
|
22
|
+
? previous[currentPath] && validateAllFieldCriteria
|
|
23
|
+
? {
|
|
24
|
+
[currentPath]: appendErrors(
|
|
25
|
+
currentPath,
|
|
26
|
+
validateAllFieldCriteria,
|
|
27
|
+
previous,
|
|
28
|
+
type,
|
|
29
|
+
message,
|
|
30
|
+
),
|
|
31
|
+
}
|
|
32
|
+
: {
|
|
33
|
+
[currentPath]: previous[currentPath] || {
|
|
34
|
+
message,
|
|
35
|
+
type,
|
|
36
|
+
...(validateAllFieldCriteria
|
|
37
|
+
? {
|
|
38
|
+
types: { [type]: message || true },
|
|
39
|
+
}
|
|
40
|
+
: {}),
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
: {}),
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
{},
|
|
47
|
+
)
|
|
48
|
+
: [];
|
|
49
|
+
|
|
50
|
+
export const joiResolver = <TFieldValues extends FieldValues>(
|
|
51
|
+
schema: Joi.Schema,
|
|
52
|
+
options: Joi.AsyncValidationOptions = {
|
|
53
|
+
abortEarly: false,
|
|
54
|
+
},
|
|
55
|
+
): Resolver<TFieldValues> => async (
|
|
56
|
+
values,
|
|
57
|
+
context,
|
|
58
|
+
validateAllFieldCriteria = false,
|
|
59
|
+
) => {
|
|
60
|
+
try {
|
|
61
|
+
return {
|
|
62
|
+
values: await schema.validateAsync(values, {
|
|
63
|
+
...options,
|
|
64
|
+
context,
|
|
65
|
+
}),
|
|
66
|
+
errors: {},
|
|
67
|
+
};
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return {
|
|
70
|
+
values: {},
|
|
71
|
+
errors: transformToNestObject(
|
|
72
|
+
parseErrorSchema(e, validateAllFieldCriteria),
|
|
73
|
+
),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|