@hookform/resolvers 3.7.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.
Files changed (252) hide show
  1. package/README.md +69 -0
  2. package/ajv/dist/ajv.js.map +1 -1
  3. package/ajv/dist/ajv.modern.mjs.map +1 -1
  4. package/ajv/dist/ajv.module.js.map +1 -1
  5. package/ajv/dist/ajv.umd.js.map +1 -1
  6. package/ajv/dist/types.d.ts +1 -1
  7. package/ajv/src/__tests__/ajv.ts +7 -1
  8. package/ajv/src/ajv.ts +1 -1
  9. package/ajv/src/types.ts +1 -5
  10. package/arktype/dist/arktype.js +1 -1
  11. package/arktype/dist/arktype.js.map +1 -1
  12. package/arktype/dist/arktype.mjs +1 -1
  13. package/arktype/dist/arktype.modern.mjs +1 -1
  14. package/arktype/dist/arktype.modern.mjs.map +1 -1
  15. package/arktype/dist/arktype.module.js +1 -1
  16. package/arktype/dist/arktype.module.js.map +1 -1
  17. package/arktype/dist/arktype.umd.js +1 -1
  18. package/arktype/dist/arktype.umd.js.map +1 -1
  19. package/arktype/dist/types.d.ts +1 -1
  20. package/arktype/package.json +2 -1
  21. package/arktype/src/__tests__/Form-native-validation.tsx +5 -5
  22. package/arktype/src/__tests__/Form.tsx +2 -2
  23. package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
  24. package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +29 -29
  25. package/arktype/src/__tests__/arktype.ts +1 -1
  26. package/arktype/src/arktype.ts +2 -2
  27. package/arktype/src/types.ts +1 -1
  28. package/class-validator/dist/class-validator.js.map +1 -1
  29. package/class-validator/dist/class-validator.modern.mjs.map +1 -1
  30. package/class-validator/dist/class-validator.module.js.map +1 -1
  31. package/class-validator/dist/class-validator.umd.js.map +1 -1
  32. package/class-validator/dist/types.d.ts +2 -2
  33. package/class-validator/src/__tests__/Form-native-validation.tsx +2 -2
  34. package/class-validator/src/__tests__/Form.tsx +2 -2
  35. package/class-validator/src/__tests__/__snapshots__/class-validator.ts.snap +2 -2
  36. package/class-validator/src/__tests__/class-validator.ts +4 -4
  37. package/class-validator/src/class-validator.ts +2 -2
  38. package/class-validator/src/types.ts +2 -2
  39. package/computed-types/dist/computed-types.js.map +1 -1
  40. package/computed-types/dist/computed-types.modern.mjs.map +1 -1
  41. package/computed-types/dist/computed-types.module.js.map +1 -1
  42. package/computed-types/dist/computed-types.umd.js.map +1 -1
  43. package/computed-types/dist/types.d.ts +1 -1
  44. package/computed-types/src/__tests__/Form-native-validation.tsx +2 -2
  45. package/computed-types/src/__tests__/Form.tsx +2 -2
  46. package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
  47. package/computed-types/src/__tests__/__snapshots__/computed-types.ts.snap +6 -6
  48. package/computed-types/src/__tests__/computed-types.ts +1 -1
  49. package/computed-types/src/computed-types.ts +2 -2
  50. package/computed-types/src/types.ts +1 -1
  51. package/dist/resolvers.js +1 -1
  52. package/dist/resolvers.js.map +1 -1
  53. package/dist/resolvers.mjs +1 -1
  54. package/dist/resolvers.mjs.map +1 -1
  55. package/dist/resolvers.module.js +1 -1
  56. package/dist/resolvers.module.js.map +1 -1
  57. package/dist/resolvers.umd.js +1 -1
  58. package/dist/resolvers.umd.js.map +1 -1
  59. package/dist/toNestErrors.d.ts +1 -6
  60. package/dist/validateFieldsNatively.d.ts +1 -1
  61. package/effect-ts/dist/effect-ts.js +1 -1
  62. package/effect-ts/dist/effect-ts.js.map +1 -1
  63. package/effect-ts/dist/effect-ts.mjs +1 -1
  64. package/effect-ts/dist/effect-ts.modern.mjs +1 -1
  65. package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
  66. package/effect-ts/dist/effect-ts.module.js +1 -1
  67. package/effect-ts/dist/effect-ts.module.js.map +1 -1
  68. package/effect-ts/dist/effect-ts.umd.js +1 -1
  69. package/effect-ts/dist/effect-ts.umd.js.map +1 -1
  70. package/effect-ts/src/__tests__/Form-native-validation.tsx +2 -2
  71. package/effect-ts/src/__tests__/Form.tsx +2 -2
  72. package/effect-ts/src/__tests__/__snapshots__/effect-ts.ts.snap +2 -2
  73. package/effect-ts/src/__tests__/effect-ts.ts +1 -1
  74. package/effect-ts/src/effect-ts.ts +1 -1
  75. package/io-ts/dist/errorsToRecord.d.ts +0 -1
  76. package/io-ts/dist/io-ts.js +1 -1
  77. package/io-ts/dist/io-ts.js.map +1 -1
  78. package/io-ts/dist/io-ts.mjs +1 -1
  79. package/io-ts/dist/io-ts.modern.mjs +1 -1
  80. package/io-ts/dist/io-ts.modern.mjs.map +1 -1
  81. package/io-ts/dist/io-ts.module.js +1 -1
  82. package/io-ts/dist/io-ts.module.js.map +1 -1
  83. package/io-ts/dist/io-ts.umd.js +1 -1
  84. package/io-ts/dist/io-ts.umd.js.map +1 -1
  85. package/io-ts/src/__tests__/Form-native-validation.tsx +2 -2
  86. package/io-ts/src/__tests__/Form.tsx +2 -2
  87. package/io-ts/src/__tests__/__snapshots__/io-ts.ts.snap +9 -9
  88. package/io-ts/src/__tests__/errorsToRecord.ts +29 -19
  89. package/io-ts/src/__tests__/io-ts.ts +1 -1
  90. package/io-ts/src/errorsToRecord.ts +19 -10
  91. package/io-ts/src/io-ts.ts +1 -1
  92. package/joi/dist/joi.js +1 -1
  93. package/joi/dist/joi.js.map +1 -1
  94. package/joi/dist/joi.mjs +1 -1
  95. package/joi/dist/joi.modern.mjs +1 -1
  96. package/joi/dist/joi.modern.mjs.map +1 -1
  97. package/joi/dist/joi.module.js +1 -1
  98. package/joi/dist/joi.module.js.map +1 -1
  99. package/joi/dist/joi.umd.js +1 -1
  100. package/joi/dist/joi.umd.js.map +1 -1
  101. package/joi/dist/types.d.ts +1 -1
  102. package/joi/src/__tests__/Form-native-validation.tsx +2 -2
  103. package/joi/src/__tests__/Form.tsx +2 -2
  104. package/joi/src/__tests__/__snapshots__/joi.ts.snap +56 -56
  105. package/joi/src/__tests__/joi.ts +1 -1
  106. package/joi/src/joi.ts +1 -1
  107. package/joi/src/types.ts +1 -5
  108. package/nope/dist/nope.js.map +1 -1
  109. package/nope/dist/nope.modern.mjs.map +1 -1
  110. package/nope/dist/nope.module.js.map +1 -1
  111. package/nope/dist/nope.umd.js.map +1 -1
  112. package/nope/dist/types.d.ts +1 -1
  113. package/nope/src/__tests__/Form-native-validation.tsx +2 -2
  114. package/nope/src/__tests__/Form.tsx +2 -2
  115. package/nope/src/__tests__/__fixtures__/data.ts +1 -1
  116. package/nope/src/__tests__/nope.ts +1 -1
  117. package/nope/src/nope.ts +1 -1
  118. package/nope/src/types.ts +2 -2
  119. package/package.json +54 -51
  120. package/superstruct/dist/superstruct.js.map +1 -1
  121. package/superstruct/dist/superstruct.modern.mjs.map +1 -1
  122. package/superstruct/dist/superstruct.module.js.map +1 -1
  123. package/superstruct/dist/superstruct.umd.js.map +1 -1
  124. package/superstruct/dist/types.d.ts +1 -1
  125. package/superstruct/src/__tests__/Form-native-validation.tsx +2 -2
  126. package/superstruct/src/__tests__/Form.tsx +2 -2
  127. package/superstruct/src/__tests__/__fixtures__/data.ts +8 -8
  128. package/superstruct/src/__tests__/__snapshots__/superstruct.ts.snap +4 -4
  129. package/superstruct/src/__tests__/superstruct.ts +1 -1
  130. package/superstruct/src/superstruct.ts +1 -1
  131. package/superstruct/src/types.ts +1 -1
  132. package/typanion/dist/typanion.js.map +1 -1
  133. package/typanion/dist/typanion.modern.mjs.map +1 -1
  134. package/typanion/dist/typanion.module.js.map +1 -1
  135. package/typanion/dist/typanion.umd.js.map +1 -1
  136. package/typanion/dist/types.d.ts +1 -1
  137. package/typanion/src/__tests__/Form-native-validation.tsx +1 -1
  138. package/typanion/src/__tests__/Form.tsx +1 -1
  139. package/typanion/src/__tests__/__fixtures__/data.ts +12 -7
  140. package/typanion/src/__tests__/__snapshots__/typanion.ts.snap +4 -4
  141. package/typanion/src/__tests__/typanion.ts +1 -1
  142. package/typanion/src/typanion.ts +1 -1
  143. package/typanion/src/types.ts +4 -4
  144. package/typebox/dist/typebox.js +1 -1
  145. package/typebox/dist/typebox.js.map +1 -1
  146. package/typebox/dist/typebox.mjs +1 -1
  147. package/typebox/dist/typebox.modern.mjs +1 -1
  148. package/typebox/dist/typebox.modern.mjs.map +1 -1
  149. package/typebox/dist/typebox.module.js +1 -1
  150. package/typebox/dist/typebox.module.js.map +1 -1
  151. package/typebox/dist/typebox.umd.js +1 -1
  152. package/typebox/dist/typebox.umd.js.map +1 -1
  153. package/typebox/dist/types.d.ts +3 -2
  154. package/typebox/src/__tests__/Form-compiler.tsx +57 -0
  155. package/typebox/src/__tests__/Form-native-validation-compiler.tsx +86 -0
  156. package/typebox/src/__tests__/Form-native-validation.tsx +4 -4
  157. package/typebox/src/__tests__/Form.tsx +3 -3
  158. package/typebox/src/__tests__/__fixtures__/data.ts +6 -2
  159. package/typebox/src/__tests__/__snapshots__/typebox-compiler.ts.snap +188 -0
  160. package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +13 -13
  161. package/typebox/src/__tests__/typebox-compiler.ts +37 -0
  162. package/typebox/src/__tests__/typebox.ts +1 -1
  163. package/typebox/src/typebox.ts +8 -3
  164. package/typebox/src/types.ts +3 -2
  165. package/typeschema/dist/index.d.ts +2 -0
  166. package/typeschema/dist/types.d.ts +9 -0
  167. package/typeschema/dist/typeschema.d.ts +2 -0
  168. package/typeschema/dist/typeschema.js +2 -0
  169. package/typeschema/dist/typeschema.js.map +1 -0
  170. package/typeschema/dist/typeschema.mjs +2 -0
  171. package/typeschema/dist/typeschema.modern.mjs +2 -0
  172. package/typeschema/dist/typeschema.modern.mjs.map +1 -0
  173. package/typeschema/dist/typeschema.module.js +2 -0
  174. package/typeschema/dist/typeschema.module.js.map +1 -0
  175. package/typeschema/dist/typeschema.umd.js +2 -0
  176. package/typeschema/dist/typeschema.umd.js.map +1 -0
  177. package/typeschema/package.json +18 -0
  178. package/typeschema/src/__tests__/Form-native-validation.tsx +82 -0
  179. package/typeschema/src/__tests__/Form.tsx +53 -0
  180. package/typeschema/src/__tests__/__fixtures__/data.ts +88 -0
  181. package/typeschema/src/__tests__/__snapshots__/typeschema.ts.snap +225 -0
  182. package/typeschema/src/__tests__/typeschema.ts +70 -0
  183. package/typeschema/src/index.ts +2 -0
  184. package/typeschema/src/types.ts +18 -0
  185. package/typeschema/src/typeschema.ts +70 -0
  186. package/valibot/dist/types.d.ts +1 -1
  187. package/valibot/dist/valibot.js +1 -1
  188. package/valibot/dist/valibot.js.map +1 -1
  189. package/valibot/dist/valibot.mjs +1 -1
  190. package/valibot/dist/valibot.modern.mjs +1 -1
  191. package/valibot/dist/valibot.modern.mjs.map +1 -1
  192. package/valibot/dist/valibot.module.js +1 -1
  193. package/valibot/dist/valibot.module.js.map +1 -1
  194. package/valibot/dist/valibot.umd.js +1 -1
  195. package/valibot/dist/valibot.umd.js.map +1 -1
  196. package/valibot/src/__tests__/Form-native-validation.tsx +2 -2
  197. package/valibot/src/__tests__/Form.tsx +1 -1
  198. package/valibot/src/__tests__/valibot.ts +4 -4
  199. package/valibot/src/types.ts +1 -1
  200. package/valibot/src/valibot.ts +12 -6
  201. package/vest/dist/types.d.ts +2 -2
  202. package/vest/dist/vest.js.map +1 -1
  203. package/vest/dist/vest.modern.mjs.map +1 -1
  204. package/vest/dist/vest.module.js.map +1 -1
  205. package/vest/dist/vest.umd.js.map +1 -1
  206. package/vest/src/__tests__/Form-native-validation.tsx +1 -1
  207. package/vest/src/__tests__/Form.tsx +1 -1
  208. package/vest/src/__tests__/__snapshots__/vest.ts.snap +0 -6
  209. package/vest/src/__tests__/vest.ts +14 -8
  210. package/vest/src/types.ts +11 -4
  211. package/vest/src/vest.ts +1 -1
  212. package/vine/dist/types.d.ts +1 -1
  213. package/vine/dist/vine.js +1 -1
  214. package/vine/dist/vine.js.map +1 -1
  215. package/vine/dist/vine.mjs +1 -1
  216. package/vine/dist/vine.modern.mjs +1 -1
  217. package/vine/dist/vine.modern.mjs.map +1 -1
  218. package/vine/dist/vine.module.js +1 -1
  219. package/vine/dist/vine.module.js.map +1 -1
  220. package/vine/dist/vine.umd.js +1 -1
  221. package/vine/dist/vine.umd.js.map +1 -1
  222. package/vine/src/__tests__/Form-native-validation.tsx +9 -5
  223. package/vine/src/__tests__/Form.tsx +9 -5
  224. package/vine/src/__tests__/__fixtures__/data.ts +4 -2
  225. package/vine/src/__tests__/vine.ts +1 -1
  226. package/vine/src/types.ts +1 -1
  227. package/vine/src/vine.ts +10 -8
  228. package/yup/dist/yup.d.ts +1 -1
  229. package/yup/dist/yup.js.map +1 -1
  230. package/yup/dist/yup.modern.mjs.map +1 -1
  231. package/yup/dist/yup.module.js.map +1 -1
  232. package/yup/dist/yup.umd.js.map +1 -1
  233. package/yup/src/__tests__/Form-native-validation.tsx +1 -1
  234. package/yup/src/__tests__/Form.tsx +1 -1
  235. package/yup/src/__tests__/__snapshots__/yup.ts.snap +13 -13
  236. package/yup/src/__tests__/yup.ts +9 -7
  237. package/yup/src/yup.ts +2 -2
  238. package/zod/dist/types.d.ts +1 -1
  239. package/zod/dist/zod.js +1 -1
  240. package/zod/dist/zod.js.map +1 -1
  241. package/zod/dist/zod.mjs +1 -1
  242. package/zod/dist/zod.modern.mjs +1 -1
  243. package/zod/dist/zod.modern.mjs.map +1 -1
  244. package/zod/dist/zod.module.js +1 -1
  245. package/zod/dist/zod.module.js.map +1 -1
  246. package/zod/dist/zod.umd.js +1 -1
  247. package/zod/dist/zod.umd.js.map +1 -1
  248. package/zod/src/__tests__/Form-native-validation.tsx +2 -2
  249. package/zod/src/__tests__/Form.tsx +1 -1
  250. package/zod/src/__tests__/zod.ts +1 -1
  251. package/zod/src/types.ts +1 -1
  252. package/zod/src/zod.ts +4 -3
