@hookform/resolvers 4.0.0 → 4.1.1
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 +5 -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/package.json +1 -0
- package/standard-schema/src/__tests__/Form.tsx +32 -6
- package/standard-schema/src/__tests__/__fixtures__/data.ts +67 -21
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +222 -11
- package/standard-schema/src/__tests__/standard-schema.ts +49 -3
- 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":"effect-ts.modern.mjs","sources":["../src/effect-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Effect } from 'effect';\
|
|
1
|
+
{"version":3,"file":"effect-ts.modern.mjs","sources":["../src/effect-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Effect, Schema } from 'effect';\nimport { ArrayFormatter, decodeUnknown } from 'effect/ParseResult';\nimport { ParseOptions } from 'effect/SchemaAST';\nimport {\n type FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\n/**\n * Creates a resolver for react-hook-form using Effect.ts schema validation\n * @param {Schema.Schema<TFieldValues, I>} schema - The Effect.ts schema to validate against\n * @param {ParseOptions} [schemaOptions] - Optional Effect.ts validation options\n * @returns {Resolver<Schema.Schema.Type<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Schema.Struct({\n * name: Schema.String,\n * age: Schema.Number\n * });\n *\n * useForm({\n * resolver: effectTsResolver(schema)\n * });\n */\nexport function effectTsResolver<TFieldValues extends FieldValues, I>(\n schema: Schema.Schema<TFieldValues, I>,\n schemaOptions: ParseOptions = { errors: 'all', onExcessProperty: 'ignore' },\n): Resolver<Schema.Schema.Type<typeof schema>> {\n return (values, _, options) => {\n return decodeUnknown(\n schema,\n schemaOptions,\n )(values).pipe(\n Effect.catchAll((parseIssue) =>\n Effect.flip(ArrayFormatter.formatIssue(parseIssue)),\n ),\n Effect.mapError((issues) => {\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n const errors = issues.reduce(\n (acc, error) => {\n const key = error.path.join('.');\n\n if (!acc[key]) {\n acc[key] = { message: error.message, type: error._tag };\n }\n\n if (validateAllFieldCriteria) {\n const types = acc[key].types;\n const messages = types && types[String(error._tag)];\n\n acc[key] = appendErrors(\n key,\n validateAllFieldCriteria,\n acc,\n error._tag,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return acc;\n },\n {} as Record<string, FieldError>,\n );\n\n return toNestErrors(errors, options);\n }),\n Effect.tap(() =>\n Effect.sync(\n () =>\n options.shouldUseNativeValidation &&\n validateFieldsNatively({}, options),\n ),\n ),\n Effect.match({\n onFailure: (errors) => ({ errors, values: {} }),\n onSuccess: (result) => ({ errors: {}, values: result }),\n }),\n Effect.runPromise,\n );\n };\n}\n"],"names":["effectTsResolver","schema","schemaOptions","errors","onExcessProperty","values","_","options","decodeUnknown","pipe","Effect","catchAll","parseIssue","flip","ArrayFormatter","formatIssue","mapError","issues","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","reduce","acc","error","key","path","join","message","type","_tag","types","messages","String","appendErrors","concat","toNestErrors","tap","sync","validateFieldsNatively","match","onFailure","onSuccess","result","runPromise"],"mappings":"8OA0BgBA,EACdC,EACAC,EAA8B,CAAEC,OAAQ,MAAOC,iBAAkB,WAEjE,MAAO,CAACC,EAAQC,EAAGC,IACVC,EACLP,EACAC,EAFKM,CAGLH,GAAQI,KACRC,EAAOC,SAAUC,GACfF,EAAOG,KAAKC,EAAeC,YAAYH,KAEzCF,EAAOM,SAAUC,IACf,MAAMC,GACHX,EAAQY,2BAAsD,QAAzBZ,EAAQa,aAE1CjB,EAASc,EAAOI,OACpB,CAACC,EAAKC,KACJ,MAAMC,EAAMD,EAAME,KAAKC,KAAK,KAM5B,GAJKJ,EAAIE,KACPF,EAAIE,GAAO,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG/CX,EAA0B,CAC5B,MAAMY,EAAQR,EAAIE,GAAKM,MACjBC,EAAWD,GAASA,EAAME,OAAOT,EAAMM,OAE7CP,EAAIE,GAAOS,EACTT,EACAN,EACAI,EACAC,EAAMM,KACNE,EACK,GAAgBG,OAAOH,EAAsBR,EAAMI,SACpDJ,EAAMI,QAEd,CAEA,OAAOL,GAET,CAAgC,GAGlC,OAAOa,EAAahC,EAAQI,EAAO,GAErCG,EAAO0B,IAAI,IACT1B,EAAO2B,KACL,IACE9B,EAAQY,2BACRmB,EAAuB,CAAA,EAAI/B,KAGjCG,EAAO6B,MAAM,CACXC,UAAYrC,IAAY,CAAEA,SAAQE,OAAQ,KAC1CoC,UAAYC,IAAY,CAAEvC,OAAQ,CAAE,EAAEE,OAAQqC,MAEhDhC,EAAOiC,WAGb"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{Effect as t}from"effect";import{decodeUnknown as o,ArrayFormatter as n}from"effect/ParseResult";import{appendErrors as a}from"react-hook-form";
|
|
1
|
+
import{toNestErrors as r,validateFieldsNatively as e}from"@hookform/resolvers";import{Effect as t}from"effect";import{decodeUnknown as o,ArrayFormatter as n}from"effect/ParseResult";import{appendErrors as a}from"react-hook-form";function i(i,s){return void 0===s&&(s={errors:"all",onExcessProperty:"ignore"}),function(u,c,f){return o(i,s)(u).pipe(t.catchAll(function(r){return t.flip(n.formatIssue(r))}),t.mapError(function(e){var t=!f.shouldUseNativeValidation&&"all"===f.criteriaMode,o=e.reduce(function(r,e){var o=e.path.join(".");if(r[o]||(r[o]={message:e.message,type:e._tag}),t){var n=r[o].types,i=n&&n[String(e._tag)];r[o]=a(o,t,r,e._tag,i?[].concat(i,e.message):e.message)}return r},{});return r(o,f)}),t.tap(function(){return t.sync(function(){return f.shouldUseNativeValidation&&e({},f)})}),t.match({onFailure:function(r){return{errors:r,values:{}}},onSuccess:function(r){return{errors:{},values:r}}}),t.runPromise)}}export{i as effectTsResolver};
|
|
2
2
|
//# sourceMappingURL=effect-ts.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect-ts.module.js","sources":["../src/effect-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Effect } from 'effect';\
|
|
1
|
+
{"version":3,"file":"effect-ts.module.js","sources":["../src/effect-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Effect, Schema } from 'effect';\nimport { ArrayFormatter, decodeUnknown } from 'effect/ParseResult';\nimport { ParseOptions } from 'effect/SchemaAST';\nimport {\n type FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\n/**\n * Creates a resolver for react-hook-form using Effect.ts schema validation\n * @param {Schema.Schema<TFieldValues, I>} schema - The Effect.ts schema to validate against\n * @param {ParseOptions} [schemaOptions] - Optional Effect.ts validation options\n * @returns {Resolver<Schema.Schema.Type<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Schema.Struct({\n * name: Schema.String,\n * age: Schema.Number\n * });\n *\n * useForm({\n * resolver: effectTsResolver(schema)\n * });\n */\nexport function effectTsResolver<TFieldValues extends FieldValues, I>(\n schema: Schema.Schema<TFieldValues, I>,\n schemaOptions: ParseOptions = { errors: 'all', onExcessProperty: 'ignore' },\n): Resolver<Schema.Schema.Type<typeof schema>> {\n return (values, _, options) => {\n return decodeUnknown(\n schema,\n schemaOptions,\n )(values).pipe(\n Effect.catchAll((parseIssue) =>\n Effect.flip(ArrayFormatter.formatIssue(parseIssue)),\n ),\n Effect.mapError((issues) => {\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n const errors = issues.reduce(\n (acc, error) => {\n const key = error.path.join('.');\n\n if (!acc[key]) {\n acc[key] = { message: error.message, type: error._tag };\n }\n\n if (validateAllFieldCriteria) {\n const types = acc[key].types;\n const messages = types && types[String(error._tag)];\n\n acc[key] = appendErrors(\n key,\n validateAllFieldCriteria,\n acc,\n error._tag,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return acc;\n },\n {} as Record<string, FieldError>,\n );\n\n return toNestErrors(errors, options);\n }),\n Effect.tap(() =>\n Effect.sync(\n () =>\n options.shouldUseNativeValidation &&\n validateFieldsNatively({}, options),\n ),\n ),\n Effect.match({\n onFailure: (errors) => ({ errors, values: {} }),\n onSuccess: (result) => ({ errors: {}, values: result }),\n }),\n Effect.runPromise,\n );\n };\n}\n"],"names":["effectTsResolver","schema","schemaOptions","errors","onExcessProperty","values","_","options","decodeUnknown","pipe","Effect","catchAll","parseIssue","flip","ArrayFormatter","formatIssue","mapError","issues","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","reduce","acc","error","key","path","join","message","type","_tag","types","messages","String","appendErrors","concat","toNestErrors","tap","sync","validateFieldsNatively","match","onFailure","onSuccess","result","runPromise"],"mappings":"qOA0BgB,SAAAA,EACdC,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAA8B,CAAEC,OAAQ,MAAOC,iBAAkB,WAE1D,SAACC,EAAQC,EAAGC,GACjB,OAAOC,EACLP,EACAC,EAFKM,CAGLH,GAAQI,KACRC,EAAOC,SAAS,SAACC,GACf,OAAAF,EAAOG,KAAKC,EAAeC,YAAYH,GAAY,GAErDF,EAAOM,SAAS,SAACC,GACf,IAAMC,GACHX,EAAQY,2BAAsD,QAAzBZ,EAAQa,aAE1CjB,EAASc,EAAOI,OACpB,SAACC,EAAKC,GACJ,IAAMC,EAAMD,EAAME,KAAKC,KAAK,KAM5B,GAJKJ,EAAIE,KACPF,EAAIE,GAAO,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG/CX,EAA0B,CAC5B,IAAMY,EAAQR,EAAIE,GAAKM,MACjBC,EAAWD,GAASA,EAAME,OAAOT,EAAMM,OAE7CP,EAAIE,GAAOS,EACTT,EACAN,EACAI,EACAC,EAAMM,KACNE,EACK,GAAgBG,OAAOH,EAAsBR,EAAMI,SACpDJ,EAAMI,QAEd,CAEA,OAAOL,CACT,EACA,CAAgC,GAGlC,OAAOa,EAAahC,EAAQI,EAC9B,GACAG,EAAO0B,IAAI,WAAA,OACT1B,EAAO2B,KACL,kBACE9B,EAAQY,2BACRmB,EAAuB,GAAI/B,EAAQ,EACtC,GAEHG,EAAO6B,MAAM,CACXC,UAAW,SAACrC,GAAM,MAAM,CAAEA,OAAAA,EAAQE,OAAQ,GAAI,EAC9CoC,UAAW,SAACC,GAAY,MAAA,CAAEvC,OAAQ,CAAA,EAAIE,OAAQqC,EAAQ,IAExDhC,EAAOiC,WAEX,CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect-ts.umd.js","sources":["../src/effect-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Effect } from 'effect';\
|
|
1
|
+
{"version":3,"file":"effect-ts.umd.js","sources":["../src/effect-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport { Effect, Schema } from 'effect';\nimport { ArrayFormatter, decodeUnknown } from 'effect/ParseResult';\nimport { ParseOptions } from 'effect/SchemaAST';\nimport {\n type FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\n\n/**\n * Creates a resolver for react-hook-form using Effect.ts schema validation\n * @param {Schema.Schema<TFieldValues, I>} schema - The Effect.ts schema to validate against\n * @param {ParseOptions} [schemaOptions] - Optional Effect.ts validation options\n * @returns {Resolver<Schema.Schema.Type<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = Schema.Struct({\n * name: Schema.String,\n * age: Schema.Number\n * });\n *\n * useForm({\n * resolver: effectTsResolver(schema)\n * });\n */\nexport function effectTsResolver<TFieldValues extends FieldValues, I>(\n schema: Schema.Schema<TFieldValues, I>,\n schemaOptions: ParseOptions = { errors: 'all', onExcessProperty: 'ignore' },\n): Resolver<Schema.Schema.Type<typeof schema>> {\n return (values, _, options) => {\n return decodeUnknown(\n schema,\n schemaOptions,\n )(values).pipe(\n Effect.catchAll((parseIssue) =>\n Effect.flip(ArrayFormatter.formatIssue(parseIssue)),\n ),\n Effect.mapError((issues) => {\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n const errors = issues.reduce(\n (acc, error) => {\n const key = error.path.join('.');\n\n if (!acc[key]) {\n acc[key] = { message: error.message, type: error._tag };\n }\n\n if (validateAllFieldCriteria) {\n const types = acc[key].types;\n const messages = types && types[String(error._tag)];\n\n acc[key] = appendErrors(\n key,\n validateAllFieldCriteria,\n acc,\n error._tag,\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n return acc;\n },\n {} as Record<string, FieldError>,\n );\n\n return toNestErrors(errors, options);\n }),\n Effect.tap(() =>\n Effect.sync(\n () =>\n options.shouldUseNativeValidation &&\n validateFieldsNatively({}, options),\n ),\n ),\n Effect.match({\n onFailure: (errors) => ({ errors, values: {} }),\n onSuccess: (result) => ({ errors: {}, values: result }),\n }),\n Effect.runPromise,\n );\n };\n}\n"],"names":["schema","schemaOptions","errors","onExcessProperty","values","_","options","decodeUnknown","pipe","Effect","catchAll","parseIssue","flip","ArrayFormatter","formatIssue","mapError","issues","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","reduce","acc","error","key","path","join","message","type","_tag","types","messages","String","appendErrors","concat","toNestErrors","tap","sync","validateFieldsNatively","match","onFailure","onSuccess","result","runPromise"],"mappings":"+fA0BgB,SACdA,EACAC,GAEA,YAFAA,IAAAA,IAAAA,EAA8B,CAAEC,OAAQ,MAAOC,iBAAkB,WAE1D,SAACC,EAAQC,EAAGC,GACjB,OAAOC,EAAaA,cAClBP,EACAC,EAFKM,CAGLH,GAAQI,KACRC,EAAMA,OAACC,SAAS,SAACC,GACf,OAAAF,SAAOG,KAAKC,EAAAA,eAAeC,YAAYH,GAAY,GAErDF,EAAAA,OAAOM,SAAS,SAACC,GACf,IAAMC,GACHX,EAAQY,2BAAsD,QAAzBZ,EAAQa,aAE1CjB,EAASc,EAAOI,OACpB,SAACC,EAAKC,GACJ,IAAMC,EAAMD,EAAME,KAAKC,KAAK,KAM5B,GAJKJ,EAAIE,KACPF,EAAIE,GAAO,CAAEG,QAASJ,EAAMI,QAASC,KAAML,EAAMM,OAG/CX,EAA0B,CAC5B,IAAMY,EAAQR,EAAIE,GAAKM,MACjBC,EAAWD,GAASA,EAAME,OAAOT,EAAMM,OAE7CP,EAAIE,GAAOS,EAAYA,aACrBT,EACAN,EACAI,EACAC,EAAMM,KACNE,EACK,GAAgBG,OAAOH,EAAsBR,EAAMI,SACpDJ,EAAMI,QAEd,CAEA,OAAOL,CACT,EACA,CAAgC,GAGlC,OAAOa,eAAahC,EAAQI,EAC9B,GACAG,EAAAA,OAAO0B,IAAI,WAAA,OACT1B,EAAAA,OAAO2B,KACL,kBACE9B,EAAQY,2BACRmB,EAAsBA,uBAAC,GAAI/B,EAAQ,EACtC,GAEHG,EAAAA,OAAO6B,MAAM,CACXC,UAAW,SAACrC,GAAM,MAAM,CAAEA,OAAAA,EAAQE,OAAQ,GAAI,EAC9CoC,UAAW,SAACC,GAAY,MAAA,CAAEvC,OAAQ,CAAA,EAAIE,OAAQqC,EAAQ,IAExDhC,EAAAA,OAAOiC,WAEX,CACF"}
|
|
@@ -19,11 +19,11 @@ const schema = Schema.Struct({
|
|
|
19
19
|
|
|
20
20
|
type FormData = Schema.Schema.Type<typeof schema>;
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
function TestComponent({
|
|
23
|
+
onSubmit,
|
|
24
|
+
}: {
|
|
23
25
|
onSubmit: (data: FormData) => void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function TestComponent({ onSubmit }: Props) {
|
|
26
|
+
}) {
|
|
27
27
|
const {
|
|
28
28
|
register,
|
|
29
29
|
handleSubmit,
|
|
@@ -57,3 +57,29 @@ test("form's validation with Zod and TypeScript's integration", async () => {
|
|
|
57
57
|
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
|
|
58
58
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
59
59
|
});
|
|
60
|
+
|
|
61
|
+
export function TestComponentManualType({
|
|
62
|
+
onSubmit,
|
|
63
|
+
}: {
|
|
64
|
+
onSubmit: (data: FormData) => void;
|
|
65
|
+
}) {
|
|
66
|
+
const {
|
|
67
|
+
register,
|
|
68
|
+
handleSubmit,
|
|
69
|
+
formState: { errors },
|
|
70
|
+
} = useForm<Schema.Schema.Type<typeof schema>, undefined, FormData>({
|
|
71
|
+
resolver: effectTsResolver(schema), // Useful to check TypeScript regressions
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
76
|
+
<input {...register('username')} />
|
|
77
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
78
|
+
|
|
79
|
+
<input {...register('password')} />
|
|
80
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
81
|
+
|
|
82
|
+
<button type="submit">submit</button>
|
|
83
|
+
</form>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
@@ -102,7 +102,7 @@ export const invalidData = {
|
|
|
102
102
|
{ type: 'car', brand: 'BMW', horsepower: 150 },
|
|
103
103
|
{ type: 'car', brand: 'Mercedes' },
|
|
104
104
|
],
|
|
105
|
-
}
|
|
105
|
+
} as any as Schema.Schema.Type<typeof schema>;
|
|
106
106
|
|
|
107
107
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
108
108
|
username: {
|
|
@@ -1,16 +1,37 @@
|
|
|
1
1
|
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
|
-
import { Effect } from 'effect';
|
|
3
|
-
|
|
2
|
+
import { Effect, Schema } from 'effect';
|
|
4
3
|
import { ArrayFormatter, decodeUnknown } from 'effect/ParseResult';
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
4
|
+
import { ParseOptions } from 'effect/SchemaAST';
|
|
5
|
+
import {
|
|
6
|
+
type FieldError,
|
|
7
|
+
FieldValues,
|
|
8
|
+
Resolver,
|
|
9
|
+
appendErrors,
|
|
10
|
+
} from 'react-hook-form';
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Creates a resolver for react-hook-form using Effect.ts schema validation
|
|
14
|
+
* @param {Schema.Schema<TFieldValues, I>} schema - The Effect.ts schema to validate against
|
|
15
|
+
* @param {ParseOptions} [schemaOptions] - Optional Effect.ts validation options
|
|
16
|
+
* @returns {Resolver<Schema.Schema.Type<typeof schema>>} A resolver function compatible with react-hook-form
|
|
17
|
+
* @example
|
|
18
|
+
* const schema = Schema.Struct({
|
|
19
|
+
* name: Schema.String,
|
|
20
|
+
* age: Schema.Number
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* useForm({
|
|
24
|
+
* resolver: effectTsResolver(schema)
|
|
25
|
+
* });
|
|
26
|
+
*/
|
|
27
|
+
export function effectTsResolver<TFieldValues extends FieldValues, I>(
|
|
28
|
+
schema: Schema.Schema<TFieldValues, I>,
|
|
29
|
+
schemaOptions: ParseOptions = { errors: 'all', onExcessProperty: 'ignore' },
|
|
30
|
+
): Resolver<Schema.Schema.Type<typeof schema>> {
|
|
31
|
+
return (values, _, options) => {
|
|
11
32
|
return decodeUnknown(
|
|
12
33
|
schema,
|
|
13
|
-
|
|
34
|
+
schemaOptions,
|
|
14
35
|
)(values).pipe(
|
|
15
36
|
Effect.catchAll((parseIssue) =>
|
|
16
37
|
Effect.flip(ArrayFormatter.formatIssue(parseIssue)),
|
|
@@ -63,3 +84,4 @@ export const effectTsResolver: Resolver =
|
|
|
63
84
|
Effect.runPromise,
|
|
64
85
|
);
|
|
65
86
|
};
|
|
87
|
+
}
|
package/effect-ts/src/index.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
import { AsyncValidator, Validator } from 'fluentvalidation-ts';
|
|
2
2
|
import { FieldValues, Resolver } from 'react-hook-form';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resolver for react-hook-form using FluentValidation schema validation
|
|
5
|
+
* @param {Validator<TFieldValues>} validator - The FluentValidation validator to use
|
|
6
|
+
* @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form
|
|
7
|
+
* @example
|
|
8
|
+
* import { Validator } from 'fluentvalidation-ts';
|
|
9
|
+
*
|
|
10
|
+
* class SchemaValidator extends Validator<Schema> {
|
|
11
|
+
* constructor() {
|
|
12
|
+
* super();
|
|
13
|
+
* this.ruleFor('username').notEmpty();
|
|
14
|
+
* this.ruleFor('password').notEmpty();
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const validator = new SchemaValidator();
|
|
19
|
+
*
|
|
20
|
+
* useForm({
|
|
21
|
+
* resolver: fluentValidationResolver(validator)
|
|
22
|
+
* });
|
|
23
|
+
*/
|
|
3
24
|
export declare function fluentValidationResolver<TFieldValues extends FieldValues>(validator: Validator<TFieldValues>): Resolver<TFieldValues>;
|
|
4
25
|
export declare function fluentAsyncValidationResolver<TFieldValues extends FieldValues, TValidator extends AsyncValidator<TFieldValues>>(validator: TValidator): Resolver<TFieldValues>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var e=require("@hookform/resolvers");function r(e,t,o){void 0===o&&(o=[]);var n=function(){var n=[].concat(o,[a]),i=e[a];Array.isArray(i)?i.forEach(function(e,o){r(e,t,[].concat(n,[o]))}):"object"==typeof i&&null!==i?r(i,t,n):"string"==typeof i&&(t[n.join(".")]={type:"validation",message:i})};for(var a in e)n()}function t(e,t){var o={};return r(e,o),o}exports.fluentAsyncValidationResolver=function(r){return function(o,n,a){try{return Promise.resolve(r.validateAsync(o)).then(function(r){var n=0===Object.keys(r).length;return a.shouldUseNativeValidation&&e.validateFieldsNatively({},a),n?{values:o,errors:{}}:{values:{},errors:e.toNestErrors(t(r),a)}})}catch(e){return Promise.reject(e)}}},exports.fluentValidationResolver=function(r){return function(o,n,a){try{var i=r.validate(o),s=0===Object.keys(i).length;return a.shouldUseNativeValidation&&e.validateFieldsNatively({},a),Promise.resolve(s?{values:o,errors:{}}:{values:{},errors:e.toNestErrors(t(i),a)})}catch(e){return Promise.reject(e)}}};
|
|
2
2
|
//# sourceMappingURL=fluentvalidation-ts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluentvalidation-ts.js","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\
|
|
1
|
+
{"version":3,"file":"fluentvalidation-ts.js","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\nfunction parseErrorSchema<T>(\n validationErrors: ValidationErrors<T>,\n validateAllFieldCriteria: boolean,\n) {\n if (validateAllFieldCriteria) {\n // TODO: check this but i think its always one validation error\n }\n\n const errors: Record<string, FieldError> = {};\n traverseObject(validationErrors, errors);\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form using FluentValidation schema validation\n * @param {Validator<TFieldValues>} validator - The FluentValidation validator to use\n * @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form\n * @example\n * import { Validator } from 'fluentvalidation-ts';\n *\n * class SchemaValidator extends Validator<Schema> {\n * constructor() {\n * super();\n * this.ruleFor('username').notEmpty();\n * this.ruleFor('password').notEmpty();\n * }\n * }\n *\n * const validator = new SchemaValidator();\n *\n * useForm({\n * resolver: fluentValidationResolver(validator)\n * });\n */\nexport function fluentValidationResolver<TFieldValues extends FieldValues>(\n validator: Validator<TFieldValues>,\n): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = validator.validate(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n\nexport function fluentAsyncValidationResolver<\n TFieldValues extends FieldValues,\n TValidator extends AsyncValidator<TFieldValues>,\n>(validator: TValidator): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = await validator.validateAsync(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n"],"names":["traverseObject","object","errors","parentIndices","_loop","currentIndex","concat","key","currentValue","Array","isArray","forEach","item","index","join","type","message","parseErrorSchema","validationErrors","validateAllFieldCriteria","validator","values","_context","options","Promise","resolve","validateAsync","then","validationResult","isValid","Object","keys","length","shouldUseNativeValidation","validateFieldsNatively","toNestErrors","e","reject","validate"],"mappings":"qCAQA,SAASA,EACPC,EACAC,EACAC,YAAAA,IAAAA,EAAqC,IAAE,IAAAC,EAAAA,WAGrC,IAAMC,EAAY,GAAAC,OAAOH,EAAa,CAAEI,IAClCC,EAAeP,EAAOM,GAExBE,MAAMC,QAAQF,GAChBA,EAAaG,QAAQ,SAACC,EAAWC,GAC/Bb,EAAeY,EAAMV,EAAMI,GAAAA,OAAMD,EAAY,CAAEQ,IACjD,GACiC,iBAAjBL,GAA8C,OAAjBA,EAC7CR,EAAeQ,EAAcN,EAAQG,GACJ,iBAAjBG,IAChBN,EAAOG,EAAaS,KAAK,MAAQ,CAC/BC,KAAM,aACNC,QAASR,GAGf,EAhBA,IAAK,IAAMD,KAAON,EAAMG,GAiB1B,CAEA,SAASa,EACPC,EACAC,GAMA,IAAMjB,EAAqC,CAAA,EAG3C,OAFAF,EAAekB,EAAkBhB,GAE1BA,CACT,uCAmDM,SAGJkB,GACA,OAAcC,SAAAA,EAAQC,EAAUC,GAAO,WAAIC,QAAAC,QACVL,EAAUM,cAAcL,IAAOM,KAAxDC,SAAAA,GACN,IAAMC,EAAmD,IAAzCC,OAAOC,KAAKH,GAAkBI,OAI9C,OAFAT,EAAQU,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEX,GAEzDM,EACH,CACER,OAAQA,EACRnB,OAAQ,IAEV,CACEmB,OAAQ,GACRnB,OAAQiC,EAAYA,aAClBlB,EACEW,GAIFL,GAEF,EACR,CAAC,MAAAa,GAAA,OAAAZ,QAAAa,OAAAD,EACH,CAAA,CAAA,mCAvDgB,SACdhB,GAEA,OAAcC,SAAAA,EAAQC,EAAUC,GAAW,IACzC,IAAMK,EAAmBR,EAAUkB,SAASjB,GACtCQ,EAAmD,IAAzCC,OAAOC,KAAKH,GAAkBI,OAI9C,OAFAT,EAAQU,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEX,GAEhEC,QAAAC,QAAOI,EACH,CACER,OAAQA,EACRnB,OAAQ,IAEV,CACEmB,OAAQ,GACRnB,OAAQiC,EAAAA,aACNlB,EACEW,GAIFL,IAGV,CAAC,MAAAa,UAAAZ,QAAAa,OAAAD,EAAA,CAAA,CACH"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function t(r,e,o){void 0===o&&(o=[]);var n=function(){var n=[].concat(o,[a]),i=r[a];Array.isArray(i)?i.forEach(function(r,o){t(r,e,[].concat(n,[o]))}):"object"==typeof i&&null!==i?t(i,e,n):"string"==typeof i&&(e[n.join(".")]={type:"validation",message:i})};for(var a in r)n()}
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function t(r,e,o){void 0===o&&(o=[]);var n=function(){var n=[].concat(o,[a]),i=r[a];Array.isArray(i)?i.forEach(function(r,o){t(r,e,[].concat(n,[o]))}):"object"==typeof i&&null!==i?t(i,e,n):"string"==typeof i&&(e[n.join(".")]={type:"validation",message:i})};for(var a in r)n()}function o(r,e){var o={};return t(r,o),o}function n(t){return function(n,a,i){try{var s=t.validate(n),c=0===Object.keys(s).length;return i.shouldUseNativeValidation&&r({},i),Promise.resolve(c?{values:n,errors:{}}:{values:{},errors:e(o(s),i)})}catch(r){return Promise.reject(r)}}}function a(t){return function(n,a,i){try{return Promise.resolve(t.validateAsync(n)).then(function(t){var a=0===Object.keys(t).length;return i.shouldUseNativeValidation&&r({},i),a?{values:n,errors:{}}:{values:{},errors:e(o(t),i)}})}catch(r){return Promise.reject(r)}}}export{a as fluentAsyncValidationResolver,n as fluentValidationResolver};
|
|
2
2
|
//# sourceMappingURL=fluentvalidation-ts.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function t(r,e,o=[]){for(const n in r){const s=[...o,n],a=r[n];Array.isArray(a)?a.forEach((r,o)=>{t(r,e,[...s,o])}):"object"==typeof a&&null!==a?t(a,e,s):"string"==typeof a&&(e[s.join(".")]={type:"validation",message:a})}}
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function t(r,e,o=[]){for(const n in r){const s=[...o,n],a=r[n];Array.isArray(a)?a.forEach((r,o)=>{t(r,e,[...s,o])}):"object"==typeof a&&null!==a?t(a,e,s):"string"==typeof a&&(e[s.join(".")]={type:"validation",message:a})}}function o(r,e){const o={};return t(r,o),o}function n(t){return async(n,s,a)=>{const i=t.validate(n),c=0===Object.keys(i).length;return a.shouldUseNativeValidation&&r({},a),c?{values:n,errors:{}}:{values:{},errors:e(o(i),a)}}}function s(t){return async(n,s,a)=>{const i=await t.validateAsync(n),c=0===Object.keys(i).length;return a.shouldUseNativeValidation&&r({},a),c?{values:n,errors:{}}:{values:{},errors:e(o(i),a)}}}export{s as fluentAsyncValidationResolver,n as fluentValidationResolver};
|
|
2
2
|
//# sourceMappingURL=fluentvalidation-ts.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluentvalidation-ts.modern.mjs","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\
|
|
1
|
+
{"version":3,"file":"fluentvalidation-ts.modern.mjs","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\nfunction parseErrorSchema<T>(\n validationErrors: ValidationErrors<T>,\n validateAllFieldCriteria: boolean,\n) {\n if (validateAllFieldCriteria) {\n // TODO: check this but i think its always one validation error\n }\n\n const errors: Record<string, FieldError> = {};\n traverseObject(validationErrors, errors);\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form using FluentValidation schema validation\n * @param {Validator<TFieldValues>} validator - The FluentValidation validator to use\n * @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form\n * @example\n * import { Validator } from 'fluentvalidation-ts';\n *\n * class SchemaValidator extends Validator<Schema> {\n * constructor() {\n * super();\n * this.ruleFor('username').notEmpty();\n * this.ruleFor('password').notEmpty();\n * }\n * }\n *\n * const validator = new SchemaValidator();\n *\n * useForm({\n * resolver: fluentValidationResolver(validator)\n * });\n */\nexport function fluentValidationResolver<TFieldValues extends FieldValues>(\n validator: Validator<TFieldValues>,\n): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = validator.validate(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n\nexport function fluentAsyncValidationResolver<\n TFieldValues extends FieldValues,\n TValidator extends AsyncValidator<TFieldValues>,\n>(validator: TValidator): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = await validator.validateAsync(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n"],"names":["traverseObject","object","errors","parentIndices","key","currentIndex","currentValue","Array","isArray","forEach","item","index","join","type","message","parseErrorSchema","validationErrors","validateAllFieldCriteria","fluentValidationResolver","validator","values","_context","options","validationResult","validate","isValid","Object","keys","length","shouldUseNativeValidation","validateFieldsNatively","toNestErrors","fluentAsyncValidationResolver","validateAsync"],"mappings":"+EAQA,SAASA,EACPC,EACAC,EACAC,EAAqC,IAErC,IAAK,MAAMC,KAAOH,EAAQ,CACxB,MAAMI,EAAe,IAAIF,EAAeC,GAClCE,EAAeL,EAAOG,GAExBG,MAAMC,QAAQF,GAChBA,EAAaG,QAAQ,CAACC,EAAWC,KAC/BX,EAAeU,EAAMR,EAAQ,IAAIG,EAAcM,GAAM,GAEtB,iBAAjBL,GAA8C,OAAjBA,EAC7CN,EAAeM,EAAcJ,EAAQG,GACJ,iBAAjBC,IAChBJ,EAAOG,EAAaO,KAAK,MAAQ,CAC/BC,KAAM,aACNC,QAASR,GAGf,CACF,CAEA,SAASS,EACPC,EACAC,GAMA,MAAMf,EAAqC,CAAA,EAG3C,OAFAF,EAAegB,EAAkBd,GAE1BA,CACT,CAuBM,SAAUgB,EACdC,GAEA,OAAcC,MAAAA,EAAQC,EAAUC,KAC9B,MAAMC,EAAmBJ,EAAUK,SAASJ,GACtCK,EAAmD,IAAzCC,OAAOC,KAAKJ,GAAkBK,OAI9C,OAFAN,EAAQO,2BAA6BC,EAAuB,GAAIR,GAEzDG,EACH,CACEL,OAAQA,EACRlB,OAAQ,CACT,GACD,CACEkB,OAAQ,CAAA,EACRlB,OAAQ6B,EACNhB,EACEQ,GAIFD,IAIZ,CAEgB,SAAAU,EAGdb,GACA,OAAcC,MAAAA,EAAQC,EAAUC,KAC9B,MAAMC,QAAyBJ,EAAUc,cAAcb,GACjDK,EAAmD,IAAzCC,OAAOC,KAAKJ,GAAkBK,OAI9C,OAFAN,EAAQO,2BAA6BC,EAAuB,CAAE,EAAER,GAEzDG,EACH,CACEL,OAAQA,EACRlB,OAAQ,CACT,GACD,CACEkB,OAAQ,CAAA,EACRlB,OAAQ6B,EACNhB,EACEQ,GAIFD,IAIZ"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function t(r,e,o){void 0===o&&(o=[]);var n=function(){var n=[].concat(o,[a]),i=r[a];Array.isArray(i)?i.forEach(function(r,o){t(r,e,[].concat(n,[o]))}):"object"==typeof i&&null!==i?t(i,e,n):"string"==typeof i&&(e[n.join(".")]={type:"validation",message:i})};for(var a in r)n()}
|
|
1
|
+
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";function t(r,e,o){void 0===o&&(o=[]);var n=function(){var n=[].concat(o,[a]),i=r[a];Array.isArray(i)?i.forEach(function(r,o){t(r,e,[].concat(n,[o]))}):"object"==typeof i&&null!==i?t(i,e,n):"string"==typeof i&&(e[n.join(".")]={type:"validation",message:i})};for(var a in r)n()}function o(r,e){var o={};return t(r,o),o}function n(t){return function(n,a,i){try{var s=t.validate(n),c=0===Object.keys(s).length;return i.shouldUseNativeValidation&&r({},i),Promise.resolve(c?{values:n,errors:{}}:{values:{},errors:e(o(s),i)})}catch(r){return Promise.reject(r)}}}function a(t){return function(n,a,i){try{return Promise.resolve(t.validateAsync(n)).then(function(t){var a=0===Object.keys(t).length;return i.shouldUseNativeValidation&&r({},i),a?{values:n,errors:{}}:{values:{},errors:e(o(t),i)}})}catch(r){return Promise.reject(r)}}}export{a as fluentAsyncValidationResolver,n as fluentValidationResolver};
|
|
2
2
|
//# sourceMappingURL=fluentvalidation-ts.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluentvalidation-ts.module.js","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\
|
|
1
|
+
{"version":3,"file":"fluentvalidation-ts.module.js","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\nfunction parseErrorSchema<T>(\n validationErrors: ValidationErrors<T>,\n validateAllFieldCriteria: boolean,\n) {\n if (validateAllFieldCriteria) {\n // TODO: check this but i think its always one validation error\n }\n\n const errors: Record<string, FieldError> = {};\n traverseObject(validationErrors, errors);\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form using FluentValidation schema validation\n * @param {Validator<TFieldValues>} validator - The FluentValidation validator to use\n * @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form\n * @example\n * import { Validator } from 'fluentvalidation-ts';\n *\n * class SchemaValidator extends Validator<Schema> {\n * constructor() {\n * super();\n * this.ruleFor('username').notEmpty();\n * this.ruleFor('password').notEmpty();\n * }\n * }\n *\n * const validator = new SchemaValidator();\n *\n * useForm({\n * resolver: fluentValidationResolver(validator)\n * });\n */\nexport function fluentValidationResolver<TFieldValues extends FieldValues>(\n validator: Validator<TFieldValues>,\n): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = validator.validate(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n\nexport function fluentAsyncValidationResolver<\n TFieldValues extends FieldValues,\n TValidator extends AsyncValidator<TFieldValues>,\n>(validator: TValidator): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = await validator.validateAsync(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n"],"names":["traverseObject","object","errors","parentIndices","_loop","currentIndex","concat","key","currentValue","Array","isArray","forEach","item","index","join","type","message","parseErrorSchema","validationErrors","validateAllFieldCriteria","fluentValidationResolver","validator","values","_context","options","validationResult","validate","isValid","Object","keys","length","shouldUseNativeValidation","validateFieldsNatively","Promise","resolve","toNestErrors","e","reject","fluentAsyncValidationResolver","validateAsync","then"],"mappings":"+EAQA,SAASA,EACPC,EACAC,EACAC,YAAAA,IAAAA,EAAqC,IAAE,IAAAC,EAAAA,WAGrC,IAAMC,EAAY,GAAAC,OAAOH,EAAa,CAAEI,IAClCC,EAAeP,EAAOM,GAExBE,MAAMC,QAAQF,GAChBA,EAAaG,QAAQ,SAACC,EAAWC,GAC/Bb,EAAeY,EAAMV,EAAMI,GAAAA,OAAMD,EAAY,CAAEQ,IACjD,GACiC,iBAAjBL,GAA8C,OAAjBA,EAC7CR,EAAeQ,EAAcN,EAAQG,GACJ,iBAAjBG,IAChBN,EAAOG,EAAaS,KAAK,MAAQ,CAC/BC,KAAM,aACNC,QAASR,GAGf,EAhBA,IAAK,IAAMD,KAAON,EAAMG,GAiB1B,CAEA,SAASa,EACPC,EACAC,GAMA,IAAMjB,EAAqC,CAAA,EAG3C,OAFAF,EAAekB,EAAkBhB,GAE1BA,CACT,CAuBgB,SAAAkB,EACdC,GAEA,OAAcC,SAAAA,EAAQC,EAAUC,GAAW,IACzC,IAAMC,EAAmBJ,EAAUK,SAASJ,GACtCK,EAAmD,IAAzCC,OAAOC,KAAKJ,GAAkBK,OAI9C,OAFAN,EAAQO,2BAA6BC,EAAuB,CAAE,EAAER,GAEhES,QAAAC,QAAOP,EACH,CACEL,OAAQA,EACRpB,OAAQ,IAEV,CACEoB,OAAQ,GACRpB,OAAQiC,EACNlB,EACEQ,GAIFD,IAGV,CAAC,MAAAY,UAAAH,QAAAI,OAAAD,EAAA,CAAA,CACH,CAEM,SAAUE,EAGdjB,GACA,OAAcC,SAAAA,EAAQC,EAAUC,GAAO,WAAIS,QAAAC,QACVb,EAAUkB,cAAcjB,IAAOkB,KAAxDf,SAAAA,GACN,IAAME,EAAmD,IAAzCC,OAAOC,KAAKJ,GAAkBK,OAI9C,OAFAN,EAAQO,2BAA6BC,EAAuB,CAAE,EAAER,GAEzDG,EACH,CACEL,OAAQA,EACRpB,OAAQ,IAEV,CACEoB,OAAQ,GACRpB,OAAQiC,EACNlB,EACEQ,GAIFD,GAEF,EACR,CAAC,MAAAY,GAAA,OAAAH,QAAAI,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],r):r((e||self)["hookformResolversfluentvalidation-ts"]={},e.hookformResolvers)}(this,function(e,r){function o(e,r,t){void 0===t&&(t=[]);var n=function(){var n=[].concat(t,[i]),s=e[i];Array.isArray(s)?s.forEach(function(e,t){o(e,r,[].concat(n,[t]))}):"object"==typeof s&&null!==s?o(s,r,n):"string"==typeof s&&(r[n.join(".")]={type:"validation",message:s})};for(var i in e)n()}
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],r):r((e||self)["hookformResolversfluentvalidation-ts"]={},e.hookformResolvers)}(this,function(e,r){function o(e,r,t){void 0===t&&(t=[]);var n=function(){var n=[].concat(t,[i]),s=e[i];Array.isArray(s)?s.forEach(function(e,t){o(e,r,[].concat(n,[t]))}):"object"==typeof s&&null!==s?o(s,r,n):"string"==typeof s&&(r[n.join(".")]={type:"validation",message:s})};for(var i in e)n()}function t(e,r){var t={};return o(e,t),t}e.fluentAsyncValidationResolver=function(e){return function(o,n,i){try{return Promise.resolve(e.validateAsync(o)).then(function(e){var n=0===Object.keys(e).length;return i.shouldUseNativeValidation&&r.validateFieldsNatively({},i),n?{values:o,errors:{}}:{values:{},errors:r.toNestErrors(t(e),i)}})}catch(e){return Promise.reject(e)}}},e.fluentValidationResolver=function(e){return function(o,n,i){try{var s=e.validate(o),a=0===Object.keys(s).length;return i.shouldUseNativeValidation&&r.validateFieldsNatively({},i),Promise.resolve(a?{values:o,errors:{}}:{values:{},errors:r.toNestErrors(t(s),i)})}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=fluentvalidation-ts.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluentvalidation-ts.umd.js","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\
|
|
1
|
+
{"version":3,"file":"fluentvalidation-ts.umd.js","sources":["../src/fluentvalidation-ts.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport {\n AsyncValidator,\n ValidationErrors,\n Validator,\n} from 'fluentvalidation-ts';\nimport { FieldError, FieldValues, Resolver } from 'react-hook-form';\n\nfunction traverseObject<T>(\n object: ValidationErrors<T>,\n errors: Record<string, FieldError>,\n parentIndices: (string | number)[] = [],\n) {\n for (const key in object) {\n const currentIndex = [...parentIndices, key];\n const currentValue = object[key];\n\n if (Array.isArray(currentValue)) {\n currentValue.forEach((item: any, index: number) => {\n traverseObject(item, errors, [...currentIndex, index]);\n });\n } else if (typeof currentValue === 'object' && currentValue !== null) {\n traverseObject(currentValue, errors, currentIndex);\n } else if (typeof currentValue === 'string') {\n errors[currentIndex.join('.')] = {\n type: 'validation',\n message: currentValue,\n };\n }\n }\n}\n\nfunction parseErrorSchema<T>(\n validationErrors: ValidationErrors<T>,\n validateAllFieldCriteria: boolean,\n) {\n if (validateAllFieldCriteria) {\n // TODO: check this but i think its always one validation error\n }\n\n const errors: Record<string, FieldError> = {};\n traverseObject(validationErrors, errors);\n\n return errors;\n}\n\n/**\n * Creates a resolver for react-hook-form using FluentValidation schema validation\n * @param {Validator<TFieldValues>} validator - The FluentValidation validator to use\n * @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form\n * @example\n * import { Validator } from 'fluentvalidation-ts';\n *\n * class SchemaValidator extends Validator<Schema> {\n * constructor() {\n * super();\n * this.ruleFor('username').notEmpty();\n * this.ruleFor('password').notEmpty();\n * }\n * }\n *\n * const validator = new SchemaValidator();\n *\n * useForm({\n * resolver: fluentValidationResolver(validator)\n * });\n */\nexport function fluentValidationResolver<TFieldValues extends FieldValues>(\n validator: Validator<TFieldValues>,\n): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = validator.validate(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n\nexport function fluentAsyncValidationResolver<\n TFieldValues extends FieldValues,\n TValidator extends AsyncValidator<TFieldValues>,\n>(validator: TValidator): Resolver<TFieldValues> {\n return async (values, _context, options) => {\n const validationResult = await validator.validateAsync(values);\n const isValid = Object.keys(validationResult).length === 0;\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n return isValid\n ? {\n values: values,\n errors: {},\n }\n : {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n validationResult,\n !options.shouldUseNativeValidation &&\n options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n}\n"],"names":["traverseObject","object","errors","parentIndices","_loop","currentIndex","concat","key","currentValue","Array","isArray","forEach","item","index","join","type","message","parseErrorSchema","validationErrors","validateAllFieldCriteria","validator","values","_context","options","Promise","resolve","validateAsync","then","validationResult","isValid","Object","keys","length","shouldUseNativeValidation","validateFieldsNatively","toNestErrors","e","reject","validate"],"mappings":"4UAQA,SAASA,EACPC,EACAC,EACAC,YAAAA,IAAAA,EAAqC,IAAE,IAAAC,EAAAA,WAGrC,IAAMC,EAAY,GAAAC,OAAOH,EAAa,CAAEI,IAClCC,EAAeP,EAAOM,GAExBE,MAAMC,QAAQF,GAChBA,EAAaG,QAAQ,SAACC,EAAWC,GAC/Bb,EAAeY,EAAMV,EAAMI,GAAAA,OAAMD,EAAY,CAAEQ,IACjD,GACiC,iBAAjBL,GAA8C,OAAjBA,EAC7CR,EAAeQ,EAAcN,EAAQG,GACJ,iBAAjBG,IAChBN,EAAOG,EAAaS,KAAK,MAAQ,CAC/BC,KAAM,aACNC,QAASR,GAGf,EAhBA,IAAK,IAAMD,KAAON,EAAMG,GAiB1B,CAEA,SAASa,EACPC,EACAC,GAMA,IAAMjB,EAAqC,CAAA,EAG3C,OAFAF,EAAekB,EAAkBhB,GAE1BA,CACT,iCAmDM,SAGJkB,GACA,OAAcC,SAAAA,EAAQC,EAAUC,GAAO,WAAIC,QAAAC,QACVL,EAAUM,cAAcL,IAAOM,KAAxDC,SAAAA,GACN,IAAMC,EAAmD,IAAzCC,OAAOC,KAAKH,GAAkBI,OAI9C,OAFAT,EAAQU,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEX,GAEzDM,EACH,CACER,OAAQA,EACRnB,OAAQ,IAEV,CACEmB,OAAQ,GACRnB,OAAQiC,EAAYA,aAClBlB,EACEW,GAIFL,GAEF,EACR,CAAC,MAAAa,GAAA,OAAAZ,QAAAa,OAAAD,EACH,CAAA,CAAA,6BAvDgB,SACdhB,GAEA,OAAcC,SAAAA,EAAQC,EAAUC,GAAW,IACzC,IAAMK,EAAmBR,EAAUkB,SAASjB,GACtCQ,EAAmD,IAAzCC,OAAOC,KAAKH,GAAkBI,OAI9C,OAFAT,EAAQU,2BAA6BC,EAAAA,uBAAuB,CAAE,EAAEX,GAEhEC,QAAAC,QAAOI,EACH,CACER,OAAQA,EACRnB,OAAQ,IAEV,CACEmB,OAAQ,GACRnB,OAAQiC,EAAAA,aACNlB,EACEW,GAIFL,IAGV,CAAC,MAAAa,UAAAZ,QAAAa,OAAAD,EAAA,CAAA,CACH"}
|
|
@@ -30,10 +30,10 @@ function traverseObject<T>(
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
function parseErrorSchema<T>(
|
|
34
34
|
validationErrors: ValidationErrors<T>,
|
|
35
35
|
validateAllFieldCriteria: boolean,
|
|
36
|
-
)
|
|
36
|
+
) {
|
|
37
37
|
if (validateAllFieldCriteria) {
|
|
38
38
|
// TODO: check this but i think its always one validation error
|
|
39
39
|
}
|
|
@@ -42,8 +42,29 @@ const parseErrorSchema = <T>(
|
|
|
42
42
|
traverseObject(validationErrors, errors);
|
|
43
43
|
|
|
44
44
|
return errors;
|
|
45
|
-
}
|
|
45
|
+
}
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Creates a resolver for react-hook-form using FluentValidation schema validation
|
|
49
|
+
* @param {Validator<TFieldValues>} validator - The FluentValidation validator to use
|
|
50
|
+
* @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form
|
|
51
|
+
* @example
|
|
52
|
+
* import { Validator } from 'fluentvalidation-ts';
|
|
53
|
+
*
|
|
54
|
+
* class SchemaValidator extends Validator<Schema> {
|
|
55
|
+
* constructor() {
|
|
56
|
+
* super();
|
|
57
|
+
* this.ruleFor('username').notEmpty();
|
|
58
|
+
* this.ruleFor('password').notEmpty();
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* const validator = new SchemaValidator();
|
|
63
|
+
*
|
|
64
|
+
* useForm({
|
|
65
|
+
* resolver: fluentValidationResolver(validator)
|
|
66
|
+
* });
|
|
67
|
+
*/
|
|
47
68
|
export function fluentValidationResolver<TFieldValues extends FieldValues>(
|
|
48
69
|
validator: Validator<TFieldValues>,
|
|
49
70
|
): Resolver<TFieldValues> {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { ValidationError } from 'io-ts';
|
|
2
|
-
import {
|
|
2
|
+
import { FieldError } from 'react-hook-form';
|
|
3
|
+
export type ErrorObject = Record<string, FieldError>;
|
|
4
|
+
export type FieldErrorWithPath = FieldError & {
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
3
7
|
declare const errorsToRecord: (validateAllFieldCriteria: boolean) => (validationErrors: ReadonlyArray<ValidationError>) => ErrorObject;
|
|
4
8
|
export default errorsToRecord;
|
package/io-ts/dist/index.d.ts
CHANGED
package/io-ts/dist/io-ts.d.ts
CHANGED
|
@@ -1,2 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import * as t from 'io-ts';
|
|
2
|
+
import { FieldValues, Resolver } from 'react-hook-form';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resolver for react-hook-form using io-ts schema validation
|
|
5
|
+
* @param {t.Type<TFieldValues, T>} schema - The io-ts schema to validate against
|
|
6
|
+
* @param {Object} options - Additional resolver configuration
|
|
7
|
+
* @param {string} [options.mode='async'] - Validation mode
|
|
8
|
+
* @returns {Resolver<t.OutputOf<typeof schema>>} A resolver function compatible with react-hook-form
|
|
9
|
+
* @example
|
|
10
|
+
* const schema = t.type({
|
|
11
|
+
* name: t.string,
|
|
12
|
+
* age: t.number
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* useForm({
|
|
16
|
+
* resolver: ioTsResolver(schema)
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
export declare function ioTsResolver<T extends Record<string, any>, TFieldValues extends FieldValues>(schema: t.Type<TFieldValues, T>): Resolver<t.OutputOf<typeof schema>>;
|
package/io-ts/dist/io-ts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"io-ts.js","sources":["../src/arrayToPath.ts","../src/errorsToRecord.ts","../src/io-ts.ts"],"sourcesContent":["import * as Either from 'fp-ts/Either';\nimport { pipe } from 'fp-ts/function';\n\nconst arrayToPath = (paths: Either.Either<string, number>[]): string =>\n paths.reduce(\n (previous, path, index) =>\n pipe(\n path,\n Either.fold(\n (key) => `${index > 0 ? '.' : ''}${key}`,\n (key) => `[${key}]`,\n ),\n (path) => `${previous}${path}`,\n ),\n '',\n );\n\nexport default arrayToPath;\n","import * as Either from 'fp-ts/Either';\nimport * as Option from 'fp-ts/Option';\nimport * as ReadonlyArray from 'fp-ts/ReadonlyArray';\nimport * as ReadonlyRecord from 'fp-ts/ReadonlyRecord';\nimport * as SemiGroup from 'fp-ts/Semigroup';\nimport { absurd, flow, identity, not, pipe } from 'fp-ts/function';\nimport * as t from 'io-ts';\nimport {\n ExactType,\n IntersectionType,\n RefinementType,\n TaggedUnionType,\n UnionType,\n ValidationError,\n} from 'io-ts';\nimport arrayToPath from './arrayToPath';\
|
|
1
|
+
{"version":3,"file":"io-ts.js","sources":["../src/arrayToPath.ts","../src/errorsToRecord.ts","../src/io-ts.ts"],"sourcesContent":["import * as Either from 'fp-ts/Either';\nimport { pipe } from 'fp-ts/function';\n\nconst arrayToPath = (paths: Either.Either<string, number>[]): string =>\n paths.reduce(\n (previous, path, index) =>\n pipe(\n path,\n Either.fold(\n (key) => `${index > 0 ? '.' : ''}${key}`,\n (key) => `[${key}]`,\n ),\n (path) => `${previous}${path}`,\n ),\n '',\n );\n\nexport default arrayToPath;\n","import * as Either from 'fp-ts/Either';\nimport * as Option from 'fp-ts/Option';\nimport * as ReadonlyArray from 'fp-ts/ReadonlyArray';\nimport * as ReadonlyRecord from 'fp-ts/ReadonlyRecord';\nimport * as SemiGroup from 'fp-ts/Semigroup';\nimport { absurd, flow, identity, not, pipe } from 'fp-ts/function';\nimport * as t from 'io-ts';\nimport {\n ExactType,\n IntersectionType,\n RefinementType,\n TaggedUnionType,\n UnionType,\n ValidationError,\n} from 'io-ts';\nimport { FieldError } from 'react-hook-form';\nimport arrayToPath from './arrayToPath';\n\nexport type ErrorObject = Record<string, FieldError>;\nexport type FieldErrorWithPath = FieldError & { path: string };\n\nconst INSTANCE_TYPES_TO_FILTER = [\n TaggedUnionType,\n UnionType,\n IntersectionType,\n ExactType,\n RefinementType,\n];\nconst formatErrorPath = (context: t.Context): string =>\n pipe(\n context,\n ReadonlyArray.filterMapWithIndex((index, contextEntry) => {\n const previousIndex = index - 1;\n const previousContextEntry =\n previousIndex === -1 ? undefined : context[previousIndex];\n const shouldBeFiltered =\n previousContextEntry === undefined ||\n INSTANCE_TYPES_TO_FILTER.some(\n (type) => previousContextEntry.type instanceof type,\n );\n\n return shouldBeFiltered ? Option.none : Option.some(contextEntry);\n }),\n ReadonlyArray.map(({ key }) => key),\n ReadonlyArray.map((key) =>\n pipe(\n key,\n (k) => parseInt(k, 10),\n Either.fromPredicate(not<number>(Number.isNaN), () => key),\n ),\n ),\n ReadonlyArray.toArray,\n arrayToPath,\n );\n\nconst formatError = (e: t.ValidationError): FieldErrorWithPath => {\n const path = formatErrorPath(e.context);\n\n const message = pipe(\n e.message,\n Either.fromNullable(e.context),\n Either.mapLeft(\n flow(\n ReadonlyArray.last,\n Option.map(\n (contextEntry) =>\n `expected ${contextEntry.type.name} but got ${JSON.stringify(\n contextEntry.actual,\n )}`,\n ),\n Option.getOrElseW(() =>\n absurd<string>('Error context is missing name' as never),\n ),\n ),\n ),\n Either.getOrElseW(identity),\n );\n\n const type = pipe(\n e.context,\n ReadonlyArray.last,\n Option.map((contextEntry) => contextEntry.type.name),\n Option.getOrElse(() => 'unknown'),\n );\n\n return { message, type, path };\n};\n\n// this is almost the same function like Semigroup.getObjectSemigroup but reversed\n// in order to get the first error\nconst getObjectSemigroup = <\n A extends Record<string, unknown> = never,\n>(): SemiGroup.Semigroup<A> => ({\n concat: (first, second) => Object.assign({}, second, first),\n});\n\nconst concatToSingleError = (\n errors: ReadonlyArray<FieldErrorWithPath>,\n): ErrorObject =>\n pipe(\n errors,\n ReadonlyArray.map((error) => ({\n [error.path]: {\n type: error.type,\n message: error.message,\n },\n })),\n (errors) => SemiGroup.fold(getObjectSemigroup<ErrorObject>())({}, errors),\n );\n\nconst appendSeveralErrors: SemiGroup.Semigroup<FieldErrorWithPath> = {\n concat: (a, b) => ({\n ...b,\n types: { ...a.types, [a.type]: a.message, [b.type]: b.message },\n }),\n};\n\nconst concatToMultipleErrors = (\n errors: ReadonlyArray<FieldErrorWithPath>,\n): ErrorObject =>\n pipe(\n ReadonlyRecord.fromFoldableMap(appendSeveralErrors, ReadonlyArray.Foldable)(\n errors,\n (error) => [error.path, error],\n ),\n ReadonlyRecord.map((errorWithPath) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { path, ...error } = errorWithPath;\n\n return error;\n }),\n );\n\nconst errorsToRecord =\n (validateAllFieldCriteria: boolean) =>\n (validationErrors: ReadonlyArray<ValidationError>): ErrorObject => {\n const concat = validateAllFieldCriteria\n ? concatToMultipleErrors\n : concatToSingleError;\n\n return pipe(validationErrors, ReadonlyArray.map(formatError), concat);\n };\n\nexport default errorsToRecord;\n","import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport * as Either from 'fp-ts/Either';\nimport { pipe } from 'fp-ts/function';\nimport * as t from 'io-ts';\nimport { FieldValues, Resolver } from 'react-hook-form';\nimport errorsToRecord from './errorsToRecord';\n\n/**\n * Creates a resolver for react-hook-form using io-ts schema validation\n * @param {t.Type<TFieldValues, T>} schema - The io-ts schema to validate against\n * @param {Object} options - Additional resolver configuration\n * @param {string} [options.mode='async'] - Validation mode\n * @returns {Resolver<t.OutputOf<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = t.type({\n * name: t.string,\n * age: t.number\n * });\n *\n * useForm({\n * resolver: ioTsResolver(schema)\n * });\n */\nexport function ioTsResolver<\n T extends Record<string, any>,\n TFieldValues extends FieldValues,\n>(schema: t.Type<TFieldValues, T>): Resolver<t.OutputOf<typeof schema>> {\n return (values, _context, options) =>\n pipe(\n values,\n schema.decode,\n Either.mapLeft(\n errorsToRecord(\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n ),\n Either.mapLeft((errors) => toNestErrors(errors, options)),\n Either.fold(\n (errors) => ({\n values: {},\n errors,\n }),\n (values) => {\n options.shouldUseNativeValidation &&\n validateFieldsNatively({}, options);\n\n return {\n values,\n errors: {},\n };\n },\n ),\n );\n}\n"],"names":["arrayToPath","paths","reduce","previous","path","index","pipe","Either","fold","key","_excluded","INSTANCE_TYPES_TO_FILTER","TaggedUnionType","UnionType","IntersectionType","ExactType","RefinementType","formatError","e","context","ReadonlyArray","filterMapWithIndex","contextEntry","previousIndex","previousContextEntry","undefined","some","type","Option","none","map","_ref","k","parseInt","fromPredicate","not","Number","isNaN","toArray","message","fromNullable","mapLeft","flow","last","name","JSON","stringify","actual","getOrElseW","absurd","identity","getOrElse","concatToSingleError","errors","error","_ref2","SemiGroup","concat","first","second","Object","assign","appendSeveralErrors","a","b","_extends2","_extends","types","concatToMultipleErrors","ReadonlyRecord","fromFoldableMap","Foldable","errorWithPath","_objectWithoutPropertiesLoose","schema","values","_context","options","decode","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","validationErrors","toNestErrors","validateFieldsNatively"],"mappings":"szBAGA,IAAMA,EAAc,SAACC,UACnBA,EAAMC,OACJ,SAACC,EAAUC,EAAMC,GAAK,OACpBC,EAAAA,KACEF,EACAG,EAAOC,KACL,SAACC,GAAG,OAAQJ,EAAQ,EAAI,IAAM,IAAKI,CAAG,EACtC,SAACA,GAAYA,MAAAA,IAAAA,QAEf,SAACL,YAAYD,EAAWC,CAAI,EAC7B,EACH,GACD,ECfHM,EAAA,CAAA,QAqBMC,EAA2B,CAC/BC,EAAeA,gBACfC,EAASA,UACTC,mBACAC,EAAAA,UACAC,EAAAA,gBA6BIC,EAAc,SAACC,GACnB,IA5BuBC,EA4BjBf,EA3BNE,EAAAA,KADuBa,EA4BMD,EAAEC,QAzB7BC,EAAcC,mBAAmB,SAAChB,EAAOiB,GACvC,IAAMC,EAAgBlB,EAAQ,EACxBmB,GACe,IAAnBD,OAAuBE,EAAYN,EAAQI,GAO7C,YAL2BE,IAAzBD,GACAb,EAAyBe,KACvB,SAACC,UAASH,EAAqBG,gBAAgBA,CAAI,GAG7BC,EAAOC,KAAOD,EAAOF,KAAKJ,EACtD,GACAF,EAAcU,IAAI,SAAAC,GAAM,OAAAA,EAAHtB,GAAa,GAClCW,EAAcU,IAAI,SAACrB,UACjBH,OACEG,EACA,SAACuB,GAAC,OAAKC,SAASD,EAAG,GAAG,EACtBzB,EAAO2B,cAAcC,EAAAA,IAAYC,OAAOC,OAAQ,WAAM,OAAA5B,CAAG,GAC1D,GAEHW,EAAckB,QACdtC,GAiCF,MAAO,CAAEuC,QA3BOjC,OACdY,EAAEqB,QACFhC,EAAOiC,aAAatB,EAAEC,SACtBZ,EAAOkC,QACLC,EAAAA,KACEtB,EAAcuB,KACdf,EAAOE,IACL,SAACR,GAAY,MAAA,YACCA,EAAaK,KAAKiB,iBAAgBC,KAAKC,UACjDxB,EAAayB,OACd,GAELnB,EAAOoB,WAAW,kBAChBC,EAAMA,OAAS,gCAAyC,KAI9D1C,EAAOyC,WAAWE,EAAAA,WAUFvB,KAPLrB,EAAAA,KACXY,EAAEC,QACFC,EAAcuB,KACdf,EAAOE,IAAI,SAACR,GAAY,OAAKA,EAAaK,KAAKiB,IAAI,GACnDhB,EAAOuB,UAAU,WAAA,MAAM,SAAS,IAGV/C,KAAAA,EAC1B,EAUMgD,EAAsB,SAC1BC,GAAyC,OAEzC/C,OACE+C,EACAjC,EAAcU,IAAI,SAACwB,GAAK,IAAAC,EAAAA,OAAAA,MACrBD,EAAMlD,MAAO,CACZuB,KAAM2B,EAAM3B,KACZY,QAASe,EAAMf,SAChBgB,CAAA,GAEH,SAACF,GAAW,OAAAG,EAAUhD,KAfM,CAC9BiD,OAAQ,SAACC,EAAOC,UAAWC,OAAOC,OAAO,GAAIF,EAAQD,EAAM,GAc7CF,CAAkD,GAAIH,EAAO,EAC1E,EAEGS,EAA+D,CACnEL,OAAQ,SAACM,EAAGC,GAAC,IAAAC,EAAAC,OAAAA,EACRF,CAAAA,EAAAA,GACHG,MAAKD,EAAA,CAAA,EAAOH,EAAEI,OAAKF,KAAAA,EAAGF,EAAEpC,MAAOoC,EAAExB,QAAO0B,EAAGD,EAAErC,MAAOqC,EAAEzB,QAAO0B,KAAE,GAI7DG,EAAyB,SAC7Bf,GAEA,OAAA/C,EAAIA,KACF+D,EAAeC,gBAAgBR,EAAqB1C,EAAcmD,SAAlEF,CACEhB,EACA,SAACC,GAAU,MAAA,CAACA,EAAMlD,KAAMkD,EAAM,GAEhCe,EAAevC,IAAI,SAAC0C,GAIlB,kJAFsBC,CAAKD,EAAa9D,EAG1C,GACD,uBC5Ga,SAGdgE,GACA,OAAO,SAACC,EAAQC,EAAUC,GAAO,OAC/BvE,EAAIA,KACFqE,EACAD,EAAOI,OACPvE,EAAOkC,SDuGVsC,GCrGQF,EAAQG,2BAAsD,QAAzBH,EAAQI,sBDsGrDC,GACC,IAAMzB,EAASsB,EACXX,EACAhB,EAEJ,OAAO9C,EAAAA,KAAK4E,EAAkB9D,EAAcU,IAAIb,GAAcwC,EAChE,ICzGIlD,EAAOkC,QAAQ,SAACY,GAAW,OAAA8B,EAAAA,aAAa9B,EAAQwB,EAAQ,GACxDtE,EAAOC,KACL,SAAC6C,GAAM,MAAM,CACXsB,OAAQ,CAAA,EACRtB,OAAAA,EACD,EACD,SAACsB,GAIC,OAHAE,EAAQG,2BACNI,EAAAA,uBAAuB,CAAE,EAAEP,GAEtB,CACLF,OAAAA,EACAtB,OAAQ,GAEZ,IDoFN,IAAC0B,CClFE,CACL"}
|
package/io-ts/dist/io-ts.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as n,validateFieldsNatively as t}from"@hookform/resolvers";import*as r from"fp-ts/Either";import{pipe as e,flow as o,absurd as u,identity as a,not as i}from"fp-ts/function";import*as f from"fp-ts/Option";import*as s from"fp-ts/ReadonlyArray";import*as c from"fp-ts/ReadonlyRecord";import*as p from"fp-ts/Semigroup";import{TaggedUnionType as m,UnionType as l,IntersectionType as d,ExactType as v,RefinementType as y}from"io-ts";function g(){return g=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var e in r)({}).hasOwnProperty.call(r,e)&&(n[e]=r[e])}return n},g.apply(null,arguments)}var h=function(n){return n.reduce(function(n,t,o){return e(t,r.fold(function(n){return(o>0?".":"")+n},function(n){return"["+n+"]"}),function(t){return""+n+t})},"")},O=["path"],b=[m,l,d,v,y],x=function(n){var t,c=e(t=n.context,s.filterMapWithIndex(function(n,r){var e=n-1,o=-1===e?void 0:t[e];return void 0===o||b.some(function(n){return o.type instanceof n})?f.none:f.some(r)}),s.map(function(n){return n.key}),s.map(function(n){return e(n,function(n){return parseInt(n,10)},r.fromPredicate(i(Number.isNaN),function(){return n}))}),s.toArray,h);return{message:e(n.message,r.fromNullable(n.context),r.mapLeft(o(s.last,f.map(function(n){return"expected "+n.type.name+" but got "+JSON.stringify(n.actual)}),f.getOrElseW(function(){return u("Error context is missing name")}))),r.getOrElseW(a)),type:e(n.context,s.last,f.map(function(n){return n.type.name}),f.getOrElse(function(){return"unknown"})),path:c}},N=function(n){return e(n,s.map(function(n){var t;return(t={})[n.path]={type:n.type,message:n.message},t}),function(n){return p.fold({concat:function(n,t){return Object.assign({},t,n)}})({},n)})},E={concat:function(n,t){var r;return g({},t,{types:g({},n.types,(r={},r[n.type]=n.message,r[t.type]=t.message,r))})}},j=function(n){return e(c.fromFoldableMap(E,s.Foldable)(n,function(n){return[n.path,n]}),c.map(function(n){return function(n,t){if(null==n)return{};var r={};for(var e in n)if({}.hasOwnProperty.call(n,e)){if(-1!==t.indexOf(e))continue;r[e]=n[e]}return r}(n,O)}))}
|
|
1
|
+
import{toNestErrors as n,validateFieldsNatively as t}from"@hookform/resolvers";import*as r from"fp-ts/Either";import{pipe as e,flow as o,absurd as u,identity as a,not as i}from"fp-ts/function";import*as f from"fp-ts/Option";import*as s from"fp-ts/ReadonlyArray";import*as c from"fp-ts/ReadonlyRecord";import*as p from"fp-ts/Semigroup";import{TaggedUnionType as m,UnionType as l,IntersectionType as d,ExactType as v,RefinementType as y}from"io-ts";function g(){return g=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var e in r)({}).hasOwnProperty.call(r,e)&&(n[e]=r[e])}return n},g.apply(null,arguments)}var h=function(n){return n.reduce(function(n,t,o){return e(t,r.fold(function(n){return(o>0?".":"")+n},function(n){return"["+n+"]"}),function(t){return""+n+t})},"")},O=["path"],b=[m,l,d,v,y],x=function(n){var t,c=e(t=n.context,s.filterMapWithIndex(function(n,r){var e=n-1,o=-1===e?void 0:t[e];return void 0===o||b.some(function(n){return o.type instanceof n})?f.none:f.some(r)}),s.map(function(n){return n.key}),s.map(function(n){return e(n,function(n){return parseInt(n,10)},r.fromPredicate(i(Number.isNaN),function(){return n}))}),s.toArray,h);return{message:e(n.message,r.fromNullable(n.context),r.mapLeft(o(s.last,f.map(function(n){return"expected "+n.type.name+" but got "+JSON.stringify(n.actual)}),f.getOrElseW(function(){return u("Error context is missing name")}))),r.getOrElseW(a)),type:e(n.context,s.last,f.map(function(n){return n.type.name}),f.getOrElse(function(){return"unknown"})),path:c}},N=function(n){return e(n,s.map(function(n){var t;return(t={})[n.path]={type:n.type,message:n.message},t}),function(n){return p.fold({concat:function(n,t){return Object.assign({},t,n)}})({},n)})},E={concat:function(n,t){var r;return g({},t,{types:g({},n.types,(r={},r[n.type]=n.message,r[t.type]=t.message,r))})}},j=function(n){return e(c.fromFoldableMap(E,s.Foldable)(n,function(n){return[n.path,n]}),c.map(function(n){return function(n,t){if(null==n)return{};var r={};for(var e in n)if({}.hasOwnProperty.call(n,e)){if(-1!==t.indexOf(e))continue;r[e]=n[e]}return r}(n,O)}))};function k(o){return function(u,a,i){return e(u,o.decode,r.mapLeft((f=!i.shouldUseNativeValidation&&"all"===i.criteriaMode,function(n){var t=f?j:N;return e(n,s.map(x),t)})),r.mapLeft(function(t){return n(t,i)}),r.fold(function(n){return{values:{},errors:n}},function(n){return i.shouldUseNativeValidation&&t({},i),{values:n,errors:{}}}));var f}}export{k as ioTsResolver};
|
|
2
2
|
//# sourceMappingURL=io-ts.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as e,validateFieldsNatively as t}from"@hookform/resolvers";import*as r from"fp-ts/Either";import{pipe as o,flow as a,absurd as
|
|
1
|
+
import{toNestErrors as e,validateFieldsNatively as t}from"@hookform/resolvers";import*as r from"fp-ts/Either";import{pipe as o,flow as a,absurd as n,identity as s,not as p}from"fp-ts/function";import*as i from"fp-ts/Option";import*as m from"fp-ts/ReadonlyArray";import*as f from"fp-ts/ReadonlyRecord";import*as l from"fp-ts/Semigroup";import{TaggedUnionType as c,UnionType as u,IntersectionType as d,ExactType as y,RefinementType as g}from"io-ts";function v(){return v=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)({}).hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},v.apply(null,arguments)}const h=e=>e.reduce((e,t,a)=>o(t,r.fold(e=>`${a>0?".":""}${e}`,e=>`[${e}]`),t=>`${e}${t}`),""),O=["path"],b=[c,u,d,y,g],x=e=>{const t=o(f=e.context,m.filterMapWithIndex((e,t)=>{const r=e-1,o=-1===r?void 0:f[r];return void 0===o||b.some(e=>o.type instanceof e)?i.none:i.some(t)}),m.map(({key:e})=>e),m.map(e=>o(e,e=>parseInt(e,10),r.fromPredicate(p(Number.isNaN),()=>e))),m.toArray,h);var f;return{message:o(e.message,r.fromNullable(e.context),r.mapLeft(a(m.last,i.map(e=>`expected ${e.type.name} but got ${JSON.stringify(e.actual)}`),i.getOrElseW(()=>n("Error context is missing name")))),r.getOrElseW(s)),type:o(e.context,m.last,i.map(e=>e.type.name),i.getOrElse(()=>"unknown")),path:t}},N=e=>o(e,m.map(e=>({[e.path]:{type:e.type,message:e.message}})),e=>l.fold({concat:(e,t)=>Object.assign({},t,e)})({},e)),$={concat:(e,t)=>v({},t,{types:v({},e.types,{[e.type]:e.message,[t.type]:t.message})})},E=e=>o(f.fromFoldableMap($,m.Foldable)(e,e=>[e.path,e]),f.map(e=>function(e,t){if(null==e)return{};var r={};for(var o in e)if({}.hasOwnProperty.call(e,o)){if(-1!==t.indexOf(o))continue;r[o]=e[o]}return r}(e,O)));function j(a){return(n,s,p)=>{return o(n,a.decode,r.mapLeft((i=!p.shouldUseNativeValidation&&"all"===p.criteriaMode,e=>{const t=i?E:N;return o(e,m.map(x),t)})),r.mapLeft(t=>e(t,p)),r.fold(e=>({values:{},errors:e}),e=>(p.shouldUseNativeValidation&&t({},p),{values:e,errors:{}})));var i}}export{j as ioTsResolver};
|
|
2
2
|
//# sourceMappingURL=io-ts.modern.mjs.map
|