@hookform/resolvers 5.2.1 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/ata-validator/dist/ata-validator.d.ts +2 -0
- package/ata-validator/dist/ata-validator.js +2 -0
- package/ata-validator/dist/ata-validator.js.map +1 -0
- package/ata-validator/dist/ata-validator.mjs +2 -0
- package/ata-validator/dist/ata-validator.modern.mjs +2 -0
- package/ata-validator/dist/ata-validator.modern.mjs.map +1 -0
- package/ata-validator/dist/ata-validator.module.js +2 -0
- package/ata-validator/dist/ata-validator.module.js.map +1 -0
- package/ata-validator/dist/ata-validator.umd.js +2 -0
- package/ata-validator/dist/ata-validator.umd.js.map +1 -0
- package/ata-validator/dist/index.d.ts +2 -0
- package/ata-validator/dist/types.d.ts +6 -0
- package/ata-validator/package.json +18 -0
- package/ata-validator/src/__tests__/Form-native-validation.tsx +80 -0
- package/ata-validator/src/__tests__/Form.tsx +61 -0
- package/ata-validator/src/__tests__/__fixtures__/data.ts +134 -0
- package/ata-validator/src/__tests__/__snapshots__/ata-validator.ts.snap +262 -0
- package/ata-validator/src/__tests__/ata-validator.ts +113 -0
- package/ata-validator/src/ata-validator.ts +76 -0
- package/ata-validator/src/index.ts +2 -0
- package/ata-validator/src/types.ts +16 -0
- package/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.mjs.map +1 -1
- package/dist/resolvers.module.js +1 -1
- package/dist/resolvers.module.js.map +1 -1
- package/dist/resolvers.umd.js +1 -1
- package/dist/resolvers.umd.js.map +1 -1
- package/package.json +16 -6
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/src/valibot.ts +1 -3
- package/vine/dist/vine.js.map +1 -1
- package/vine/dist/vine.modern.mjs.map +1 -1
- package/vine/dist/vine.module.js.map +1 -1
- package/vine/dist/vine.umd.js.map +1 -1
- package/vine/src/vine.ts +1 -2
- package/zod/dist/zod.d.ts +1 -1
- package/zod/dist/zod.js +1 -1
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.mjs +1 -1
- package/zod/dist/zod.modern.mjs +1 -1
- package/zod/dist/zod.modern.mjs.map +1 -1
- package/zod/dist/zod.module.js +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/src/zod.ts +6 -5
package/README.md
CHANGED
|
@@ -35,6 +35,7 @@ Install your preferred validation library alongside `@hookform/resolvers`.
|
|
|
35
35
|
| resolver | Infer values <br /> from schema | [criteriaMode](https://react-hook-form.com/docs/useform#criteriaMode) |
|
|
36
36
|
|---|---|---|
|
|
37
37
|
| AJV | ❌ | `firstError \| all` |
|
|
38
|
+
| ata-validator | ❌ | `firstError \| all` |
|
|
38
39
|
| Arktype | ✅ | `firstError` |
|
|
39
40
|
| class-validator | ✅ | `firstError \| all` |
|
|
40
41
|
| computed-types | ✅ | `firstError` |
|
|
@@ -109,6 +110,7 @@ useForm<z.input<typeof schema>, any, z.output<typeof schema>>({
|
|
|
109
110
|
- [computed-types](#computed-types)
|
|
110
111
|
- [typanion](#typanion)
|
|
111
112
|
- [Ajv](#ajv)
|
|
113
|
+
- [ata-validator](#ata-validator)
|
|
112
114
|
- [TypeBox](#typebox)
|
|
113
115
|
- [With `ValueCheck`](#with-valuecheck)
|
|
114
116
|
- [With `TypeCompiler`](#with-typecompiler)
|
|
@@ -147,6 +149,19 @@ Dead simple Object schema validation.
|
|
|
147
149
|
|
|
148
150
|
[](https://bundlephobia.com/result?p=yup)
|
|
149
151
|
|
|
152
|
+
> ⚠️ Pass context via `useForm({ context })`, not via `yupResolver`'s `schemaOptions`. `schemaOptions.context` is overridden by the form context, so use the `useForm` context object instead.
|
|
153
|
+
|
|
154
|
+
```typescript jsx
|
|
155
|
+
// Correct
|
|
156
|
+
useForm({
|
|
157
|
+
resolver: yupResolver(schema),
|
|
158
|
+
context: { foo: true },
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Avoid - schemaOptions.context will be ignored/overridden
|
|
162
|
+
yupResolver(schema, { context: { foo: true } });
|
|
163
|
+
```
|
|
164
|
+
|
|
150
165
|
```typescript jsx
|
|
151
166
|
import { useForm } from 'react-hook-form';
|
|
152
167
|
import { yupResolver } from '@hookform/resolvers/yup';
|
|
@@ -910,6 +925,52 @@ const App = () => {
|
|
|
910
925
|
};
|
|
911
926
|
```
|
|
912
927
|
|
|
928
|
+
### [ata-validator](https://github.com/nicholasgasior/ata-validator)
|
|
929
|
+
|
|
930
|
+
A JSON Schema-based validator for JavaScript and TypeScript
|
|
931
|
+
|
|
932
|
+
[](https://bundlephobia.com/result?p=ata-validator)
|
|
933
|
+
|
|
934
|
+
```typescript jsx
|
|
935
|
+
import { useForm } from 'react-hook-form';
|
|
936
|
+
import { ataResolver } from '@hookform/resolvers/ata-validator';
|
|
937
|
+
|
|
938
|
+
const schema = {
|
|
939
|
+
type: 'object',
|
|
940
|
+
properties: {
|
|
941
|
+
username: {
|
|
942
|
+
type: 'string',
|
|
943
|
+
minLength: 1,
|
|
944
|
+
},
|
|
945
|
+
password: {
|
|
946
|
+
type: 'string',
|
|
947
|
+
minLength: 1,
|
|
948
|
+
},
|
|
949
|
+
},
|
|
950
|
+
required: ['username', 'password'],
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
const App = () => {
|
|
954
|
+
const {
|
|
955
|
+
register,
|
|
956
|
+
handleSubmit,
|
|
957
|
+
formState: { errors },
|
|
958
|
+
} = useForm({
|
|
959
|
+
resolver: ataResolver(schema),
|
|
960
|
+
});
|
|
961
|
+
|
|
962
|
+
return (
|
|
963
|
+
<form onSubmit={handleSubmit((data) => console.log(data))}>
|
|
964
|
+
<input {...register('username')} />
|
|
965
|
+
{errors.username && <span>{errors.username.message}</span>}
|
|
966
|
+
<input {...register('password')} />
|
|
967
|
+
{errors.password && <span>{errors.password.message}</span>}
|
|
968
|
+
<button type="submit">submit</button>
|
|
969
|
+
</form>
|
|
970
|
+
);
|
|
971
|
+
};
|
|
972
|
+
```
|
|
973
|
+
|
|
913
974
|
## Backers
|
|
914
975
|
|
|
915
976
|
Thanks go to all our backers! [[Become a backer](https://opencollective.com/react-hook-form#backer)].
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=require("@hookform/resolvers"),e=require("ata-validator"),a=require("react-hook-form"),s=function(r,e){for(var s={},t=0;t<r.length;t+=1){var o=r[t],i=("required"===o.keyword?o.instancePath+"/"+o.params.missingProperty:o.instancePath).substring(1).replace(/\//g,".");if(s[i]||(s[i]={message:o.message,type:o.keyword}),e){var n=s[i].types,l=n&&n[o.keyword];s[i]=a.appendErrors(i,e,s,o.keyword,l?[].concat(l,o.message||""):o.message)}}return s};exports.ataResolver=function(a,t,o){void 0===o&&(o={});var i=new e.Validator(a,t);return function(e,a,t){try{var n=i.validate(e);return t.shouldUseNativeValidation&&r.validateFieldsNatively({},t),Promise.resolve(n.valid?{values:o.raw?Object.assign({},e):e,errors:{}}:{values:{},errors:r.toNestErrors(s(n.errors,!t.shouldUseNativeValidation&&"all"===t.criteriaMode),t)})}catch(r){return Promise.reject(r)}}};
|
|
2
|
+
//# sourceMappingURL=ata-validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ata-validator.js","sources":["../src/ata-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ValidationError, Validator } from 'ata-validator';\nimport { FieldError, appendErrors } from 'react-hook-form';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (\n ataErrors: ValidationError[],\n validateAllFieldCriteria: boolean,\n) => {\n const parsedErrors: Record<string, FieldError> = {};\n\n for (let index = 0; index < ataErrors.length; index += 1) {\n const error = ataErrors[index];\n\n const instancePath =\n error.keyword === 'required'\n ? `${error.instancePath}/${error.params.missingProperty}`\n : error.instancePath;\n\n const path = instancePath.substring(1).replace(/\\//g, '.');\n\n if (!parsedErrors[path]) {\n parsedErrors[path] = {\n message: error.message,\n type: error.keyword,\n };\n }\n\n if (validateAllFieldCriteria) {\n const types = parsedErrors[path].types;\n const messages = types && types[error.keyword];\n\n parsedErrors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n parsedErrors,\n error.keyword,\n messages\n ? ([] as string[]).concat(messages as string[], error.message || '')\n : error.message,\n ) as FieldError;\n }\n }\n\n return parsedErrors;\n};\n\nexport const ataResolver: Resolver = (\n schema,\n schemaOptions,\n resolverOptions = {},\n) => {\n const validator = new Validator(schema, schemaOptions);\n return async (values, _, options) => {\n const result = validator.validate(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return result.valid\n ? {\n values: resolverOptions.raw ? Object.assign({}, values) : values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n};\n"],"names":["parseErrorSchema","ataErrors","validateAllFieldCriteria","parsedErrors","index","length","error","path","keyword","instancePath","params","missingProperty","substring","replace","message","type","types","messages","appendErrors","concat","schema","schemaOptions","resolverOptions","validator","Validator","values","_","options","result","validate","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","valid","raw","Object","assign","errors","toNestErrors","criteriaMode","e","reject"],"mappings":"6FAKMA,EAAmB,SACvBC,EACAC,GAIA,IAFA,IAAMC,EAA2C,CAAE,EAE1CC,EAAQ,EAAGA,EAAQH,EAAUI,OAAQD,GAAS,EAAG,CACxD,IAAME,EAAQL,EAAUG,GAOlBG,GAJc,aAAlBD,EAAME,QACCF,EAAMG,iBAAgBH,EAAMI,OAAOC,gBACtCL,EAAMG,cAEcG,UAAU,GAAGC,QAAQ,MAAO,KAStD,GAPKV,EAAaI,KAChBJ,EAAaI,GAAQ,CACnBO,QAASR,EAAMQ,QACfC,KAAMT,EAAME,UAIZN,EAA0B,CAC5B,IAAMc,EAAQb,EAAaI,GAAMS,MAC3BC,EAAWD,GAASA,EAAMV,EAAME,SAEtCL,EAAaI,GAAQW,EAAAA,aACnBX,EACAL,EACAC,EACAG,EAAME,QACNS,EACK,GAAgBE,OAAOF,EAAsBX,EAAMQ,SAAW,IAC/DR,EAAMQ,QAEd,CACF,CAEA,OAAOX,CACT,sBAEqC,SACnCiB,EACAC,EACAC,QAAe,IAAfA,IAAAA,EAAkB,CAAA,GAElB,IAAMC,EAAY,IAAIC,EAASA,UAACJ,EAAQC,GACxC,OAAA,SAAcI,EAAQC,EAAGC,GAAO,IAC9B,IAAMC,EAASL,EAAUM,SAASJ,GAIlC,OAFAE,EAAQG,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEJ,GAEhEK,QAAAC,QAAOL,EAAOM,MACV,CACET,OAAQH,EAAgBa,IAAMC,OAAOC,OAAO,CAAE,EAAEZ,GAAUA,EAC1Da,OAAQ,IAEV,CACEb,OAAQ,CAAE,EACVa,OAAQC,eACNvC,EACE4B,EAAOU,QACNX,EAAQG,2BACkB,QAAzBH,EAAQa,cAEZb,IAGV,CAAC,MAAAc,UAAAT,QAAAU,OAAAD,IACH"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";import{Validator as a}from"ata-validator";import{appendErrors as o}from"react-hook-form";var t=function(r,e){for(var a={},t=0;t<r.length;t+=1){var s=r[t],i=("required"===s.keyword?s.instancePath+"/"+s.params.missingProperty:s.instancePath).substring(1).replace(/\//g,".");if(a[i]||(a[i]={message:s.message,type:s.keyword}),e){var n=a[i].types,m=n&&n[s.keyword];a[i]=o(i,e,a,s.keyword,m?[].concat(m,s.message||""):s.message)}}return a},s=function(o,s,i){void 0===i&&(i={});var n=new a(o,s);return function(a,o,s){try{var m=n.validate(a);return s.shouldUseNativeValidation&&r({},s),Promise.resolve(m.valid?{values:i.raw?Object.assign({},a):a,errors:{}}:{values:{},errors:e(t(m.errors,!s.shouldUseNativeValidation&&"all"===s.criteriaMode),s)})}catch(r){return Promise.reject(r)}}};export{s as ataResolver};
|
|
2
|
+
//# sourceMappingURL=ata-validator.module.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{validateFieldsNatively as e,toNestErrors as r}from"@hookform/resolvers";import{Validator as o}from"ata-validator";import{appendErrors as s}from"react-hook-form";const t=(e,r)=>{const o={};for(let t=0;t<e.length;t+=1){const a=e[t],i=("required"===a.keyword?`${a.instancePath}/${a.params.missingProperty}`:a.instancePath).substring(1).replace(/\//g,".");if(o[i]||(o[i]={message:a.message,type:a.keyword}),r){const e=o[i].types,t=e&&e[a.keyword];o[i]=s(i,r,o,a.keyword,t?[].concat(t,a.message||""):a.message)}}return o},a=(s,a,i={})=>{const n=new o(s,a);return async(o,s,a)=>{const c=n.validate(o);return a.shouldUseNativeValidation&&e({},a),c.valid?{values:i.raw?Object.assign({},o):o,errors:{}}:{values:{},errors:r(t(c.errors,!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)}}};export{a as ataResolver};
|
|
2
|
+
//# sourceMappingURL=ata-validator.modern.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ata-validator.modern.mjs","sources":["../src/ata-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ValidationError, Validator } from 'ata-validator';\nimport { FieldError, appendErrors } from 'react-hook-form';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (\n ataErrors: ValidationError[],\n validateAllFieldCriteria: boolean,\n) => {\n const parsedErrors: Record<string, FieldError> = {};\n\n for (let index = 0; index < ataErrors.length; index += 1) {\n const error = ataErrors[index];\n\n const instancePath =\n error.keyword === 'required'\n ? `${error.instancePath}/${error.params.missingProperty}`\n : error.instancePath;\n\n const path = instancePath.substring(1).replace(/\\//g, '.');\n\n if (!parsedErrors[path]) {\n parsedErrors[path] = {\n message: error.message,\n type: error.keyword,\n };\n }\n\n if (validateAllFieldCriteria) {\n const types = parsedErrors[path].types;\n const messages = types && types[error.keyword];\n\n parsedErrors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n parsedErrors,\n error.keyword,\n messages\n ? ([] as string[]).concat(messages as string[], error.message || '')\n : error.message,\n ) as FieldError;\n }\n }\n\n return parsedErrors;\n};\n\nexport const ataResolver: Resolver = (\n schema,\n schemaOptions,\n resolverOptions = {},\n) => {\n const validator = new Validator(schema, schemaOptions);\n return async (values, _, options) => {\n const result = validator.validate(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return result.valid\n ? {\n values: resolverOptions.raw ? Object.assign({}, values) : values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n};\n"],"names":["parseErrorSchema","ataErrors","validateAllFieldCriteria","parsedErrors","index","length","error","path","keyword","instancePath","params","missingProperty","substring","replace","message","type","types","messages","appendErrors","concat","ataResolver","schema","schemaOptions","resolverOptions","validator","Validator","values","_","options","result","validate","shouldUseNativeValidation","validateFieldsNatively","valid","raw","Object","assign","errors","toNestErrors","criteriaMode"],"mappings":"wKAKA,MAAMA,EAAmBA,CACvBC,EACAC,KAEA,MAAMC,EAA2C,CAAE,EAEnD,IAAK,IAAIC,EAAQ,EAAGA,EAAQH,EAAUI,OAAQD,GAAS,EAAG,CACxD,MAAME,EAAQL,EAAUG,GAOlBG,GAJc,aAAlBD,EAAME,QACF,GAAGF,EAAMG,gBAAgBH,EAAMI,OAAOC,kBACtCL,EAAMG,cAEcG,UAAU,GAAGC,QAAQ,MAAO,KAStD,GAPKV,EAAaI,KAChBJ,EAAaI,GAAQ,CACnBO,QAASR,EAAMQ,QACfC,KAAMT,EAAME,UAIZN,EAA0B,CAC5B,MAAMc,EAAQb,EAAaI,GAAMS,MAC3BC,EAAWD,GAASA,EAAMV,EAAME,SAEtCL,EAAaI,GAAQW,EACnBX,EACAL,EACAC,EACAG,EAAME,QACNS,EACK,GAAgBE,OAAOF,EAAsBX,EAAMQ,SAAW,IAC/DR,EAAMQ,QAEd,CACF,CAEA,OAAOX,GAGIiB,EAAwBA,CACnCC,EACAC,EACAC,EAAkB,CAAE,KAEpB,MAAMC,EAAY,IAAIC,EAAUJ,EAAQC,GACxC,OAAcI,MAAAA,EAAQC,EAAGC,KACvB,MAAMC,EAASL,EAAUM,SAASJ,GAIlC,OAFAE,EAAQG,2BAA6BC,EAAuB,CAAE,EAAEJ,GAEzDC,EAAOI,MACV,CACEP,OAAQH,EAAgBW,IAAMC,OAAOC,OAAO,CAAE,EAAEV,GAAUA,EAC1DW,OAAQ,CACT,GACD,CACEX,OAAQ,CAAA,EACRW,OAAQC,EACNtC,EACE6B,EAAOQ,QACNT,EAAQG,2BACkB,QAAzBH,EAAQW,cAEZX,IAGV"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";import{Validator as a}from"ata-validator";import{appendErrors as o}from"react-hook-form";var t=function(r,e){for(var a={},t=0;t<r.length;t+=1){var s=r[t],i=("required"===s.keyword?s.instancePath+"/"+s.params.missingProperty:s.instancePath).substring(1).replace(/\//g,".");if(a[i]||(a[i]={message:s.message,type:s.keyword}),e){var n=a[i].types,m=n&&n[s.keyword];a[i]=o(i,e,a,s.keyword,m?[].concat(m,s.message||""):s.message)}}return a},s=function(o,s,i){void 0===i&&(i={});var n=new a(o,s);return function(a,o,s){try{var m=n.validate(a);return s.shouldUseNativeValidation&&r({},s),Promise.resolve(m.valid?{values:i.raw?Object.assign({},a):a,errors:{}}:{values:{},errors:e(t(m.errors,!s.shouldUseNativeValidation&&"all"===s.criteriaMode),s)})}catch(r){return Promise.reject(r)}}};export{s as ataResolver};
|
|
2
|
+
//# sourceMappingURL=ata-validator.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ata-validator.module.js","sources":["../src/ata-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ValidationError, Validator } from 'ata-validator';\nimport { FieldError, appendErrors } from 'react-hook-form';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (\n ataErrors: ValidationError[],\n validateAllFieldCriteria: boolean,\n) => {\n const parsedErrors: Record<string, FieldError> = {};\n\n for (let index = 0; index < ataErrors.length; index += 1) {\n const error = ataErrors[index];\n\n const instancePath =\n error.keyword === 'required'\n ? `${error.instancePath}/${error.params.missingProperty}`\n : error.instancePath;\n\n const path = instancePath.substring(1).replace(/\\//g, '.');\n\n if (!parsedErrors[path]) {\n parsedErrors[path] = {\n message: error.message,\n type: error.keyword,\n };\n }\n\n if (validateAllFieldCriteria) {\n const types = parsedErrors[path].types;\n const messages = types && types[error.keyword];\n\n parsedErrors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n parsedErrors,\n error.keyword,\n messages\n ? ([] as string[]).concat(messages as string[], error.message || '')\n : error.message,\n ) as FieldError;\n }\n }\n\n return parsedErrors;\n};\n\nexport const ataResolver: Resolver = (\n schema,\n schemaOptions,\n resolverOptions = {},\n) => {\n const validator = new Validator(schema, schemaOptions);\n return async (values, _, options) => {\n const result = validator.validate(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return result.valid\n ? {\n values: resolverOptions.raw ? Object.assign({}, values) : values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n};\n"],"names":["parseErrorSchema","ataErrors","validateAllFieldCriteria","parsedErrors","index","length","error","path","keyword","instancePath","params","missingProperty","substring","replace","message","type","types","messages","appendErrors","concat","ataResolver","schema","schemaOptions","resolverOptions","validator","Validator","values","_","options","result","validate","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","valid","raw","Object","assign","errors","toNestErrors","criteriaMode","e","reject"],"mappings":"wKAKA,IAAMA,EAAmB,SACvBC,EACAC,GAIA,IAFA,IAAMC,EAA2C,CAAE,EAE1CC,EAAQ,EAAGA,EAAQH,EAAUI,OAAQD,GAAS,EAAG,CACxD,IAAME,EAAQL,EAAUG,GAOlBG,GAJc,aAAlBD,EAAME,QACCF,EAAMG,iBAAgBH,EAAMI,OAAOC,gBACtCL,EAAMG,cAEcG,UAAU,GAAGC,QAAQ,MAAO,KAStD,GAPKV,EAAaI,KAChBJ,EAAaI,GAAQ,CACnBO,QAASR,EAAMQ,QACfC,KAAMT,EAAME,UAIZN,EAA0B,CAC5B,IAAMc,EAAQb,EAAaI,GAAMS,MAC3BC,EAAWD,GAASA,EAAMV,EAAME,SAEtCL,EAAaI,GAAQW,EACnBX,EACAL,EACAC,EACAG,EAAME,QACNS,EACK,GAAgBE,OAAOF,EAAsBX,EAAMQ,SAAW,IAC/DR,EAAMQ,QAEd,CACF,CAEA,OAAOX,CACT,EAEaiB,EAAwB,SACnCC,EACAC,EACAC,QAAe,IAAfA,IAAAA,EAAkB,CAAA,GAElB,IAAMC,EAAY,IAAIC,EAAUJ,EAAQC,GACxC,OAAA,SAAcI,EAAQC,EAAGC,GAAO,IAC9B,IAAMC,EAASL,EAAUM,SAASJ,GAIlC,OAFAE,EAAQG,2BAA6BC,EAAuB,CAAE,EAAEJ,GAEhEK,QAAAC,QAAOL,EAAOM,MACV,CACET,OAAQH,EAAgBa,IAAMC,OAAOC,OAAO,CAAE,EAAEZ,GAAUA,EAC1Da,OAAQ,IAEV,CACEb,OAAQ,CAAE,EACVa,OAAQC,EACNxC,EACE6B,EAAOU,QACNX,EAAQG,2BACkB,QAAzBH,EAAQa,cAEZb,IAGV,CAAC,MAAAc,UAAAT,QAAAU,OAAAD,IACH"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("ata-validator"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","ata-validator","react-hook-form"],r):r((e||self).hookformResolversAtaValidator={},e.hookformResolvers,e.ataValidator,e.ReactHookForm)}(this,function(e,r,o,a){var t=function(e,r){for(var o={},t=0;t<e.length;t+=1){var s=e[t],i=("required"===s.keyword?s.instancePath+"/"+s.params.missingProperty:s.instancePath).substring(1).replace(/\//g,".");if(o[i]||(o[i]={message:s.message,type:s.keyword}),r){var n=o[i].types,l=n&&n[s.keyword];o[i]=a.appendErrors(i,r,o,s.keyword,l?[].concat(l,s.message||""):s.message)}}return o};e.ataResolver=function(e,a,s){void 0===s&&(s={});var i=new o.Validator(e,a);return function(e,o,a){try{var n=i.validate(e);return a.shouldUseNativeValidation&&r.validateFieldsNatively({},a),Promise.resolve(n.valid?{values:s.raw?Object.assign({},e):e,errors:{}}:{values:{},errors:r.toNestErrors(t(n.errors,!a.shouldUseNativeValidation&&"all"===a.criteriaMode),a)})}catch(e){return Promise.reject(e)}}}});
|
|
2
|
+
//# sourceMappingURL=ata-validator.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ata-validator.umd.js","sources":["../src/ata-validator.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { ValidationError, Validator } from 'ata-validator';\nimport { FieldError, appendErrors } from 'react-hook-form';\nimport { Resolver } from './types';\n\nconst parseErrorSchema = (\n ataErrors: ValidationError[],\n validateAllFieldCriteria: boolean,\n) => {\n const parsedErrors: Record<string, FieldError> = {};\n\n for (let index = 0; index < ataErrors.length; index += 1) {\n const error = ataErrors[index];\n\n const instancePath =\n error.keyword === 'required'\n ? `${error.instancePath}/${error.params.missingProperty}`\n : error.instancePath;\n\n const path = instancePath.substring(1).replace(/\\//g, '.');\n\n if (!parsedErrors[path]) {\n parsedErrors[path] = {\n message: error.message,\n type: error.keyword,\n };\n }\n\n if (validateAllFieldCriteria) {\n const types = parsedErrors[path].types;\n const messages = types && types[error.keyword];\n\n parsedErrors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n parsedErrors,\n error.keyword,\n messages\n ? ([] as string[]).concat(messages as string[], error.message || '')\n : error.message,\n ) as FieldError;\n }\n }\n\n return parsedErrors;\n};\n\nexport const ataResolver: Resolver = (\n schema,\n schemaOptions,\n resolverOptions = {},\n) => {\n const validator = new Validator(schema, schemaOptions);\n return async (values, _, options) => {\n const result = validator.validate(values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return result.valid\n ? {\n values: resolverOptions.raw ? Object.assign({}, values) : values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.errors,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n};\n"],"names":["parseErrorSchema","ataErrors","validateAllFieldCriteria","parsedErrors","index","length","error","path","keyword","instancePath","params","missingProperty","substring","replace","message","type","types","messages","appendErrors","concat","schema","schemaOptions","resolverOptions","validator","Validator","values","_","options","result","validate","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","valid","raw","Object","assign","errors","toNestErrors","criteriaMode","e","reject"],"mappings":"2bAKA,IAAMA,EAAmB,SACvBC,EACAC,GAIA,IAFA,IAAMC,EAA2C,CAAE,EAE1CC,EAAQ,EAAGA,EAAQH,EAAUI,OAAQD,GAAS,EAAG,CACxD,IAAME,EAAQL,EAAUG,GAOlBG,GAJc,aAAlBD,EAAME,QACCF,EAAMG,iBAAgBH,EAAMI,OAAOC,gBACtCL,EAAMG,cAEcG,UAAU,GAAGC,QAAQ,MAAO,KAStD,GAPKV,EAAaI,KAChBJ,EAAaI,GAAQ,CACnBO,QAASR,EAAMQ,QACfC,KAAMT,EAAME,UAIZN,EAA0B,CAC5B,IAAMc,EAAQb,EAAaI,GAAMS,MAC3BC,EAAWD,GAASA,EAAMV,EAAME,SAEtCL,EAAaI,GAAQW,EAAAA,aACnBX,EACAL,EACAC,EACAG,EAAME,QACNS,EACK,GAAgBE,OAAOF,EAAsBX,EAAMQ,SAAW,IAC/DR,EAAMQ,QAEd,CACF,CAEA,OAAOX,CACT,gBAEqC,SACnCiB,EACAC,EACAC,QAAe,IAAfA,IAAAA,EAAkB,CAAA,GAElB,IAAMC,EAAY,IAAIC,EAASA,UAACJ,EAAQC,GACxC,OAAA,SAAcI,EAAQC,EAAGC,GAAO,IAC9B,IAAMC,EAASL,EAAUM,SAASJ,GAIlC,OAFAE,EAAQG,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEJ,GAEhEK,QAAAC,QAAOL,EAAOM,MACV,CACET,OAAQH,EAAgBa,IAAMC,OAAOC,OAAO,CAAE,EAAEZ,GAAUA,EAC1Da,OAAQ,IAEV,CACEb,OAAQ,CAAE,EACVa,OAAQC,eACNvC,EACE4B,EAAOU,QACNX,EAAQG,2BACkB,QAAzBH,EAAQa,cAEZb,IAGV,CAAC,MAAAc,UAAAT,QAAAU,OAAAD,IACH"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ValidationError, ValidatorOptions } from 'ata-validator';
|
|
2
|
+
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
3
|
+
export type Resolver = (schema: object, schemaOptions?: ValidatorOptions, resolverOptions?: {
|
|
4
|
+
raw?: boolean;
|
|
5
|
+
}) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
|
6
|
+
export type AtaValidationError = ValidationError;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hookform/resolvers/ata-validator",
|
|
3
|
+
"amdName": "hookformResolversAtaValidator",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": true,
|
|
6
|
+
"description": "React Hook Form validation resolver: ata-validator",
|
|
7
|
+
"main": "dist/ata-validator.js",
|
|
8
|
+
"module": "dist/ata-validator.module.js",
|
|
9
|
+
"umd:main": "dist/ata-validator.umd.js",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react-hook-form": "^7.55.0",
|
|
15
|
+
"@hookform/resolvers": "^2.0.0",
|
|
16
|
+
"ata-validator": "^0.7.0"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import user from '@testing-library/user-event';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { useForm } from 'react-hook-form';
|
|
5
|
+
import { ataResolver } from '..';
|
|
6
|
+
|
|
7
|
+
type FormData = { username: string; password: string };
|
|
8
|
+
|
|
9
|
+
const schema = {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
username: {
|
|
13
|
+
type: 'string',
|
|
14
|
+
minLength: 1,
|
|
15
|
+
},
|
|
16
|
+
password: {
|
|
17
|
+
type: 'string',
|
|
18
|
+
minLength: 1,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
required: ['username', 'password'],
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
interface Props {
|
|
26
|
+
onSubmit: (data: FormData) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function TestComponent({ onSubmit }: Props) {
|
|
30
|
+
const { register, handleSubmit } = useForm<FormData>({
|
|
31
|
+
resolver: ataResolver(schema),
|
|
32
|
+
shouldUseNativeValidation: true,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
37
|
+
<input {...register('username')} placeholder="username" />
|
|
38
|
+
|
|
39
|
+
<input {...register('password')} placeholder="password" />
|
|
40
|
+
|
|
41
|
+
<button type="submit">submit</button>
|
|
42
|
+
</form>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test("form's native validation with ata-validator", async () => {
|
|
47
|
+
const handleSubmit = vi.fn();
|
|
48
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
49
|
+
|
|
50
|
+
let usernameField = screen.getByPlaceholderText(
|
|
51
|
+
/username/i,
|
|
52
|
+
) as HTMLInputElement;
|
|
53
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
54
|
+
expect(usernameField.validationMessage).toBe('');
|
|
55
|
+
|
|
56
|
+
let passwordField = screen.getByPlaceholderText(
|
|
57
|
+
/password/i,
|
|
58
|
+
) as HTMLInputElement;
|
|
59
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
60
|
+
expect(passwordField.validationMessage).toBe('');
|
|
61
|
+
|
|
62
|
+
await user.click(screen.getByText(/submit/i));
|
|
63
|
+
|
|
64
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
65
|
+
expect(usernameField.validity.valid).toBe(false);
|
|
66
|
+
|
|
67
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
68
|
+
expect(passwordField.validity.valid).toBe(false);
|
|
69
|
+
|
|
70
|
+
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
71
|
+
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
72
|
+
|
|
73
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
74
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
75
|
+
expect(usernameField.validationMessage).toBe('');
|
|
76
|
+
|
|
77
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
78
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
79
|
+
expect(passwordField.validationMessage).toBe('');
|
|
80
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import user from '@testing-library/user-event';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { useForm } from 'react-hook-form';
|
|
5
|
+
import { ataResolver } from '..';
|
|
6
|
+
|
|
7
|
+
type FormData = { username: string; password: string };
|
|
8
|
+
|
|
9
|
+
const schema = {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
username: {
|
|
13
|
+
type: 'string',
|
|
14
|
+
minLength: 1,
|
|
15
|
+
},
|
|
16
|
+
password: {
|
|
17
|
+
type: 'string',
|
|
18
|
+
minLength: 1,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
required: ['username', 'password'],
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
interface Props {
|
|
26
|
+
onSubmit: (data: FormData) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function TestComponent({ onSubmit }: Props) {
|
|
30
|
+
const {
|
|
31
|
+
register,
|
|
32
|
+
formState: { errors },
|
|
33
|
+
handleSubmit,
|
|
34
|
+
} = useForm<FormData>({
|
|
35
|
+
resolver: ataResolver(schema),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
40
|
+
<input {...register('username')} />
|
|
41
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
42
|
+
|
|
43
|
+
<input {...register('password')} />
|
|
44
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
45
|
+
|
|
46
|
+
<button type="submit">submit</button>
|
|
47
|
+
</form>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
test("form's validation with ata-validator and TypeScript's integration", async () => {
|
|
52
|
+
const handleSubmit = vi.fn();
|
|
53
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
54
|
+
|
|
55
|
+
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
|
56
|
+
|
|
57
|
+
await user.click(screen.getByText(/submit/i));
|
|
58
|
+
|
|
59
|
+
expect(screen.queryAllByRole('alert').length).toBeGreaterThan(0);
|
|
60
|
+
expect(handleSubmit).not.toHaveBeenCalled();
|
|
61
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
|
|
3
|
+
interface Data {
|
|
4
|
+
username: string;
|
|
5
|
+
password: string;
|
|
6
|
+
email?: string;
|
|
7
|
+
birthday?: number;
|
|
8
|
+
tags: string[];
|
|
9
|
+
enabled: boolean;
|
|
10
|
+
url: string;
|
|
11
|
+
like?: { id: number; name: string }[];
|
|
12
|
+
deepObject: { data: string; twoLayersDeep: { name: string } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const schema = {
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
username: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
minLength: 3,
|
|
21
|
+
maxLength: 30,
|
|
22
|
+
pattern: '^\\w+$',
|
|
23
|
+
},
|
|
24
|
+
password: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
minLength: 8,
|
|
27
|
+
pattern: '.*[A-Z].*',
|
|
28
|
+
},
|
|
29
|
+
email: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
format: 'email',
|
|
32
|
+
},
|
|
33
|
+
birthday: {
|
|
34
|
+
type: 'integer',
|
|
35
|
+
minimum: 1900,
|
|
36
|
+
maximum: 2013,
|
|
37
|
+
},
|
|
38
|
+
tags: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
items: { type: 'string' },
|
|
41
|
+
},
|
|
42
|
+
enabled: {
|
|
43
|
+
type: 'boolean',
|
|
44
|
+
},
|
|
45
|
+
url: {
|
|
46
|
+
type: 'string',
|
|
47
|
+
format: 'uri',
|
|
48
|
+
},
|
|
49
|
+
like: {
|
|
50
|
+
type: 'array',
|
|
51
|
+
items: {
|
|
52
|
+
type: 'object',
|
|
53
|
+
properties: {
|
|
54
|
+
id: { type: 'number' },
|
|
55
|
+
name: { type: 'string', minLength: 4, maxLength: 4 },
|
|
56
|
+
},
|
|
57
|
+
required: ['id', 'name'],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
deepObject: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
data: { type: 'string' },
|
|
64
|
+
twoLayersDeep: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: { name: { type: 'string' } },
|
|
67
|
+
additionalProperties: false,
|
|
68
|
+
required: ['name'],
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
required: ['data', 'twoLayersDeep'],
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
required: ['username', 'password', 'tags', 'enabled', 'deepObject'],
|
|
75
|
+
additionalProperties: false,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const validData: Data = {
|
|
79
|
+
username: 'Doe',
|
|
80
|
+
password: 'Password123_',
|
|
81
|
+
email: 'john@doe.com',
|
|
82
|
+
birthday: 2000,
|
|
83
|
+
tags: ['tag1', 'tag2'],
|
|
84
|
+
enabled: true,
|
|
85
|
+
url: 'https://react-hook-form.com/',
|
|
86
|
+
like: [{ id: 1, name: 'name' }],
|
|
87
|
+
deepObject: {
|
|
88
|
+
data: 'data',
|
|
89
|
+
twoLayersDeep: { name: 'deeper' },
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const invalidData = {
|
|
94
|
+
username: '__',
|
|
95
|
+
password: 'invalid',
|
|
96
|
+
email: '',
|
|
97
|
+
birthday: 'birthYear',
|
|
98
|
+
like: [{ id: 'z' }],
|
|
99
|
+
url: 'abc',
|
|
100
|
+
deepObject: {
|
|
101
|
+
data: 233,
|
|
102
|
+
twoLayersDeep: { name: 123 },
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const invalidDataWithUndefined = {
|
|
107
|
+
username: 'jsun969',
|
|
108
|
+
password: undefined,
|
|
109
|
+
deepObject: {
|
|
110
|
+
twoLayersDeep: {
|
|
111
|
+
name: 'deeper',
|
|
112
|
+
},
|
|
113
|
+
data: undefined,
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
118
|
+
username: {
|
|
119
|
+
ref: { name: 'username' },
|
|
120
|
+
name: 'username',
|
|
121
|
+
},
|
|
122
|
+
password: {
|
|
123
|
+
ref: { name: 'password' },
|
|
124
|
+
name: 'password',
|
|
125
|
+
},
|
|
126
|
+
email: {
|
|
127
|
+
ref: { name: 'email' },
|
|
128
|
+
name: 'email',
|
|
129
|
+
},
|
|
130
|
+
birthday: {
|
|
131
|
+
ref: { name: 'birthday' },
|
|
132
|
+
name: 'birthday',
|
|
133
|
+
},
|
|
134
|
+
};
|