@@ -1 +1 @@
1
- {"version":3,"file":"typebox.modern.mjs","sources":["../src/typebox.ts"],"sourcesContent":["import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\nimport { Value, ValueError } from '@sinclair/typebox/value';\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(Value.Errors(schema, values));\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","Value","Errors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors","criteriaMode"],"mappings":"8KAKA,MAAMA,EAAmBA,CACvBC,EACAC,KAEA,MAAMC,EAAqC,CAAA,EAC3C,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,QAEb,CAEDN,EAAQe,OACT,CAED,OAAOb,GAGIc,EACVC,GAAWC,MAAOC,EAAQC,EAAGC,KAC5B,MAAMnB,EAASoB,MAAMC,KAAKC,EAAMC,OAAOR,EAAQE,IAI/C,OAFAE,EAAQK,2BAA6BC,EAAuB,CAAE,EAAEN,GAE3DnB,EAAOC,OAOL,CACLgB,OAAQ,CAAA,EACRjB,OAAQ0B,EACN7B,EACEG,GACCmB,EAAQK,2BAAsD,QAAzBL,EAAQQ,cAEhDR,IAbK,CACLnB,OAAQ,CAAA,EACRiB"}
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{appendErrors as r}from"react-hook-form";import{validateFieldsNatively as e,toNestErrors as o}from"@hookform/resolvers";import{Value as t}from"@sinclair/typebox/value";var a=function(e,o){for(var t={};e.length;){var a=e[0],s=a.type,i=a.message,n=a.path.substring(1).replace(/\//g,".");if(t[n]||(t[n]={message:i,type:""+s}),o){var l=t[n].types,m=l&&l[""+s];t[n]=r(n,o,t,""+s,m?[].concat(m,a.message):a.message)}e.shift()}return t},s=function(r){return function(s,i,n){try{var l=Array.from(t.Errors(r,s));return n.shouldUseNativeValidation&&e({},n),Promise.resolve(l.length?{values:{},errors:o(a(l,!n.shouldUseNativeValidation&&"all"===n.criteriaMode),n)}:{errors:{},values:s})}catch(r){return Promise.reject(r)}}};export{s as typeboxResolver};
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 { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\nimport { Value, ValueError } from '@sinclair/typebox/value';\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(Value.Errors(schema, values));\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","Value","Errors","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","toNestErrors","criteriaMode","e","reject"],"mappings":"8KAKA,IAAMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,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,QAEb,CAEDN,EAAQe,OACT,CAED,OAAOb,CACT,EAEac,EACX,SAACC,GAAkBC,OAAAA,SAAAA,EAAQC,EAAGC,GAAO,IACnC,IAAMlB,EAASmB,MAAMC,KAAKC,EAAMC,OAAOP,EAAQC,IAI/C,OAFAE,EAAQK,2BAA6BC,EAAuB,CAAA,EAAIN,GAShEO,QAAAC,QAPK1B,EAAOC,OAOL,CACLe,OAAQ,CAAE,EACVhB,OAAQ2B,EACN9B,EACEG,GACCkB,EAAQK,2BAAsD,QAAzBL,EAAQU,cAEhDV,IAbK,CACLlB,OAAQ,GACRgB,OAAAA,GAcN,CAAC,MAAAa,GAAAJ,OAAAA,QAAAK,OAAAD,EAAA,CAAA,CAAA"}
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("react-hook-form"),require("@hookform/resolvers"),require("@sinclair/typebox/value")):"function"==typeof define&&define.amd?define(["exports","react-hook-form","@hookform/resolvers","@sinclair/typebox/value"],r):r((e||self).hookformResolversTypebox={},e.ReactHookForm,e.hookformResolvers,e.value)}(this,function(e,r,o,s){var t=function(e,o){for(var s={};e.length;){var t=e[0],a=t.type,i=t.message,l=t.path.substring(1).replace(/\//g,".");if(s[l]||(s[l]={message:i,type:""+a}),o){var n=s[l].types,f=n&&n[""+a];s[l]=r.appendErrors(l,o,s,""+a,f?[].concat(f,t.message):t.message)}e.shift()}return s};e.typeboxResolver=function(e){return function(r,a,i){try{var l=Array.from(s.Value.Errors(e,r));return i.shouldUseNativeValidation&&o.validateFieldsNatively({},i),Promise.resolve(l.length?{values:{},errors:o.toNestErrors(t(l,!i.shouldUseNativeValidation&&"all"===i.criteriaMode),i)}:{errors:{},values:r})}catch(e){return Promise.reject(e)}}}});
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 { appendErrors, FieldError, FieldErrors } from 'react-hook-form';\nimport { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { Resolver } from './types';\nimport { Value, ValueError } from '@sinclair/typebox/value';\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(Value.Errors(schema, values));\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","Value","Errors","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","toNestErrors","criteriaMode","e","reject"],"mappings":"mcAKA,IAAMA,EAAmB,SACvBC,EACAC,GAGA,IADA,IAAMC,EAAqC,CAAE,EACtCF,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,EAAYA,aAC1BN,EACAN,EACAC,EACA,GAAKG,EACLO,EACK,GAAgBE,OAAOF,EAAsBR,EAAME,SACpDF,EAAME,QAEb,CAEDN,EAAQe,OACT,CAED,OAAOb,CACT,oBAGE,SAACc,GAAkBC,OAAAA,SAAAA,EAAQC,EAAGC,GAAO,IACnC,IAAMjB,EAASkB,MAAMC,KAAKC,EAAAA,MAAMC,OAAOP,EAAQC,IAI/C,OAFAE,EAAQK,2BAA6BC,EAAAA,uBAAuB,CAAA,EAAIN,GAShEO,QAAAC,QAPKzB,EAAOC,OAOL,CACLc,OAAQ,CAAE,EACVf,OAAQ0B,EAAAA,aACN7B,EACEG,GACCiB,EAAQK,2BAAsD,QAAzBL,EAAQU,cAEhDV,IAbK,CACLjB,OAAQ,GACRe,OAAAA,GAcN,CAAC,MAAAa,GAAAJ,OAAAA,QAAAK,OAAAD,EAAA,CAAA,CAAA"}
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"}
@@ -1,3 +1,4 @@
1
1
  import { Type } from '@sinclair/typebox';
2
- import { FieldValues, ResolverResult, ResolverOptions } from 'react-hook-form';
3
- export type Resolver = <T extends ReturnType<typeof Type.Object>>(schema: T) => <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<ResolverResult<TFieldValues>>;
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 { Type, Static } from '@sinclair/typebox';
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 Zod", async () => {
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 React from 'react';
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 Zod and TypeScript's integration", async () => {
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(Type.RegExp(/^[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)),
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 '^[a-z0-9!#$%&'*+/=?^_\`{|}~-]+(?:\\\\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$'",
22
+ "message": "Expected string to match regular expression",
23
23
  "ref": {
24
24
  "name": "email",
25
25
  },
26
- "type": "52",
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": "51",
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
- "61": "Expected union value",
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 '^[a-z0-9!#$%&'*+/=?^_\`{|}~-]+(?:\\\\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$'",
106
+ "message": "Expected string to match regular expression",
107
107
  "ref": {
108
108
  "name": "email",
109
109
  },
110
- "type": "52",
110
+ "type": "48",
111
111
  "types": {
112
- "52": "Expected string to match '^[a-z0-9!#$%&'*+/=?^_\`{|}~-]+(?:\\\\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$'",
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
- "53": "Expected string",
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": "51",
150
+ "type": "52",
151
151
  "types": {
152
- "51": "Expected string length greater or equal to 8",
153
- "52": "Expected string to match '^(.*[A-Za-z\\\\d].*)$'",
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
- "53": "Expected string",
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
- "53": "Expected string",
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
+ });
@@ -1,5 +1,5 @@
1
1
  import { typeboxResolver } from '..';
2
- import { schema, validData, invalidData, fields } from './__fixtures__/data';
2
+ import { fields, invalidData, schema, validData } from './__fixtures__/data';
3
3
 
4
4
  const shouldUseNativeValidation = false;
5
5
 
@@ -1,7 +1,8 @@
1
- import { appendErrors, FieldError, FieldErrors } from 'react-hook-form';
2
1
  import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
2
+ import { TypeCheck } from '@sinclair/typebox/compiler';
3
+ import { Value, type ValueError } from '@sinclair/typebox/value';
4
+ import { FieldError, FieldErrors, appendErrors } from 'react-hook-form';
3
5
  import type { Resolver } from './types';
4
- import { Value, ValueError } from '@sinclair/typebox/value';
5
6
 
6
7
  const parseErrorSchema = (
7
8
  _errors: ValueError[],
@@ -40,7 +41,11 @@ const parseErrorSchema = (
40
41
 
41
42
  export const typeboxResolver: Resolver =
42
43
  (schema) => async (values, _, options) => {
43
- const errors = Array.from(Value.Errors(schema, values));
44
+ const errors = Array.from(
45
+ schema instanceof TypeCheck
46
+ ? schema.Errors(values)
47
+ : Value.Errors(schema, values),
48
+ );
44
49
 
45
50
  options.shouldUseNativeValidation && validateFieldsNatively({}, options);
46
51