@hookform/resolvers 3.6.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -25
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/dist/types.d.ts +1 -1
- package/ajv/src/__tests__/ajv.ts +7 -1
- package/ajv/src/ajv.ts +1 -1
- package/ajv/src/types.ts +1 -5
- package/arktype/dist/arktype.js +1 -1
- package/arktype/dist/arktype.js.map +1 -1
- package/arktype/dist/arktype.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs +1 -1
- package/arktype/dist/arktype.modern.mjs.map +1 -1
- package/arktype/dist/arktype.module.js +1 -1
- package/arktype/dist/arktype.module.js.map +1 -1
- package/arktype/dist/arktype.umd.js +1 -1
- package/arktype/dist/arktype.umd.js.map +1 -1
- package/arktype/dist/types.d.ts +1 -1
- package/arktype/package.json +2 -1
- package/arktype/src/__tests__/Form-native-validation.tsx +5 -5
- package/arktype/src/__tests__/Form.tsx +2 -2
- package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
- package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +29 -29
- package/arktype/src/__tests__/arktype.ts +1 -1
- package/arktype/src/arktype.ts +2 -2
- package/arktype/src/types.ts +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.modern.mjs.map +1 -1
- package/class-validator/dist/class-validator.module.js.map +1 -1
- package/class-validator/dist/class-validator.umd.js.map +1 -1
- package/class-validator/dist/types.d.ts +2 -2
- package/class-validator/src/__tests__/Form-native-validation.tsx +2 -2
- package/class-validator/src/__tests__/Form.tsx +2 -2
- package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +2 -2
- package/class-validator/src/__tests__/class-validator.ts +4 -4
- package/class-validator/src/class-validator.ts +2 -2
- package/class-validator/src/types.ts +2 -2
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.modern.mjs.map +1 -1
- package/computed-types/dist/computed-types.module.js.map +1 -1
- package/computed-types/dist/computed-types.umd.js.map +1 -1
- package/computed-types/dist/types.d.ts +1 -1
- package/computed-types/src/__tests__/Form-native-validation.tsx +2 -2
- package/computed-types/src/__tests__/Form.tsx +2 -2
- package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
- package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +6 -6
- package/computed-types/src/__tests__/computed-types.ts +1 -1
- package/computed-types/src/computed-types.ts +2 -2
- package/computed-types/src/types.ts +1 -1
- 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/dist/toNestErrors.d.ts +1 -6
- package/dist/validateFieldsNatively.d.ts +1 -1
- package/effect-ts/dist/effect-ts.js +1 -1
- package/effect-ts/dist/effect-ts.js.map +1 -1
- package/effect-ts/dist/effect-ts.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
- package/effect-ts/dist/effect-ts.module.js +1 -1
- package/effect-ts/dist/effect-ts.module.js.map +1 -1
- package/effect-ts/dist/effect-ts.umd.js +1 -1
- package/effect-ts/dist/effect-ts.umd.js.map +1 -1
- package/effect-ts/src/__tests__/Form-native-validation.tsx +2 -2
- package/effect-ts/src/__tests__/Form.tsx +2 -2
- package/effect-ts/src/__tests__/__snapshots__/effect-ts.ts.snap +2 -2
- package/effect-ts/src/__tests__/effect-ts.ts +1 -1
- package/effect-ts/src/effect-ts.ts +1 -1
- package/io-ts/dist/errorsToRecord.d.ts +0 -1
- package/io-ts/dist/io-ts.js +1 -1
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs.map +1 -1
- package/io-ts/dist/io-ts.module.js +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/io-ts/src/__tests__/Form-native-validation.tsx +2 -2
- package/io-ts/src/__tests__/Form.tsx +2 -2
- package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +9 -9
- package/io-ts/src/__tests__/errorsToRecord.ts +29 -19
- package/io-ts/src/__tests__/io-ts.ts +1 -1
- package/io-ts/src/errorsToRecord.ts +19 -10
- package/io-ts/src/io-ts.ts +1 -1
- package/joi/dist/joi.js +1 -1
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/joi.mjs +1 -1
- package/joi/dist/joi.modern.mjs +1 -1
- package/joi/dist/joi.modern.mjs.map +1 -1
- package/joi/dist/joi.module.js +1 -1
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/dist/types.d.ts +1 -1
- package/joi/src/__tests__/Form-native-validation.tsx +2 -2
- package/joi/src/__tests__/Form.tsx +2 -2
- package/joi/src/__tests__/__snapshots__/joi.ts.snap +56 -56
- package/joi/src/__tests__/joi.ts +1 -1
- package/joi/src/joi.ts +1 -1
- package/joi/src/types.ts +1 -5
- package/nope/dist/nope.js.map +1 -1
- package/nope/dist/nope.modern.mjs.map +1 -1
- package/nope/dist/nope.module.js.map +1 -1
- package/nope/dist/nope.umd.js.map +1 -1
- package/nope/dist/types.d.ts +1 -1
- package/nope/src/__tests__/Form-native-validation.tsx +2 -2
- package/nope/src/__tests__/Form.tsx +2 -2
- package/nope/src/__tests__/__fixtures__/data.ts +1 -1
- package/nope/src/__tests__/nope.ts +1 -1
- package/nope/src/nope.ts +1 -1
- package/nope/src/types.ts +2 -2
- package/package.json +69 -54
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.modern.mjs.map +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/dist/types.d.ts +1 -1
- package/superstruct/src/__tests__/Form-native-validation.tsx +2 -2
- package/superstruct/src/__tests__/Form.tsx +2 -2
- package/superstruct/src/__tests__/__fixtures__/data.ts +8 -8
- package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +4 -4
- package/superstruct/src/__tests__/superstruct.ts +1 -1
- package/superstruct/src/superstruct.ts +1 -1
- package/superstruct/src/types.ts +1 -1
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -1
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/dist/types.d.ts +1 -1
- package/typanion/src/__tests__/Form-native-validation.tsx +1 -1
- package/typanion/src/__tests__/Form.tsx +1 -1
- package/typanion/src/__tests__/__fixtures__/data.ts +12 -7
- package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +4 -4
- package/typanion/src/__tests__/typanion.ts +1 -1
- package/typanion/src/typanion.ts +1 -1
- package/typanion/src/types.ts +4 -4
- package/typebox/dist/typebox.js +1 -1
- package/typebox/dist/typebox.js.map +1 -1
- package/typebox/dist/typebox.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs +1 -1
- package/typebox/dist/typebox.modern.mjs.map +1 -1
- package/typebox/dist/typebox.module.js +1 -1
- package/typebox/dist/typebox.module.js.map +1 -1
- package/typebox/dist/typebox.umd.js +1 -1
- package/typebox/dist/typebox.umd.js.map +1 -1
- package/typebox/dist/types.d.ts +3 -2
- package/typebox/src/__tests__/Form-compiler.tsx +57 -0
- package/typebox/src/__tests__/Form-native-validation-compiler.tsx +86 -0
- package/typebox/src/__tests__/Form-native-validation.tsx +4 -4
- package/typebox/src/__tests__/Form.tsx +3 -3
- package/typebox/src/__tests__/__fixtures__/data.ts +6 -2
- package/typebox/src/__tests__/__snapshots__/typebox-compiler.ts.snap +188 -0
- package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +13 -13
- package/typebox/src/__tests__/typebox-compiler.ts +37 -0
- package/typebox/src/__tests__/typebox.ts +1 -1
- package/typebox/src/typebox.ts +8 -3
- package/typebox/src/types.ts +3 -2
- package/typeschema/dist/index.d.ts +2 -0
- package/typeschema/dist/types.d.ts +9 -0
- package/typeschema/dist/typeschema.d.ts +2 -0
- package/typeschema/dist/typeschema.js +2 -0
- package/typeschema/dist/typeschema.js.map +1 -0
- package/typeschema/dist/typeschema.mjs +2 -0
- package/typeschema/dist/typeschema.modern.mjs +2 -0
- package/typeschema/dist/typeschema.modern.mjs.map +1 -0
- package/typeschema/dist/typeschema.module.js +2 -0
- package/typeschema/dist/typeschema.module.js.map +1 -0
- package/typeschema/dist/typeschema.umd.js +2 -0
- package/typeschema/dist/typeschema.umd.js.map +1 -0
- package/typeschema/package.json +18 -0
- package/typeschema/src/__tests__/Form-native-validation.tsx +82 -0
- package/typeschema/src/__tests__/Form.tsx +53 -0
- package/typeschema/src/__tests__/__fixtures__/data.ts +88 -0
- package/typeschema/src/__tests__/__snapshots__/typeschema.ts.snap +225 -0
- package/typeschema/src/__tests__/typeschema.ts +70 -0
- package/typeschema/src/index.ts +2 -0
- package/typeschema/src/types.ts +18 -0
- package/typeschema/src/typeschema.ts +70 -0
- package/valibot/dist/types.d.ts +1 -1
- package/valibot/dist/valibot.js +1 -1
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/package.json +1 -1
- package/valibot/src/__tests__/Form-native-validation.tsx +2 -2
- package/valibot/src/__tests__/Form.tsx +1 -1
- package/valibot/src/__tests__/valibot.ts +4 -4
- package/valibot/src/types.ts +1 -1
- package/valibot/src/valibot.ts +12 -6
- package/vest/dist/types.d.ts +2 -2
- package/vest/dist/vest.js.map +1 -1
- package/vest/dist/vest.modern.mjs.map +1 -1
- package/vest/dist/vest.module.js.map +1 -1
- package/vest/dist/vest.umd.js.map +1 -1
- package/vest/src/__tests__/Form-native-validation.tsx +1 -1
- package/vest/src/__tests__/Form.tsx +1 -1
- package/vest/src/__tests__/__snapshots__/vest.ts.snap +0 -6
- package/vest/src/__tests__/vest.ts +14 -8
- package/vest/src/types.ts +11 -4
- package/vest/src/vest.ts +1 -1
- package/vine/dist/index.d.ts +2 -0
- package/vine/dist/types.d.ts +6 -0
- package/vine/dist/vine.d.ts +2 -0
- package/vine/dist/vine.js +2 -0
- package/vine/dist/vine.js.map +1 -0
- package/vine/dist/vine.mjs +2 -0
- package/vine/dist/vine.modern.mjs +2 -0
- package/vine/dist/vine.modern.mjs.map +1 -0
- package/vine/dist/vine.module.js +2 -0
- package/vine/dist/vine.module.js.map +1 -0
- package/vine/dist/vine.umd.js +2 -0
- package/vine/dist/vine.umd.js.map +1 -0
- package/vine/package.json +18 -0
- package/vine/src/__tests__/Form-native-validation.tsx +85 -0
- package/vine/src/__tests__/Form.tsx +59 -0
- package/vine/src/__tests__/__fixtures__/data.ts +76 -0
- package/vine/src/__tests__/__snapshots__/vine.ts.snap +180 -0
- package/vine/src/__tests__/vine.ts +53 -0
- package/vine/src/index.ts +2 -0
- package/vine/src/types.ts +13 -0
- package/vine/src/vine.ts +68 -0
- package/yup/dist/yup.d.ts +1 -1
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.modern.mjs.map +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/src/__tests__/Form-native-validation.tsx +1 -1
- package/yup/src/__tests__/Form.tsx +1 -1
- package/yup/src/__tests__/__snapshots__/yup.ts.snap +13 -13
- package/yup/src/__tests__/yup.ts +9 -7
- package/yup/src/yup.ts +2 -2
- package/zod/dist/types.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/__tests__/Form-native-validation.tsx +2 -2
- package/zod/src/__tests__/Form.tsx +1 -1
- package/zod/src/__tests__/zod.ts +1 -1
- package/zod/src/types.ts +1 -1
- package/zod/src/zod.ts +4 -3
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as o}from"@hookform/resolvers";import{TypeCheck as e}from"@sinclair/typebox/compiler";import{Value as s}from"@sinclair/typebox/value";import{appendErrors as t}from"react-hook-form";const a=(r,o)=>{const e={};for(;r.length;){const s=r[0],{type:a,message:i,path:n}=s,l=n.substring(1).replace(/\//g,".");if(e[l]||(e[l]={message:i,type:""+a}),o){const r=e[l].types,i=r&&r[""+a];e[l]=t(l,o,e,""+a,i?[].concat(i,s.message):s.message)}r.shift()}return e},i=t=>async(i,n,l)=>{const m=Array.from(t instanceof e?t.Errors(i):s.Errors(t,i));return l.shouldUseNativeValidation&&r({},l),m.length?{values:{},errors:o(a(m,!l.shouldUseNativeValidation&&"all"===l.criteriaMode),l)}:{errors:{},values:i}};export{i as typeboxResolver};
|
|
2
2
|
//# sourceMappingURL=typebox.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typebox.modern.mjs","sources":["../src/typebox.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"typebox.modern.mjs","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n _errors: ValueError[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; _errors.length; ) {\n const error = _errors[0];\n const { type, message, path } = error;\n const _path = path.substring(1).replace(/\\//g, '.');\n\n if (!errors[_path]) {\n errors[_path] = { message, type: '' + type };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types['' + type];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '' + type,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n _errors.shift();\n }\n\n return errors;\n};\n\nexport const typeboxResolver: Resolver =\n (schema) => async (values, _, options) => {\n const errors = Array.from(\n schema instanceof TypeCheck\n ? schema.Errors(values)\n : Value.Errors(schema, values),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (!errors.length) {\n return {\n errors: {} as FieldErrors,\n values,\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n errors,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","_errors","validateAllFieldCriteria","errors","length","error","type","message","path","_path","substring","replace","types","messages","appendErrors","concat","shift","typeboxResolver","schema","async","values","_","options","Array","from","TypeCheck","Errors","Value","shouldUseNativeValidation","validateFieldsNatively","toNestErrors","criteriaMode"],"mappings":"qOAMA,MAAMA,EAAmBA,CACvBC,EACAC,KAEA,MAAMC,EAAqC,CAAE,EAC7C,KAAOF,EAAQG,QAAU,CACvB,MAAMC,EAAQJ,EAAQ,IAChBK,KAAEA,EAAIC,QAAEA,EAAOC,KAAEA,GAASH,EAC1BI,EAAQD,EAAKE,UAAU,GAAGC,QAAQ,MAAO,KAM/C,GAJKR,EAAOM,KACVN,EAAOM,GAAS,CAAEF,UAASD,KAAM,GAAKA,IAGpCJ,EAA0B,CAC5B,MAAMU,EAAQT,EAAOM,GAAOG,MACtBC,EAAWD,GAASA,EAAM,GAAKN,GAErCH,EAAOM,GAASK,EACdL,EACAP,EACAC,EACA,GAAKG,EACLO,EACK,GAAgBE,OAAOF,EAAsBR,EAAME,SACpDF,EAAME,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,GAGIc,EACVC,GAAWC,MAAOC,EAAQC,EAAGC,KAC5B,MAAMnB,EAASoB,MAAMC,KACnBN,aAAkBO,EACdP,EAAOQ,OAAON,GACdO,EAAMD,OAAOR,EAAQE,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAuB,CAAE,EAAEP,GAE3DnB,EAAOC,OAOL,CACLgB,OAAQ,CAAE,EACVjB,OAAQ2B,EACN9B,EACEG,GACCmB,EAAQM,2BAAsD,QAAzBN,EAAQS,cAEhDT,IAbK,CACLnB,OAAQ,CAAiB,EACzBiB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";import{TypeCheck as o}from"@sinclair/typebox/compiler";import{Value as t}from"@sinclair/typebox/value";import{appendErrors as s}from"react-hook-form";var a=function(r,e){for(var o={};r.length;){var t=r[0],a=t.type,i=t.message,n=t.path.substring(1).replace(/\//g,".");if(o[n]||(o[n]={message:i,type:""+a}),e){var m=o[n].types,l=m&&m[""+a];o[n]=s(n,e,o,""+a,l?[].concat(l,t.message):t.message)}r.shift()}return o},i=function(s){return function(i,n,m){try{var l=Array.from(s instanceof o?s.Errors(i):t.Errors(s,i));return m.shouldUseNativeValidation&&r({},m),Promise.resolve(l.length?{values:{},errors:e(a(l,!m.shouldUseNativeValidation&&"all"===m.criteriaMode),m)}:{errors:{},values:i})}catch(r){return Promise.reject(r)}}};export{i as typeboxResolver};
|
|
2
2
|
//# sourceMappingURL=typebox.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typebox.module.js","sources":["../src/typebox.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"typebox.module.js","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n _errors: ValueError[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; _errors.length; ) {\n const error = _errors[0];\n const { type, message, path } = error;\n const _path = path.substring(1).replace(/\\//g, '.');\n\n if (!errors[_path]) {\n errors[_path] = { message, type: '' + type };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types['' + type];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '' + type,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n _errors.shift();\n }\n\n return errors;\n};\n\nexport const typeboxResolver: Resolver =\n (schema) => async (values, _, options) => {\n const errors = Array.from(\n schema instanceof TypeCheck\n ? schema.Errors(values)\n : Value.Errors(schema, values),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (!errors.length) {\n return {\n errors: {} as FieldErrors,\n values,\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n errors,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","_errors","validateAllFieldCriteria","errors","length","error","type","message","_path","path","substring","replace","types","messages","appendErrors","concat","shift","typeboxResolver","schema","values","_","options","Array","from","TypeCheck","Errors","Value","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","toNestErrors","criteriaMode","e","reject"],"mappings":"qOAMA,IAAMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,GACpCF,EAAQG,QAAU,CACvB,IAAMC,EAAQJ,EAAQ,GACdK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,UAAU,GAAGC,QAAQ,MAAO,KAM/C,GAJKR,EAAOK,KACVL,EAAOK,GAAS,CAAED,QAAAA,EAASD,KAAM,GAAKA,IAGpCJ,EAA0B,CAC5B,IAAMU,EAAQT,EAAOK,GAAOI,MACtBC,EAAWD,GAASA,EAAM,GAAKN,GAErCH,EAAOK,GAASM,EACdN,EACAN,EACAC,EACA,GAAKG,EACLO,EACK,GAAgBE,OAAOF,EAAsBR,EAAME,SACpDF,EAAME,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,CACT,EAEac,EACX,SAACC,GAAkBC,OAAAA,SAAAA,EAAQC,EAAGC,GAAO,IACnC,IAAMlB,EAASmB,MAAMC,KACnBL,aAAkBM,EACdN,EAAOO,OAAON,GACdO,EAAMD,OAAOP,EAAQC,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAuB,GAAIP,GAShEQ,QAAAC,QAPK3B,EAAOC,OAOL,CACLe,OAAQ,CAAA,EACRhB,OAAQ4B,EACN/B,EACEG,GACCkB,EAAQM,2BAAsD,QAAzBN,EAAQW,cAEhDX,IAbK,CACLlB,OAAQ,CAAA,EACRgB,OAAAA,GAcN,CAAC,MAAAc,GAAA,OAAAJ,QAAAK,OAAAD,EAAA,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers"),require("@sinclair/typebox/compiler"),require("@sinclair/typebox/value"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","@sinclair/typebox/compiler","@sinclair/typebox/value","react-hook-form"],r):r((e||self).hookformResolversTypebox={},e.hookformResolvers,e.compiler,e.value,e.ReactHookForm)}(this,function(e,r,o,s,t){var i=function(e,r){for(var o={};e.length;){var s=e[0],i=s.type,a=s.message,l=s.path.substring(1).replace(/\//g,".");if(o[l]||(o[l]={message:a,type:""+i}),r){var n=o[l].types,f=n&&n[""+i];o[l]=t.appendErrors(l,r,o,""+i,f?[].concat(f,s.message):s.message)}e.shift()}return o};e.typeboxResolver=function(e){return function(t,a,l){try{var n=Array.from(e instanceof o.TypeCheck?e.Errors(t):s.Value.Errors(e,t));return l.shouldUseNativeValidation&&r.validateFieldsNatively({},l),Promise.resolve(n.length?{values:{},errors:r.toNestErrors(i(n,!l.shouldUseNativeValidation&&"all"===l.criteriaMode),l)}:{errors:{},values:t})}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=typebox.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typebox.umd.js","sources":["../src/typebox.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"typebox.umd.js","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n _errors: ValueError[],\n validateAllFieldCriteria: boolean,\n) => {\n const errors: Record<string, FieldError> = {};\n for (; _errors.length; ) {\n const error = _errors[0];\n const { type, message, path } = error;\n const _path = path.substring(1).replace(/\\//g, '.');\n\n if (!errors[_path]) {\n errors[_path] = { message, type: '' + type };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types['' + type];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '' + type,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n _errors.shift();\n }\n\n return errors;\n};\n\nexport const typeboxResolver: Resolver =\n (schema) => async (values, _, options) => {\n const errors = Array.from(\n schema instanceof TypeCheck\n ? schema.Errors(values)\n : Value.Errors(schema, values),\n );\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (!errors.length) {\n return {\n errors: {} as FieldErrors,\n values,\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n errors,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","_errors","validateAllFieldCriteria","errors","length","error","type","message","_path","path","substring","replace","types","messages","appendErrors","concat","shift","schema","values","_","options","Array","from","TypeCheck","Errors","Value","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","toNestErrors","criteriaMode","e","reject"],"mappings":"mhBAMA,IAAMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,GACpCF,EAAQG,QAAU,CACvB,IAAMC,EAAQJ,EAAQ,GACdK,EAAwBD,EAAxBC,KAAMC,EAAkBF,EAAlBE,QACRC,EAD0BH,EAATI,KACJC,UAAU,GAAGC,QAAQ,MAAO,KAM/C,GAJKR,EAAOK,KACVL,EAAOK,GAAS,CAAED,QAAAA,EAASD,KAAM,GAAKA,IAGpCJ,EAA0B,CAC5B,IAAMU,EAAQT,EAAOK,GAAOI,MACtBC,EAAWD,GAASA,EAAM,GAAKN,GAErCH,EAAOK,GAASM,EAAAA,aACdN,EACAN,EACAC,EACA,GAAKG,EACLO,EACK,GAAgBE,OAAOF,EAAsBR,EAAME,SACpDF,EAAME,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,CACT,oBAGE,SAACc,GAAkBC,OAAAA,SAAAA,EAAQC,EAAGC,GAAO,IACnC,IAAMjB,EAASkB,MAAMC,KACnBL,aAAkBM,EAASA,UACvBN,EAAOO,OAAON,GACdO,EAAKA,MAACD,OAAOP,EAAQC,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAsBA,uBAAC,GAAIP,GAShEQ,QAAAC,QAPK1B,EAAOC,OAOL,CACLc,OAAQ,CAAA,EACRf,OAAQ2B,EAAYA,aAClB9B,EACEG,GACCiB,EAAQM,2BAAsD,QAAzBN,EAAQW,cAEhDX,IAbK,CACLjB,OAAQ,CAAA,EACRe,OAAAA,GAcN,CAAC,MAAAc,GAAA,OAAAJ,QAAAK,OAAAD,EAAA,CAAA,CAAA"}
|
package/typebox/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import type { TypeCheck } from '@sinclair/typebox/compiler';
|
|
3
|
+
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
|
4
|
+
export type Resolver = <T extends ReturnType<typeof Type.Object>>(schema: T | TypeCheck<T>) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Static, Type } from '@sinclair/typebox';
|
|
2
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
3
|
+
import { render, screen } from '@testing-library/react';
|
|
4
|
+
import user from '@testing-library/user-event';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { useForm } from 'react-hook-form';
|
|
7
|
+
import { typeboxResolver } from '..';
|
|
8
|
+
|
|
9
|
+
const schema = Type.Object({
|
|
10
|
+
username: Type.String({ minLength: 1 }),
|
|
11
|
+
password: Type.String({ minLength: 1 }),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const typecheck = TypeCompiler.Compile(schema);
|
|
15
|
+
|
|
16
|
+
type FormData = Static<typeof schema> & { unusedProperty: string };
|
|
17
|
+
|
|
18
|
+
interface Props {
|
|
19
|
+
onSubmit: (data: FormData) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function TestComponent({ onSubmit }: Props) {
|
|
23
|
+
const {
|
|
24
|
+
register,
|
|
25
|
+
handleSubmit,
|
|
26
|
+
formState: { errors },
|
|
27
|
+
} = useForm<FormData>({
|
|
28
|
+
resolver: typeboxResolver(typecheck), // Useful to check TypeScript regressions
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
33
|
+
<input {...register('username')} />
|
|
34
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
35
|
+
|
|
36
|
+
<input {...register('password')} />
|
|
37
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
38
|
+
|
|
39
|
+
<button type="submit">submit</button>
|
|
40
|
+
</form>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
test("form's validation with Typebox (with compiler) and TypeScript's integration", async () => {
|
|
45
|
+
const handleSubmit = vi.fn();
|
|
46
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
47
|
+
|
|
48
|
+
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
|
49
|
+
|
|
50
|
+
await user.click(screen.getByText(/submit/i));
|
|
51
|
+
|
|
52
|
+
expect(
|
|
53
|
+
screen.getAllByText(/Expected string length greater or equal to 1/i),
|
|
54
|
+
).toHaveLength(2);
|
|
55
|
+
|
|
56
|
+
expect(handleSubmit).not.toHaveBeenCalled();
|
|
57
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
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 { typeboxResolver } from '..';
|
|
6
|
+
|
|
7
|
+
import { Static, Type } from '@sinclair/typebox';
|
|
8
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
9
|
+
|
|
10
|
+
const schema = Type.Object({
|
|
11
|
+
username: Type.String({ minLength: 1 }),
|
|
12
|
+
password: Type.String({ minLength: 1 }),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const typecheck = TypeCompiler.Compile(schema);
|
|
16
|
+
|
|
17
|
+
type FormData = Static<typeof schema>;
|
|
18
|
+
|
|
19
|
+
interface Props {
|
|
20
|
+
onSubmit: (data: FormData) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function TestComponent({ onSubmit }: Props) {
|
|
24
|
+
const { register, handleSubmit } = useForm<FormData>({
|
|
25
|
+
resolver: typeboxResolver(typecheck),
|
|
26
|
+
shouldUseNativeValidation: true,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
31
|
+
<input {...register('username')} placeholder="username" />
|
|
32
|
+
|
|
33
|
+
<input {...register('password')} placeholder="password" />
|
|
34
|
+
|
|
35
|
+
<button type="submit">submit</button>
|
|
36
|
+
</form>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
test("form's native validation with Typebox (with compiler)", async () => {
|
|
41
|
+
const handleSubmit = vi.fn();
|
|
42
|
+
render(<TestComponent onSubmit={handleSubmit} />);
|
|
43
|
+
|
|
44
|
+
// username
|
|
45
|
+
let usernameField = screen.getByPlaceholderText(
|
|
46
|
+
/username/i,
|
|
47
|
+
) as HTMLInputElement;
|
|
48
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
49
|
+
expect(usernameField.validationMessage).toBe('');
|
|
50
|
+
|
|
51
|
+
// password
|
|
52
|
+
let passwordField = screen.getByPlaceholderText(
|
|
53
|
+
/password/i,
|
|
54
|
+
) as HTMLInputElement;
|
|
55
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
56
|
+
expect(passwordField.validationMessage).toBe('');
|
|
57
|
+
|
|
58
|
+
await user.click(screen.getByText(/submit/i));
|
|
59
|
+
|
|
60
|
+
// username
|
|
61
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
62
|
+
expect(usernameField.validity.valid).toBe(false);
|
|
63
|
+
expect(usernameField.validationMessage).toBe(
|
|
64
|
+
'Expected string length greater or equal to 1',
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// password
|
|
68
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
69
|
+
expect(passwordField.validity.valid).toBe(false);
|
|
70
|
+
expect(passwordField.validationMessage).toBe(
|
|
71
|
+
'Expected string length greater or equal to 1',
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
await user.type(screen.getByPlaceholderText(/username/i), 'joe');
|
|
75
|
+
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
|
76
|
+
|
|
77
|
+
// username
|
|
78
|
+
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
|
79
|
+
expect(usernameField.validity.valid).toBe(true);
|
|
80
|
+
expect(usernameField.validationMessage).toBe('');
|
|
81
|
+
|
|
82
|
+
// password
|
|
83
|
+
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
|
84
|
+
expect(passwordField.validity.valid).toBe(true);
|
|
85
|
+
expect(passwordField.validationMessage).toBe('');
|
|
86
|
+
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { useForm } from 'react-hook-form';
|
|
3
1
|
import { render, screen } from '@testing-library/react';
|
|
4
2
|
import user from '@testing-library/user-event';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { useForm } from 'react-hook-form';
|
|
5
5
|
import { typeboxResolver } from '..';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { Static, Type } from '@sinclair/typebox';
|
|
8
8
|
|
|
9
9
|
const schema = Type.Object({
|
|
10
10
|
username: Type.String({ minLength: 1 }),
|
|
@@ -34,7 +34,7 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
34
34
|
);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
test("form's native validation with
|
|
37
|
+
test("form's native validation with Typebox", async () => {
|
|
38
38
|
const handleSubmit = vi.fn();
|
|
39
39
|
render(<TestComponent onSubmit={handleSubmit} />);
|
|
40
40
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Static, Type } from '@sinclair/typebox';
|
|
2
2
|
import { render, screen } from '@testing-library/react';
|
|
3
3
|
import user from '@testing-library/user-event';
|
|
4
|
+
import React from 'react';
|
|
4
5
|
import { useForm } from 'react-hook-form';
|
|
5
6
|
import { typeboxResolver } from '..';
|
|
6
|
-
import { Type, Static } from '@sinclair/typebox';
|
|
7
7
|
|
|
8
8
|
const schema = Type.Object({
|
|
9
9
|
username: Type.String({ minLength: 1 }),
|
|
@@ -38,7 +38,7 @@ function TestComponent({ onSubmit }: Props) {
|
|
|
38
38
|
);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
test("form's validation with
|
|
41
|
+
test("form's validation with Typebox and TypeScript's integration", async () => {
|
|
42
42
|
const handleSubmit = vi.fn();
|
|
43
43
|
render(<TestComponent onSubmit={handleSubmit} />);
|
|
44
44
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
1
|
import { Static, Type } from '@sinclair/typebox';
|
|
2
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
3
3
|
|
|
4
4
|
export const schema = Type.Object({
|
|
5
5
|
username: Type.String({
|
|
@@ -19,7 +19,11 @@ export const schema = Type.Object({
|
|
|
19
19
|
maximum: 2013,
|
|
20
20
|
}),
|
|
21
21
|
),
|
|
22
|
-
email: Type.Optional(
|
|
22
|
+
email: Type.Optional(
|
|
23
|
+
Type.RegExp(
|
|
24
|
+
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
|
|
25
|
+
),
|
|
26
|
+
),
|
|
23
27
|
tags: Type.Array(Type.String()),
|
|
24
28
|
enabled: Type.Boolean(),
|
|
25
29
|
like: Type.Optional(
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`typeboxResolver (with compiler) > should return a single error from typeboxResolver when validation fails 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"errors": {
|
|
6
|
+
"accessToken": {
|
|
7
|
+
"message": "Required property",
|
|
8
|
+
"ref": undefined,
|
|
9
|
+
"type": "45",
|
|
10
|
+
},
|
|
11
|
+
"birthYear": {
|
|
12
|
+
"message": "Expected number",
|
|
13
|
+
"ref": undefined,
|
|
14
|
+
"type": "41",
|
|
15
|
+
},
|
|
16
|
+
"dateStr": {
|
|
17
|
+
"message": "Required property",
|
|
18
|
+
"ref": undefined,
|
|
19
|
+
"type": "45",
|
|
20
|
+
},
|
|
21
|
+
"email": {
|
|
22
|
+
"message": "Expected string to match regular expression",
|
|
23
|
+
"ref": {
|
|
24
|
+
"name": "email",
|
|
25
|
+
},
|
|
26
|
+
"type": "48",
|
|
27
|
+
},
|
|
28
|
+
"enabled": {
|
|
29
|
+
"message": "Required property",
|
|
30
|
+
"ref": undefined,
|
|
31
|
+
"type": "45",
|
|
32
|
+
},
|
|
33
|
+
"like": [
|
|
34
|
+
{
|
|
35
|
+
"id": {
|
|
36
|
+
"message": "Expected number",
|
|
37
|
+
"ref": undefined,
|
|
38
|
+
"type": "41",
|
|
39
|
+
},
|
|
40
|
+
"name": {
|
|
41
|
+
"message": "Required property",
|
|
42
|
+
"ref": undefined,
|
|
43
|
+
"type": "45",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
"password": {
|
|
48
|
+
"message": "Expected string length greater or equal to 8",
|
|
49
|
+
"ref": {
|
|
50
|
+
"name": "password",
|
|
51
|
+
},
|
|
52
|
+
"type": "52",
|
|
53
|
+
},
|
|
54
|
+
"repeatPassword": {
|
|
55
|
+
"message": "Required property",
|
|
56
|
+
"ref": undefined,
|
|
57
|
+
"type": "45",
|
|
58
|
+
},
|
|
59
|
+
"tags": {
|
|
60
|
+
"message": "Required property",
|
|
61
|
+
"ref": undefined,
|
|
62
|
+
"type": "45",
|
|
63
|
+
},
|
|
64
|
+
"username": {
|
|
65
|
+
"message": "Required property",
|
|
66
|
+
"ref": {
|
|
67
|
+
"name": "username",
|
|
68
|
+
},
|
|
69
|
+
"type": "45",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
"values": {},
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
exports[`typeboxResolver (with compiler) > should return all the errors from typeboxResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
|
77
|
+
{
|
|
78
|
+
"errors": {
|
|
79
|
+
"accessToken": {
|
|
80
|
+
"message": "Required property",
|
|
81
|
+
"ref": undefined,
|
|
82
|
+
"type": "45",
|
|
83
|
+
"types": {
|
|
84
|
+
"45": "Required property",
|
|
85
|
+
"62": "Expected union value",
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
"birthYear": {
|
|
89
|
+
"message": "Expected number",
|
|
90
|
+
"ref": undefined,
|
|
91
|
+
"type": "41",
|
|
92
|
+
"types": {
|
|
93
|
+
"41": "Expected number",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
"dateStr": {
|
|
97
|
+
"message": "Required property",
|
|
98
|
+
"ref": undefined,
|
|
99
|
+
"type": "45",
|
|
100
|
+
"types": {
|
|
101
|
+
"20": "Expected Date",
|
|
102
|
+
"45": "Required property",
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
"email": {
|
|
106
|
+
"message": "Expected string to match regular expression",
|
|
107
|
+
"ref": {
|
|
108
|
+
"name": "email",
|
|
109
|
+
},
|
|
110
|
+
"type": "48",
|
|
111
|
+
"types": {
|
|
112
|
+
"48": "Expected string to match regular expression",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
"enabled": {
|
|
116
|
+
"message": "Required property",
|
|
117
|
+
"ref": undefined,
|
|
118
|
+
"type": "45",
|
|
119
|
+
"types": {
|
|
120
|
+
"14": "Expected boolean",
|
|
121
|
+
"45": "Required property",
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
"like": [
|
|
125
|
+
{
|
|
126
|
+
"id": {
|
|
127
|
+
"message": "Expected number",
|
|
128
|
+
"ref": undefined,
|
|
129
|
+
"type": "41",
|
|
130
|
+
"types": {
|
|
131
|
+
"41": "Expected number",
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
"name": {
|
|
135
|
+
"message": "Required property",
|
|
136
|
+
"ref": undefined,
|
|
137
|
+
"type": "45",
|
|
138
|
+
"types": {
|
|
139
|
+
"45": "Required property",
|
|
140
|
+
"54": "Expected string",
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
"password": {
|
|
146
|
+
"message": "Expected string length greater or equal to 8",
|
|
147
|
+
"ref": {
|
|
148
|
+
"name": "password",
|
|
149
|
+
},
|
|
150
|
+
"type": "52",
|
|
151
|
+
"types": {
|
|
152
|
+
"52": "Expected string length greater or equal to 8",
|
|
153
|
+
"53": "Expected string to match '^(.*[A-Za-z\\d].*)$'",
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
"repeatPassword": {
|
|
157
|
+
"message": "Required property",
|
|
158
|
+
"ref": undefined,
|
|
159
|
+
"type": "45",
|
|
160
|
+
"types": {
|
|
161
|
+
"45": "Required property",
|
|
162
|
+
"54": "Expected string",
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
"tags": {
|
|
166
|
+
"message": "Required property",
|
|
167
|
+
"ref": undefined,
|
|
168
|
+
"type": "45",
|
|
169
|
+
"types": {
|
|
170
|
+
"45": "Required property",
|
|
171
|
+
"6": "Expected array",
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
"username": {
|
|
175
|
+
"message": "Required property",
|
|
176
|
+
"ref": {
|
|
177
|
+
"name": "username",
|
|
178
|
+
},
|
|
179
|
+
"type": "45",
|
|
180
|
+
"types": {
|
|
181
|
+
"45": "Required property",
|
|
182
|
+
"54": "Expected string",
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
"values": {},
|
|
187
|
+
}
|
|
188
|
+
`;
|
|
@@ -19,11 +19,11 @@ exports[`typeboxResolver > should return a single error from typeboxResolver whe
|
|
|
19
19
|
"type": "45",
|
|
20
20
|
},
|
|
21
21
|
"email": {
|
|
22
|
-
"message": "Expected string to match
|
|
22
|
+
"message": "Expected string to match regular expression",
|
|
23
23
|
"ref": {
|
|
24
24
|
"name": "email",
|
|
25
25
|
},
|
|
26
|
-
"type": "
|
|
26
|
+
"type": "48",
|
|
27
27
|
},
|
|
28
28
|
"enabled": {
|
|
29
29
|
"message": "Required property",
|
|
@@ -49,7 +49,7 @@ exports[`typeboxResolver > should return a single error from typeboxResolver whe
|
|
|
49
49
|
"ref": {
|
|
50
50
|
"name": "password",
|
|
51
51
|
},
|
|
52
|
-
"type": "
|
|
52
|
+
"type": "52",
|
|
53
53
|
},
|
|
54
54
|
"repeatPassword": {
|
|
55
55
|
"message": "Required property",
|
|
@@ -82,7 +82,7 @@ exports[`typeboxResolver > should return all the errors from typeboxResolver whe
|
|
|
82
82
|
"type": "45",
|
|
83
83
|
"types": {
|
|
84
84
|
"45": "Required property",
|
|
85
|
-
"
|
|
85
|
+
"62": "Expected union value",
|
|
86
86
|
},
|
|
87
87
|
},
|
|
88
88
|
"birthYear": {
|
|
@@ -103,13 +103,13 @@ exports[`typeboxResolver > should return all the errors from typeboxResolver whe
|
|
|
103
103
|
},
|
|
104
104
|
},
|
|
105
105
|
"email": {
|
|
106
|
-
"message": "Expected string to match
|
|
106
|
+
"message": "Expected string to match regular expression",
|
|
107
107
|
"ref": {
|
|
108
108
|
"name": "email",
|
|
109
109
|
},
|
|
110
|
-
"type": "
|
|
110
|
+
"type": "48",
|
|
111
111
|
"types": {
|
|
112
|
-
"
|
|
112
|
+
"48": "Expected string to match regular expression",
|
|
113
113
|
},
|
|
114
114
|
},
|
|
115
115
|
"enabled": {
|
|
@@ -137,7 +137,7 @@ exports[`typeboxResolver > should return all the errors from typeboxResolver whe
|
|
|
137
137
|
"type": "45",
|
|
138
138
|
"types": {
|
|
139
139
|
"45": "Required property",
|
|
140
|
-
"
|
|
140
|
+
"54": "Expected string",
|
|
141
141
|
},
|
|
142
142
|
},
|
|
143
143
|
},
|
|
@@ -147,10 +147,10 @@ exports[`typeboxResolver > should return all the errors from typeboxResolver whe
|
|
|
147
147
|
"ref": {
|
|
148
148
|
"name": "password",
|
|
149
149
|
},
|
|
150
|
-
"type": "
|
|
150
|
+
"type": "52",
|
|
151
151
|
"types": {
|
|
152
|
-
"
|
|
153
|
-
"
|
|
152
|
+
"52": "Expected string length greater or equal to 8",
|
|
153
|
+
"53": "Expected string to match '^(.*[A-Za-z\\d].*)$'",
|
|
154
154
|
},
|
|
155
155
|
},
|
|
156
156
|
"repeatPassword": {
|
|
@@ -159,7 +159,7 @@ exports[`typeboxResolver > should return all the errors from typeboxResolver whe
|
|
|
159
159
|
"type": "45",
|
|
160
160
|
"types": {
|
|
161
161
|
"45": "Required property",
|
|
162
|
-
"
|
|
162
|
+
"54": "Expected string",
|
|
163
163
|
},
|
|
164
164
|
},
|
|
165
165
|
"tags": {
|
|
@@ -179,7 +179,7 @@ exports[`typeboxResolver > should return all the errors from typeboxResolver whe
|
|
|
179
179
|
"type": "45",
|
|
180
180
|
"types": {
|
|
181
181
|
"45": "Required property",
|
|
182
|
-
"
|
|
182
|
+
"54": "Expected string",
|
|
183
183
|
},
|
|
184
184
|
},
|
|
185
185
|
},
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
2
|
+
import { typeboxResolver } from '..';
|
|
3
|
+
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
4
|
+
|
|
5
|
+
const shouldUseNativeValidation = false;
|
|
6
|
+
|
|
7
|
+
describe('typeboxResolver (with compiler)', () => {
|
|
8
|
+
const typecheck = TypeCompiler.Compile(schema);
|
|
9
|
+
|
|
10
|
+
it('should return a single error from typeboxResolver when validation fails', async () => {
|
|
11
|
+
const result = await typeboxResolver(typecheck)(invalidData, undefined, {
|
|
12
|
+
fields,
|
|
13
|
+
shouldUseNativeValidation,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
expect(result).toMatchSnapshot();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should return all the errors from typeboxResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
20
|
+
const result = await typeboxResolver(typecheck)(invalidData, undefined, {
|
|
21
|
+
fields,
|
|
22
|
+
criteriaMode: 'all',
|
|
23
|
+
shouldUseNativeValidation,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(result).toMatchSnapshot();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should validate with success', async () => {
|
|
30
|
+
const result = await typeboxResolver(typecheck)(validData, undefined, {
|
|
31
|
+
fields,
|
|
32
|
+
shouldUseNativeValidation,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
36
|
+
});
|
|
37
|
+
});
|