@hookform/resolvers 4.0.0 → 4.1.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 +35 -2
- package/ajv/dist/ajv.d.ts +20 -0
- 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/src/ajv.ts +20 -0
- package/arktype/dist/arktype.d.ts +20 -2
- 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/index.d.ts +0 -1
- package/arktype/src/__tests__/Form.tsx +32 -6
- package/arktype/src/__tests__/__fixtures__/data.ts +1 -1
- package/arktype/src/arktype.ts +29 -9
- package/arktype/src/index.ts +0 -1
- package/class-validator/dist/class-validator.d.ts +29 -2
- package/class-validator/dist/class-validator.js +1 -1
- package/class-validator/dist/class-validator.js.map +1 -1
- package/class-validator/dist/class-validator.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs +1 -1
- package/class-validator/dist/class-validator.modern.mjs.map +1 -1
- package/class-validator/dist/class-validator.module.js +1 -1
- package/class-validator/dist/class-validator.module.js.map +1 -1
- package/class-validator/dist/class-validator.umd.js +1 -1
- package/class-validator/dist/class-validator.umd.js.map +1 -1
- package/class-validator/dist/index.d.ts +0 -1
- package/class-validator/src/__tests__/Form.tsx +26 -0
- package/class-validator/src/__tests__/__fixtures__/data.ts +1 -1
- package/class-validator/src/__tests__/class-validator.ts +21 -10
- package/class-validator/src/class-validator.ts +46 -12
- package/class-validator/src/index.ts +0 -1
- package/computed-types/dist/computed-types.d.ts +18 -2
- package/computed-types/dist/computed-types.js.map +1 -1
- package/computed-types/dist/computed-types.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs +1 -1
- package/computed-types/dist/computed-types.modern.mjs.map +1 -1
- package/computed-types/dist/computed-types.module.js +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/index.d.ts +0 -1
- package/computed-types/src/__tests__/Form-native-validation.tsx +2 -4
- package/computed-types/src/__tests__/Form.tsx +30 -6
- package/computed-types/src/__tests__/__fixtures__/data.ts +1 -1
- package/computed-types/src/__tests__/computed-types.ts +2 -0
- package/computed-types/src/computed-types.ts +24 -7
- package/computed-types/src/index.ts +0 -1
- package/effect-ts/dist/effect-ts.d.ts +19 -2
- 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.map +1 -1
- package/effect-ts/dist/index.d.ts +0 -1
- package/effect-ts/src/__tests__/Form.tsx +30 -4
- package/effect-ts/src/__tests__/__fixtures__/data.ts +1 -1
- package/effect-ts/src/effect-ts.ts +30 -8
- package/effect-ts/src/index.ts +0 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.d.ts +21 -0
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.js.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.mjs +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.modern.mjs.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.module.js.map +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js +1 -1
- package/fluentvalidation-ts/dist/fluentvalidation-ts.umd.js.map +1 -1
- package/fluentvalidation-ts/src/fluentvalidation-ts.ts +24 -3
- package/io-ts/dist/errorsToRecord.d.ts +5 -1
- package/io-ts/dist/index.d.ts +0 -1
- package/io-ts/dist/io-ts.d.ts +19 -2
- 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.map +1 -1
- package/io-ts/src/__tests__/Form-native-validation.tsx +4 -11
- package/io-ts/src/__tests__/Form.tsx +30 -6
- package/io-ts/src/__tests__/__fixtures__/data.ts +2 -19
- package/io-ts/src/errorsToRecord.ts +4 -1
- package/io-ts/src/index.ts +0 -1
- package/io-ts/src/io-ts.ts +47 -25
- package/joi/dist/joi.d.ts +17 -0
- package/joi/dist/joi.js.map +1 -1
- package/joi/dist/joi.modern.mjs.map +1 -1
- package/joi/dist/joi.module.js.map +1 -1
- package/joi/dist/joi.umd.js.map +1 -1
- package/joi/src/joi.ts +17 -0
- package/nope/dist/nope.d.ts +17 -0
- package/nope/dist/nope.js +1 -1
- package/nope/dist/nope.js.map +1 -1
- package/nope/dist/nope.mjs +1 -1
- package/nope/dist/nope.modern.mjs.map +1 -1
- package/nope/dist/nope.module.js +1 -1
- package/nope/dist/nope.module.js.map +1 -1
- package/nope/dist/nope.umd.js +1 -1
- package/nope/dist/nope.umd.js.map +1 -1
- package/nope/src/nope.ts +17 -0
- package/package.json +4 -1
- package/standard-schema/dist/index.d.ts +0 -1
- package/standard-schema/dist/standard-schema.d.ts +25 -2
- package/standard-schema/dist/standard-schema.js +1 -1
- package/standard-schema/dist/standard-schema.js.map +1 -1
- package/standard-schema/dist/standard-schema.mjs +1 -1
- package/standard-schema/dist/standard-schema.modern.mjs +1 -1
- package/standard-schema/dist/standard-schema.modern.mjs.map +1 -1
- package/standard-schema/dist/standard-schema.module.js +1 -1
- package/standard-schema/dist/standard-schema.module.js.map +1 -1
- package/standard-schema/dist/standard-schema.umd.js +1 -1
- package/standard-schema/dist/standard-schema.umd.js.map +1 -1
- package/standard-schema/src/__tests__/Form.tsx +32 -6
- package/standard-schema/src/__tests__/__fixtures__/data.ts +44 -21
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +198 -11
- package/standard-schema/src/__tests__/standard-schema.ts +30 -2
- package/standard-schema/src/index.ts +0 -1
- package/standard-schema/src/standard-schema.ts +54 -11
- package/superstruct/dist/index.d.ts +0 -1
- package/superstruct/dist/superstruct.d.ts +22 -2
- package/superstruct/dist/superstruct.js.map +1 -1
- package/superstruct/dist/superstruct.mjs +1 -1
- package/superstruct/dist/superstruct.modern.mjs +1 -1
- package/superstruct/dist/superstruct.modern.mjs.map +1 -1
- package/superstruct/dist/superstruct.module.js +1 -1
- package/superstruct/dist/superstruct.module.js.map +1 -1
- package/superstruct/dist/superstruct.umd.js.map +1 -1
- package/superstruct/src/__tests__/Form.tsx +26 -0
- package/superstruct/src/__tests__/__fixtures__/data.ts +1 -1
- package/superstruct/src/index.ts +0 -1
- package/superstruct/src/superstruct.ts +31 -9
- package/typanion/dist/index.d.ts +0 -1
- package/typanion/dist/typanion.d.ts +18 -2
- package/typanion/dist/typanion.js.map +1 -1
- package/typanion/dist/typanion.mjs +1 -1
- package/typanion/dist/typanion.modern.mjs +1 -1
- package/typanion/dist/typanion.modern.mjs.map +1 -1
- package/typanion/dist/typanion.module.js +1 -1
- package/typanion/dist/typanion.module.js.map +1 -1
- package/typanion/dist/typanion.umd.js.map +1 -1
- package/typanion/src/__tests__/Form-native-validation.tsx +6 -14
- package/typanion/src/__tests__/Form.tsx +32 -8
- package/typanion/src/__tests__/__fixtures__/data.ts +9 -9
- package/typanion/src/__tests__/typanion.ts +3 -3
- package/typanion/src/index.ts +0 -1
- package/typanion/src/typanion.ts +32 -9
- package/typebox/dist/index.d.ts +0 -1
- package/typebox/dist/typebox.d.ts +20 -2
- 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/src/__tests__/Form-compiler.tsx +4 -8
- package/typebox/src/__tests__/Form.tsx +32 -6
- package/typebox/src/__tests__/__fixtures__/data.ts +1 -1
- package/typebox/src/index.ts +0 -1
- package/typebox/src/typebox.ts +33 -7
- package/typeschema/dist/typeschema.d.ts +17 -0
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/src/typeschema.ts +17 -0
- package/valibot/dist/index.d.ts +0 -1
- package/valibot/dist/valibot.d.ts +31 -2
- 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.map +1 -1
- package/valibot/src/__tests__/Form.tsx +26 -0
- package/valibot/src/__tests__/__fixtures__/data.ts +7 -3
- package/valibot/src/index.ts +0 -1
- package/valibot/src/valibot.ts +52 -5
- package/vest/dist/vest.d.ts +18 -0
- 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/vest.ts +18 -0
- package/vine/dist/index.d.ts +0 -1
- package/vine/dist/vine.d.ts +25 -2
- package/vine/dist/vine.js +1 -1
- package/vine/dist/vine.js.map +1 -1
- package/vine/dist/vine.mjs +1 -1
- package/vine/dist/vine.modern.mjs +1 -1
- package/vine/dist/vine.modern.mjs.map +1 -1
- package/vine/dist/vine.module.js +1 -1
- package/vine/dist/vine.module.js.map +1 -1
- package/vine/dist/vine.umd.js +1 -1
- package/vine/dist/vine.umd.js.map +1 -1
- package/vine/src/__tests__/Form-native-validation.tsx +2 -4
- package/vine/src/__tests__/Form.tsx +30 -6
- package/vine/src/__tests__/__fixtures__/data.ts +1 -1
- package/vine/src/index.ts +0 -1
- package/vine/src/vine.ts +37 -9
- package/yup/dist/yup.d.ts +18 -7
- 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/yup.ts +21 -10
- package/zod/dist/index.d.ts +0 -1
- package/zod/dist/zod.d.ts +25 -2
- 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.tsx +30 -6
- package/zod/src/__tests__/__fixtures__/data.ts +3 -3
- package/zod/src/index.ts +0 -1
- package/zod/src/zod.ts +39 -8
- package/arktype/dist/types.d.ts +0 -9
- package/arktype/src/types.ts +0 -18
- package/class-validator/dist/types.d.ts +0 -12
- package/class-validator/src/types.ts +0 -16
- package/computed-types/dist/types.d.ts +0 -2
- package/computed-types/src/types.ts +0 -13
- package/effect-ts/dist/types.d.ts +0 -4
- package/effect-ts/src/types.ts +0 -12
- package/io-ts/dist/types.d.ts +0 -7
- package/io-ts/src/types.ts +0 -18
- package/standard-schema/dist/types.d.ts +0 -3
- package/standard-schema/src/types.ts +0 -10
- package/superstruct/dist/types.d.ts +0 -11
- package/superstruct/src/types.ts +0 -20
- package/typanion/dist/types.d.ts +0 -9
- package/typanion/src/types.ts +0 -20
- package/typebox/dist/types.d.ts +0 -4
- package/typebox/src/types.ts +0 -11
- package/valibot/dist/types.d.ts +0 -13
- package/valibot/src/types.ts +0 -34
- package/vine/dist/types.d.ts +0 -6
- package/vine/src/types.ts +0 -13
- package/zod/dist/types.d.ts +0 -13
- package/zod/src/types.ts +0 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typanion.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {
|
|
1
|
+
{"version":3,"file":"typanion.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\nimport * as t from 'typanion';\n\nfunction parseErrors(errors: string[], parsedErrors: FieldErrors = {}) {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using Typanion schema validation\n * @param {t.StrictValidator<TFieldValues, TFieldValues>} schema - The Typanion schema to validate against\n * @param {Pick<t.ValidationState, 'coercions' | 'coercion'>} [schemaOptions] - Optional Typanion validation options\n * @returns {Resolver<t.InferType<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = t.isObject({\n * name: t.isString(),\n * age: t.isInteger()\n * });\n *\n * useForm({\n * resolver: typanionResolver(schema)\n * });\n */\nexport function typanionResolver<TFieldValues extends FieldValues>(\n schema: t.StrictValidator<TFieldValues, TFieldValues>,\n schemaOptions: Pick<t.ValidationState, 'coercions' | 'coercion'> = {},\n): Resolver<t.InferType<typeof schema>> {\n return (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = schema(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n schemaOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n}\n"],"names":["schema","schemaOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"8DAuCgB,SACdA,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAAmE,CAAE,GAE7DC,SAAAA,EAAQC,EAAGC,GACjB,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,CAAE,EACF,CACEC,OAAQJ,GAEVJ,IAGES,EA9CV,SAAqBD,EAAkBC,GACrC,gBADqCA,IAAAA,EAA4B,IAC1DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,QAAAA,GAGKN,CACT,EAAGF,EACL,CAiCyBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,EAAAA,uBAAuBZ,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,CAAA,IAGpB,CAAEP,OAAQ,GAAIO,OAAQc,EAAYA,aAACb,EAAcN,GAC1D,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function o(o,i){return void 0===i&&(i={}),function(n,s,t){var u=[],a=o(n,Object.assign({},{errors:u},i)),c=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.indexOf(":"),i=e.slice(1,o),n=e.slice(o+1).trim();return r[i]={message:n},r},e)}(u);return a?(t.shouldUseNativeValidation&&r(c,t),{values:n,errors:{}}):{values:{},errors:e(c,t)}}}export{o as typanionResolver};
|
|
2
2
|
//# sourceMappingURL=typanion.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function s(s,o={}){return(t,n,i)=>{const u=[],c=s(t,Object.assign({},{errors:u},o)),a=function(r,e={}){return r.reduce((r,e)=>{const s=e.indexOf(":"),o=e.slice(1,s),t=e.slice(s+1).trim();return r[o]={message:t},r},e)}(u);return c?(i.shouldUseNativeValidation&&r(a,i),{values:t,errors:{}}):{values:{},errors:e(a,i)}}}export{s as typanionResolver};
|
|
2
2
|
//# sourceMappingURL=typanion.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typanion.modern.mjs","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {
|
|
1
|
+
{"version":3,"file":"typanion.modern.mjs","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\nimport * as t from 'typanion';\n\nfunction parseErrors(errors: string[], parsedErrors: FieldErrors = {}) {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using Typanion schema validation\n * @param {t.StrictValidator<TFieldValues, TFieldValues>} schema - The Typanion schema to validate against\n * @param {Pick<t.ValidationState, 'coercions' | 'coercion'>} [schemaOptions] - Optional Typanion validation options\n * @returns {Resolver<t.InferType<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = t.isObject({\n * name: t.isString(),\n * age: t.isInteger()\n * });\n *\n * useForm({\n * resolver: typanionResolver(schema)\n * });\n */\nexport function typanionResolver<TFieldValues extends FieldValues>(\n schema: t.StrictValidator<TFieldValues, TFieldValues>,\n schemaOptions: Pick<t.ValidationState, 'coercions' | 'coercion'> = {},\n): Resolver<t.InferType<typeof schema>> {\n return (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = schema(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n schemaOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n}\n"],"names":["typanionResolver","schema","schemaOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"+EAuCgB,SAAAA,EACdC,EACAC,EAAmE,IAEnE,MAAO,CAACC,EAAQC,EAAGC,KACjB,MAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,CAAA,EACA,CACEC,OAAQJ,GAEVJ,IAGES,EA9CV,SAAqBD,EAAkBC,EAA4B,CAAE,GACnE,OAAOD,EAAOE,OAAO,CAACC,EAAKC,KACzB,MAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,WAGKN,GACNF,EACL,CAiCyBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,EAAuBZ,EAAcN,GAEhC,CAAEF,SAAQO,OAAQ,KAGpB,CAAEP,OAAQ,CAAE,EAAEO,OAAQc,EAAab,EAAcN,IAE5D"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function o(o,i){return void 0===i&&(i={}),function(n,s,t){var u=[],a=o(n,Object.assign({},{errors:u},i)),c=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.indexOf(":"),i=e.slice(1,o),n=e.slice(o+1).trim();return r[i]={message:n},r},e)}(u);return a?(t.shouldUseNativeValidation&&r(c,t),{values:n,errors:{}}):{values:{},errors:e(c,t)}}}export{o as typanionResolver};
|
|
2
2
|
//# sourceMappingURL=typanion.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typanion.module.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {
|
|
1
|
+
{"version":3,"file":"typanion.module.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\nimport * as t from 'typanion';\n\nfunction parseErrors(errors: string[], parsedErrors: FieldErrors = {}) {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using Typanion schema validation\n * @param {t.StrictValidator<TFieldValues, TFieldValues>} schema - The Typanion schema to validate against\n * @param {Pick<t.ValidationState, 'coercions' | 'coercion'>} [schemaOptions] - Optional Typanion validation options\n * @returns {Resolver<t.InferType<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = t.isObject({\n * name: t.isString(),\n * age: t.isInteger()\n * });\n *\n * useForm({\n * resolver: typanionResolver(schema)\n * });\n */\nexport function typanionResolver<TFieldValues extends FieldValues>(\n schema: t.StrictValidator<TFieldValues, TFieldValues>,\n schemaOptions: Pick<t.ValidationState, 'coercions' | 'coercion'> = {},\n): Resolver<t.InferType<typeof schema>> {\n return (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = schema(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n schemaOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n}\n"],"names":["typanionResolver","schema","schemaOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"+EAuCgB,SAAAA,EACdC,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAAmE,CAAE,GAE7DC,SAAAA,EAAQC,EAAGC,GACjB,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,CAAE,EACF,CACEC,OAAQJ,GAEVJ,IAGES,EA9CV,SAAqBD,EAAkBC,GACrC,gBADqCA,IAAAA,EAA4B,IAC1DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,QAAAA,GAGKN,CACT,EAAGF,EACL,CAiCyBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,EAAuBZ,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,CAAA,IAGpB,CAAEP,OAAQ,GAAIO,OAAQc,EAAab,EAAcN,GAC1D,CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typanion.umd.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {
|
|
1
|
+
{"version":3,"file":"typanion.umd.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n} from 'react-hook-form';\nimport * as t from 'typanion';\n\nfunction parseErrors(errors: string[], parsedErrors: FieldErrors = {}) {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n}\n\n/**\n * Creates a resolver for react-hook-form using Typanion schema validation\n * @param {t.StrictValidator<TFieldValues, TFieldValues>} schema - The Typanion schema to validate against\n * @param {Pick<t.ValidationState, 'coercions' | 'coercion'>} [schemaOptions] - Optional Typanion validation options\n * @returns {Resolver<t.InferType<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = t.isObject({\n * name: t.isString(),\n * age: t.isInteger()\n * });\n *\n * useForm({\n * resolver: typanionResolver(schema)\n * });\n */\nexport function typanionResolver<TFieldValues extends FieldValues>(\n schema: t.StrictValidator<TFieldValues, TFieldValues>,\n schemaOptions: Pick<t.ValidationState, 'coercions' | 'coercion'> = {},\n): Resolver<t.InferType<typeof schema>> {\n return (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = schema(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n schemaOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n}\n"],"names":["schema","schemaOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"iVAuCgB,SACdA,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAAmE,CAAE,GAE7DC,SAAAA,EAAQC,EAAGC,GACjB,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,CAAE,EACF,CACEC,OAAQJ,GAEVJ,IAGES,EA9CV,SAAqBD,EAAkBC,GACrC,gBADqCA,IAAAA,EAA4B,IAC1DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,QAAAA,GAGKN,CACT,EAAGF,EACL,CAiCyBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,EAAAA,uBAAuBZ,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,CAAA,IAGpB,CAAEP,OAAQ,GAAIO,OAAQc,EAAYA,aAACb,EAAcN,GAC1D,CACF"}
|
|
@@ -9,22 +9,14 @@ const ERROR_MESSAGE =
|
|
|
9
9
|
'Expected to have a length of at least 1 elements (got 0)';
|
|
10
10
|
|
|
11
11
|
const schema = t.isObject({
|
|
12
|
-
username: t.
|
|
13
|
-
password: t.
|
|
12
|
+
username: t.cascade(t.isString(), [t.hasMinLength(1)]),
|
|
13
|
+
password: t.cascade(t.isString(), [t.hasMinLength(1)]),
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
interface Props {
|
|
23
|
-
onSubmit: (data: FormData) => void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function TestComponent({ onSubmit }: Props) {
|
|
27
|
-
const { register, handleSubmit } = useForm<FormData>({
|
|
16
|
+
function TestComponent({
|
|
17
|
+
onSubmit,
|
|
18
|
+
}: { onSubmit: (data: t.InferType<typeof schema>) => void }) {
|
|
19
|
+
const { register, handleSubmit } = useForm({
|
|
28
20
|
resolver: typanionResolver(schema),
|
|
29
21
|
shouldUseNativeValidation: true,
|
|
30
22
|
});
|
|
@@ -6,8 +6,8 @@ import * as t from 'typanion';
|
|
|
6
6
|
import { typanionResolver } from '..';
|
|
7
7
|
|
|
8
8
|
const schema = t.isObject({
|
|
9
|
-
username: t.
|
|
10
|
-
password: t.
|
|
9
|
+
username: t.cascade(t.isString(), [t.hasMinLength(1)]),
|
|
10
|
+
password: t.cascade(t.isString(), [t.hasMinLength(1)]),
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
interface FormData {
|
|
@@ -16,16 +16,14 @@ interface FormData {
|
|
|
16
16
|
password: string;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
onSubmit
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function TestComponent({ onSubmit }: Props) {
|
|
19
|
+
function TestComponent({
|
|
20
|
+
onSubmit,
|
|
21
|
+
}: { onSubmit: (data: t.InferType<typeof schema>) => void }) {
|
|
24
22
|
const {
|
|
25
23
|
register,
|
|
26
24
|
formState: { errors },
|
|
27
25
|
handleSubmit,
|
|
28
|
-
} = useForm
|
|
26
|
+
} = useForm({
|
|
29
27
|
resolver: typanionResolver(schema), // Useful to check TypeScript regressions
|
|
30
28
|
});
|
|
31
29
|
|
|
@@ -57,3 +55,29 @@ test("form's validation with Typanion and TypeScript's integration", async () =>
|
|
|
57
55
|
).toHaveLength(2);
|
|
58
56
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
59
57
|
});
|
|
58
|
+
|
|
59
|
+
export function TestComponentManualType({
|
|
60
|
+
onSubmit,
|
|
61
|
+
}: {
|
|
62
|
+
onSubmit: (data: FormData) => void;
|
|
63
|
+
}) {
|
|
64
|
+
const {
|
|
65
|
+
register,
|
|
66
|
+
handleSubmit,
|
|
67
|
+
formState: { errors },
|
|
68
|
+
} = useForm<t.InferType<typeof schema>, undefined, FormData>({
|
|
69
|
+
resolver: typanionResolver(schema), // Useful to check TypeScript regressions
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
74
|
+
<input {...register('username')} />
|
|
75
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
76
|
+
|
|
77
|
+
<input {...register('password')} />
|
|
78
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
79
|
+
|
|
80
|
+
<button type="submit">submit</button>
|
|
81
|
+
</form>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
2
|
import * as t from 'typanion';
|
|
3
3
|
|
|
4
|
-
export const
|
|
5
|
-
username: t.
|
|
4
|
+
export const schema = t.isObject({
|
|
5
|
+
username: t.cascade(t.isString(), [
|
|
6
6
|
t.matchesRegExp(/^\w+$/),
|
|
7
7
|
t.hasMinLength(2),
|
|
8
8
|
t.hasMaxLength(30),
|
|
9
9
|
]),
|
|
10
|
-
password: t.
|
|
10
|
+
password: t.cascade(t.isString(), [
|
|
11
11
|
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character
|
|
12
12
|
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character
|
|
13
13
|
t.matchesRegExp(new RegExp('.*\\d.*')), // one number
|
|
@@ -16,7 +16,7 @@ export const isSchema = t.isObject({
|
|
|
16
16
|
), // one special character
|
|
17
17
|
t.hasMinLength(8), // Must be at least 8 characters in length
|
|
18
18
|
]),
|
|
19
|
-
repeatPassword: t.
|
|
19
|
+
repeatPassword: t.cascade(t.isString(), [
|
|
20
20
|
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character
|
|
21
21
|
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character
|
|
22
22
|
t.matchesRegExp(new RegExp('.*\\d.*')), // one number
|
|
@@ -26,16 +26,16 @@ export const isSchema = t.isObject({
|
|
|
26
26
|
t.hasMinLength(8), // Must be at least 8 characters in length
|
|
27
27
|
]),
|
|
28
28
|
accessToken: t.isString(),
|
|
29
|
-
birthYear: t.
|
|
29
|
+
birthYear: t.cascade(t.isNumber(), [
|
|
30
30
|
t.isInteger(),
|
|
31
31
|
t.isInInclusiveRange(1900, 2013),
|
|
32
32
|
]),
|
|
33
|
-
email: t.
|
|
33
|
+
email: t.cascade(t.isString(), [t.matchesRegExp(/^\S+@\S+$/)]),
|
|
34
34
|
tags: t.isArray(t.isString()),
|
|
35
35
|
enabled: t.isBoolean(),
|
|
36
36
|
like: t.isObject({
|
|
37
|
-
id: t.
|
|
38
|
-
name: t.
|
|
37
|
+
id: t.cascade(t.isNumber(), [t.isInteger(), t.isPositive()]),
|
|
38
|
+
name: t.cascade(t.isString(), [t.hasMinLength(4)]),
|
|
39
39
|
}),
|
|
40
40
|
});
|
|
41
41
|
|
|
@@ -60,7 +60,7 @@ export const invalidData = {
|
|
|
60
60
|
birthYear: 'birthYear',
|
|
61
61
|
like: { id: 'z' },
|
|
62
62
|
tags: [1, 2, 3],
|
|
63
|
-
}
|
|
63
|
+
} as any as t.InferType<typeof schema>;
|
|
64
64
|
|
|
65
65
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
66
66
|
username: {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { typanionResolver } from '..';
|
|
2
|
-
import { fields, invalidData,
|
|
2
|
+
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
3
3
|
|
|
4
4
|
const tmpObj = {
|
|
5
|
-
validate:
|
|
5
|
+
validate: schema,
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
const shouldUseNativeValidation = false;
|
|
@@ -25,7 +25,7 @@ describe('typanionResolver', () => {
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
it('should return a single error from typanionResolver when validation fails', async () => {
|
|
28
|
-
const result = await typanionResolver(
|
|
28
|
+
const result = await typanionResolver(schema)(invalidData, undefined, {
|
|
29
29
|
fields,
|
|
30
30
|
shouldUseNativeValidation,
|
|
31
31
|
});
|
package/typanion/src/index.ts
CHANGED
package/typanion/src/typanion.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
2
|
+
import type {
|
|
3
|
+
FieldError,
|
|
4
|
+
FieldErrors,
|
|
5
|
+
FieldValues,
|
|
6
|
+
Resolver,
|
|
7
|
+
} from 'react-hook-form';
|
|
8
|
+
import * as t from 'typanion';
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
function parseErrors(errors: string[], parsedErrors: FieldErrors = {}) {
|
|
6
11
|
return errors.reduce((acc, error) => {
|
|
7
12
|
const fieldIndex = error.indexOf(':');
|
|
8
13
|
|
|
@@ -15,20 +20,37 @@ const parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {
|
|
|
15
20
|
|
|
16
21
|
return acc;
|
|
17
22
|
}, parsedErrors);
|
|
18
|
-
}
|
|
23
|
+
}
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Creates a resolver for react-hook-form using Typanion schema validation
|
|
27
|
+
* @param {t.StrictValidator<TFieldValues, TFieldValues>} schema - The Typanion schema to validate against
|
|
28
|
+
* @param {Pick<t.ValidationState, 'coercions' | 'coercion'>} [schemaOptions] - Optional Typanion validation options
|
|
29
|
+
* @returns {Resolver<t.InferType<typeof schema>>} A resolver function compatible with react-hook-form
|
|
30
|
+
* @example
|
|
31
|
+
* const schema = t.isObject({
|
|
32
|
+
* name: t.isString(),
|
|
33
|
+
* age: t.isInteger()
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* useForm({
|
|
37
|
+
* resolver: typanionResolver(schema)
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
export function typanionResolver<TFieldValues extends FieldValues>(
|
|
41
|
+
schema: t.StrictValidator<TFieldValues, TFieldValues>,
|
|
42
|
+
schemaOptions: Pick<t.ValidationState, 'coercions' | 'coercion'> = {},
|
|
43
|
+
): Resolver<t.InferType<typeof schema>> {
|
|
44
|
+
return (values, _, options) => {
|
|
23
45
|
const rawErrors: string[] = [];
|
|
24
|
-
const isValid =
|
|
46
|
+
const isValid = schema(
|
|
25
47
|
values,
|
|
26
48
|
Object.assign(
|
|
27
49
|
{},
|
|
28
50
|
{
|
|
29
51
|
errors: rawErrors,
|
|
30
52
|
},
|
|
31
|
-
|
|
53
|
+
schemaOptions,
|
|
32
54
|
),
|
|
33
55
|
);
|
|
34
56
|
const parsedErrors = parseErrors(rawErrors);
|
|
@@ -42,3 +64,4 @@ export const typanionResolver: Resolver =
|
|
|
42
64
|
|
|
43
65
|
return { values: {}, errors: toNestErrors(parsedErrors, options) };
|
|
44
66
|
};
|
|
67
|
+
}
|
package/typebox/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { Static, Type } from '@sinclair/typebox';
|
|
2
|
+
import { TypeCheck } from '@sinclair/typebox/compiler';
|
|
3
|
+
import { FieldValues, Resolver } from 'react-hook-form';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a resolver for react-hook-form using Typebox schema validation
|
|
6
|
+
* @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against
|
|
7
|
+
* @param {Object} options - Additional resolver configuration
|
|
8
|
+
* @param {string} [options.mode='async'] - Validation mode
|
|
9
|
+
* @returns {Resolver<Static<Schema>>} A resolver function compatible with react-hook-form
|
|
10
|
+
* @example
|
|
11
|
+
* const schema = Type.Object({
|
|
12
|
+
* name: Type.String(),
|
|
13
|
+
* age: Type.Number()
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* useForm({
|
|
17
|
+
* resolver: typeboxResolver(schema)
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
export declare function typeboxResolver<TFieldValues extends FieldValues, Schema extends ReturnType<typeof Type.Object<TFieldValues>>>(schema: Schema | TypeCheck<Schema>): Resolver<Static<Schema>>;
|
package/typebox/dist/typebox.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@hookform/resolvers"),r=require("@sinclair/typebox/compiler"),s=require("@sinclair/typebox/value"),o=require("react-hook-form")
|
|
1
|
+
var e=require("@hookform/resolvers"),r=require("@sinclair/typebox/compiler"),s=require("@sinclair/typebox/value"),o=require("react-hook-form");function a(e,r){for(var s={};e.length;){var a=e[0],t=a.type,i=a.message,l=a.path.substring(1).replace(/\//g,".");if(s[l]||(s[l]={message:i,type:""+t}),r){var n=s[l].types,u=n&&n[""+t];s[l]=o.appendErrors(l,r,s,""+t,u?[].concat(u,a.message):a.message)}e.shift()}return s}exports.typeboxResolver=function(o){return function(t,i,l){try{var n=Array.from(o instanceof r.TypeCheck?o.Errors(t):s.Value.Errors(o,t));return l.shouldUseNativeValidation&&e.validateFieldsNatively({},l),Promise.resolve(n.length?{values:{},errors:e.toNestErrors(a(n,!l.shouldUseNativeValidation&&"all"===l.criteriaMode),l)}:{errors:{},values:t})}catch(e){return Promise.reject(e)}}};
|
|
2
2
|
//# sourceMappingURL=typebox.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typebox.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 {
|
|
1
|
+
{"version":3,"file":"typebox.js","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Static, Type } from '@sinclair/typebox';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\nfunction 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\n/**\n * Creates a resolver for react-hook-form using Typebox schema validation\n * @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against\n * @param {Object} options - Additional resolver configuration\n * @param {string} [options.mode='async'] - Validation mode\n * @returns {Resolver<Static<Schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Type.Object({\n * name: Type.String(),\n * age: Type.Number()\n * });\n *\n * useForm({\n * resolver: typeboxResolver(schema)\n * });\n */\nexport function typeboxResolver<\n TFieldValues extends FieldValues,\n Schema extends ReturnType<typeof Type.Object<TFieldValues>>,\n>(schema: Schema | TypeCheck<Schema>): Resolver<Static<Schema>> {\n return 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}\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":"+IAYA,SAASA,EACPC,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,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,CACT,yBAkBgB,SAGdc,GACA,OAAcC,SAAAA,EAAQC,EAAGC,GAAO,IAC9B,IAAMjB,EAASkB,MAAMC,KACnBL,aAAkBM,YACdN,EAAOO,OAAON,GACdO,QAAMD,OAAOP,EAAQC,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEP,GAShEQ,QAAAC,QAPK1B,EAAOC,OAOL,CACLc,OAAQ,CAAE,EACVf,OAAQ2B,EAAAA,aACN9B,EACEG,GACCiB,EAAQM,2BAAsD,QAAzBN,EAAQW,cAEhDX,IAbK,CACLjB,OAAQ,GACRe,OAAAA,GAcN,CAAC,MAAAc,UAAAJ,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
package/typebox/dist/typebox.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
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";
|
|
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";function a(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}function i(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,2 +1,2 @@
|
|
|
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";
|
|
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";function a(r,o){const e={};for(;r.length;){const s=r[0],{type:a,message:i,path:n}=s,c=n.substring(1).replace(/\//g,".");if(e[c]||(e[c]={message:i,type:""+a}),o){const r=e[c].types,i=r&&r[""+a];e[c]=t(c,o,e,""+a,i?[].concat(i,s.message):s.message)}r.shift()}return e}function i(t){return async(i,n,c)=>{const l=Array.from(t instanceof e?t.Errors(i):s.Errors(t,i));return c.shouldUseNativeValidation&&r({},c),l.length?{values:{},errors:o(a(l,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)}:{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 { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {
|
|
1
|
+
{"version":3,"file":"typebox.modern.mjs","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Static, Type } from '@sinclair/typebox';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\nfunction 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\n/**\n * Creates a resolver for react-hook-form using Typebox schema validation\n * @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against\n * @param {Object} options - Additional resolver configuration\n * @param {string} [options.mode='async'] - Validation mode\n * @returns {Resolver<Static<Schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Type.Object({\n * name: Type.String(),\n * age: Type.Number()\n * });\n *\n * useForm({\n * resolver: typeboxResolver(schema)\n * });\n */\nexport function typeboxResolver<\n TFieldValues extends FieldValues,\n Schema extends ReturnType<typeof Type.Object<TFieldValues>>,\n>(schema: Schema | TypeCheck<Schema>): Resolver<Static<Schema>> {\n return 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}\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","toNestErrors","criteriaMode"],"mappings":"qOAYA,SAASA,EACPC,EACAC,GAEA,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,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,CACT,CAkBM,SAAUc,EAGdC,GACA,OAAcC,MAAAA,EAAQC,EAAGC,KACvB,MAAMlB,EAASmB,MAAMC,KACnBL,aAAkBM,EACdN,EAAOO,OAAON,GACdO,EAAMD,OAAOP,EAAQC,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAuB,CAAE,EAAEP,GAE3DlB,EAAOC,OAOL,CACLe,OAAQ,CAAA,EACRhB,OAAQ0B,EACN7B,EACEG,GACCkB,EAAQM,2BAAsD,QAAzBN,EAAQS,cAEhDT,IAbK,CACLlB,OAAQ,CAAiB,EACzBgB,UAeR"}
|
|
@@ -1,2 +1,2 @@
|
|
|
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";
|
|
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";function a(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}function i(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 { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {
|
|
1
|
+
{"version":3,"file":"typebox.module.js","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Static, Type } from '@sinclair/typebox';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\nfunction 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\n/**\n * Creates a resolver for react-hook-form using Typebox schema validation\n * @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against\n * @param {Object} options - Additional resolver configuration\n * @param {string} [options.mode='async'] - Validation mode\n * @returns {Resolver<Static<Schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Type.Object({\n * name: Type.String(),\n * age: Type.Number()\n * });\n *\n * useForm({\n * resolver: typeboxResolver(schema)\n * });\n */\nexport function typeboxResolver<\n TFieldValues extends FieldValues,\n Schema extends ReturnType<typeof Type.Object<TFieldValues>>,\n>(schema: Schema | TypeCheck<Schema>): Resolver<Static<Schema>> {\n return 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}\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":"qOAYA,SAASA,EACPC,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,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,CACT,CAkBgB,SAAAc,EAGdC,GACA,OAAcC,SAAAA,EAAQC,EAAGC,GAAO,IAC9B,IAAMlB,EAASmB,MAAMC,KACnBL,aAAkBM,EACdN,EAAOO,OAAON,GACdO,EAAMD,OAAOP,EAAQC,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAuB,CAAE,EAAEP,GAShEQ,QAAAC,QAPK3B,EAAOC,OAOL,CACLe,OAAQ,CAAE,EACVhB,OAAQ4B,EACN/B,EACEG,GACCkB,EAAQM,2BAAsD,QAAzBN,EAAQW,cAEhDX,IAbK,CACLlB,OAAQ,GACRgB,OAAAA,GAcN,CAAC,MAAAc,UAAAJ,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
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){
|
|
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){function i(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 { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {
|
|
1
|
+
{"version":3,"file":"typebox.umd.js","sources":["../src/typebox.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Static, Type } from '@sinclair/typebox';\nimport { TypeCheck } from '@sinclair/typebox/compiler';\nimport { Value, type ValueError } from '@sinclair/typebox/value';\nimport {\n FieldError,\n FieldErrors,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\nfunction 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\n/**\n * Creates a resolver for react-hook-form using Typebox schema validation\n * @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against\n * @param {Object} options - Additional resolver configuration\n * @param {string} [options.mode='async'] - Validation mode\n * @returns {Resolver<Static<Schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Type.Object({\n * name: Type.String(),\n * age: Type.Number()\n * });\n *\n * useForm({\n * resolver: typeboxResolver(schema)\n * });\n */\nexport function typeboxResolver<\n TFieldValues extends FieldValues,\n Schema extends ReturnType<typeof Type.Object<TFieldValues>>,\n>(schema: Schema | TypeCheck<Schema>): Resolver<Static<Schema>> {\n return 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}\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":"mhBAYA,SAASA,EACPC,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,QAEd,CAEAN,EAAQe,OACV,CAEA,OAAOb,CACT,mBAkBgB,SAGdc,GACA,OAAcC,SAAAA,EAAQC,EAAGC,GAAO,IAC9B,IAAMjB,EAASkB,MAAMC,KACnBL,aAAkBM,YACdN,EAAOO,OAAON,GACdO,QAAMD,OAAOP,EAAQC,IAK3B,OAFAE,EAAQM,2BAA6BC,EAAsBA,uBAAC,CAAE,EAAEP,GAShEQ,QAAAC,QAPK1B,EAAOC,OAOL,CACLc,OAAQ,CAAE,EACVf,OAAQ2B,EAAAA,aACN9B,EACEG,GACCiB,EAAQM,2BAAsD,QAAzBN,EAAQW,cAEhDX,IAbK,CACLjB,OAAQ,GACRe,OAAAA,GAcN,CAAC,MAAAc,UAAAJ,QAAAK,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -13,18 +13,14 @@ const schema = Type.Object({
|
|
|
13
13
|
|
|
14
14
|
const typecheck = TypeCompiler.Compile(schema);
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
onSubmit: (data: FormData) => void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function TestComponent({ onSubmit }: Props) {
|
|
16
|
+
function TestComponent({
|
|
17
|
+
onSubmit,
|
|
18
|
+
}: { onSubmit: (data: Static<typeof schema>) => void }) {
|
|
23
19
|
const {
|
|
24
20
|
register,
|
|
25
21
|
handleSubmit,
|
|
26
22
|
formState: { errors },
|
|
27
|
-
} = useForm
|
|
23
|
+
} = useForm({
|
|
28
24
|
resolver: typeboxResolver(typecheck), // Useful to check TypeScript regressions
|
|
29
25
|
});
|
|
30
26
|
|
|
@@ -12,16 +12,16 @@ const schema = Type.Object({
|
|
|
12
12
|
|
|
13
13
|
type FormData = Static<typeof schema> & { unusedProperty: string };
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
onSubmit
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
function TestComponent({
|
|
16
|
+
onSubmit,
|
|
17
|
+
}: {
|
|
18
|
+
onSubmit: (data: Static<typeof schema>) => void;
|
|
19
|
+
}) {
|
|
20
20
|
const {
|
|
21
21
|
register,
|
|
22
22
|
handleSubmit,
|
|
23
23
|
formState: { errors },
|
|
24
|
-
} = useForm
|
|
24
|
+
} = useForm({
|
|
25
25
|
resolver: typeboxResolver(schema), // Useful to check TypeScript regressions
|
|
26
26
|
});
|
|
27
27
|
|
|
@@ -52,3 +52,29 @@ test("form's validation with Typebox and TypeScript's integration", async () =>
|
|
|
52
52
|
|
|
53
53
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
export function TestComponentManualType({
|
|
57
|
+
onSubmit,
|
|
58
|
+
}: {
|
|
59
|
+
onSubmit: (data: FormData) => void;
|
|
60
|
+
}) {
|
|
61
|
+
const {
|
|
62
|
+
register,
|
|
63
|
+
handleSubmit,
|
|
64
|
+
formState: { errors },
|
|
65
|
+
} = useForm<Static<typeof schema>, undefined, FormData>({
|
|
66
|
+
resolver: typeboxResolver(schema), // Useful to check TypeScript regressions
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
71
|
+
<input {...register('username')} />
|
|
72
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
73
|
+
|
|
74
|
+
<input {...register('password')} />
|
|
75
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
76
|
+
|
|
77
|
+
<button type="submit">submit</button>
|
|
78
|
+
</form>
|
|
79
|
+
);
|
|
80
|
+
}
|
package/typebox/src/index.ts
CHANGED