@hookform/resolvers 3.10.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 +108 -6
- package/ajv/dist/ajv.d.ts +20 -0
- package/ajv/dist/ajv.js +1 -1
- package/ajv/dist/ajv.js.map +1 -1
- package/ajv/dist/ajv.mjs +1 -1
- package/ajv/dist/ajv.modern.mjs +1 -1
- package/ajv/dist/ajv.modern.mjs.map +1 -1
- package/ajv/dist/ajv.module.js +1 -1
- package/ajv/dist/ajv.module.js.map +1 -1
- package/ajv/dist/ajv.umd.js +1 -1
- package/ajv/dist/ajv.umd.js.map +1 -1
- package/ajv/dist/types.d.ts +8 -0
- package/ajv/src/__tests__/__fixtures__/data-errors.ts +216 -0
- package/ajv/src/__tests__/__snapshots__/ajv-errors.ts.snap +462 -0
- package/ajv/src/__tests__/__snapshots__/ajv.ts.snap +6 -6
- package/ajv/src/__tests__/ajv-errors.ts +227 -0
- package/ajv/src/ajv.ts +47 -14
- package/ajv/src/types.ts +8 -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/package.json +1 -1
- package/arktype/src/__tests__/Form-native-validation.tsx +3 -3
- package/arktype/src/__tests__/Form.tsx +34 -8
- package/arktype/src/__tests__/__fixtures__/data.ts +2 -2
- package/arktype/src/__tests__/__snapshots__/arktype.ts.snap +22 -415
- package/arktype/src/arktype.ts +46 -15
- 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 +22 -11
- package/class-validator/src/class-validator.ts +47 -13
- 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/dist/resolvers.js +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/resolvers.mjs +1 -1
- package/dist/resolvers.mjs.map +1 -1
- package/dist/resolvers.module.js +1 -1
- package/dist/resolvers.module.js.map +1 -1
- package/dist/resolvers.umd.js +1 -1
- package/dist/resolvers.umd.js.map +1 -1
- package/effect-ts/dist/effect-ts.d.ts +19 -2
- package/effect-ts/dist/effect-ts.js +1 -1
- package/effect-ts/dist/effect-ts.js.map +1 -1
- package/effect-ts/dist/effect-ts.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs +1 -1
- package/effect-ts/dist/effect-ts.modern.mjs.map +1 -1
- package/effect-ts/dist/effect-ts.module.js +1 -1
- package/effect-ts/dist/effect-ts.module.js.map +1 -1
- package/effect-ts/dist/effect-ts.umd.js +1 -1
- package/effect-ts/dist/effect-ts.umd.js.map +1 -1
- package/effect-ts/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/__tests__/__snapshots__/effect-ts.ts.snap +35 -1
- package/effect-ts/src/__tests__/effect-ts.ts +61 -0
- package/effect-ts/src/effect-ts.ts +60 -13
- 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 +1 -1
- package/io-ts/dist/io-ts.js.map +1 -1
- package/io-ts/dist/io-ts.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs +1 -1
- package/io-ts/dist/io-ts.modern.mjs.map +1 -1
- package/io-ts/dist/io-ts.module.js +1 -1
- package/io-ts/dist/io-ts.module.js.map +1 -1
- package/io-ts/dist/io-ts.umd.js +1 -1
- package/io-ts/dist/io-ts.umd.js.map +1 -1
- package/io-ts/src/__tests__/Form-native-validation.tsx +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 +49 -34
- package/standard-schema/dist/index.d.ts +1 -0
- package/standard-schema/dist/standard-schema.d.ts +25 -0
- package/standard-schema/dist/standard-schema.js +2 -0
- package/standard-schema/dist/standard-schema.js.map +1 -0
- package/standard-schema/dist/standard-schema.mjs +2 -0
- package/standard-schema/dist/standard-schema.modern.mjs +2 -0
- package/standard-schema/dist/standard-schema.modern.mjs.map +1 -0
- package/standard-schema/dist/standard-schema.module.js +2 -0
- package/standard-schema/dist/standard-schema.module.js.map +1 -0
- package/standard-schema/dist/standard-schema.umd.js +2 -0
- package/standard-schema/dist/standard-schema.umd.js.map +1 -0
- package/standard-schema/package.json +18 -0
- package/standard-schema/src/__tests__/Form-native-validation.tsx +82 -0
- package/standard-schema/src/__tests__/Form.tsx +82 -0
- package/standard-schema/src/__tests__/__fixtures__/data.ts +88 -0
- package/standard-schema/src/__tests__/__snapshots__/standard-schema.ts.snap +250 -0
- package/standard-schema/src/__tests__/standard-schema.ts +56 -0
- package/standard-schema/src/index.ts +1 -0
- package/standard-schema/src/standard-schema.ts +88 -0
- package/superstruct/dist/index.d.ts +0 -1
- package/superstruct/dist/superstruct.d.ts +22 -2
- package/superstruct/dist/superstruct.js +1 -1
- 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 +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 +32 -10
- 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/__tests__/__snapshots__/typebox-compiler.ts.snap +21 -21
- package/typebox/src/__tests__/__snapshots__/typebox.ts.snap +21 -21
- 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 +1 -1
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.mjs +1 -1
- package/typeschema/dist/typeschema.modern.mjs +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/src/typeschema.ts +20 -1
- package/valibot/dist/index.d.ts +0 -1
- package/valibot/dist/valibot.d.ts +31 -2
- package/valibot/dist/valibot.js +1 -1
- package/valibot/dist/valibot.js.map +1 -1
- package/valibot/dist/valibot.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs +1 -1
- package/valibot/dist/valibot.modern.mjs.map +1 -1
- package/valibot/dist/valibot.module.js +1 -1
- package/valibot/dist/valibot.module.js.map +1 -1
- package/valibot/dist/valibot.umd.js +1 -1
- package/valibot/dist/valibot.umd.js.map +1 -1
- package/valibot/package.json +1 -1
- package/valibot/src/__tests__/Form.tsx +26 -0
- package/valibot/src/__tests__/__fixtures__/data.ts +7 -3
- package/valibot/src/__tests__/valibot.ts +1 -1
- package/valibot/src/index.ts +0 -1
- package/valibot/src/valibot.ts +55 -6
- 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 +38 -10
- package/yup/dist/yup.d.ts +18 -7
- package/yup/dist/yup.js +1 -1
- package/yup/dist/yup.js.map +1 -1
- package/yup/dist/yup.mjs +1 -1
- package/yup/dist/yup.modern.mjs +1 -1
- package/yup/dist/yup.modern.mjs.map +1 -1
- package/yup/dist/yup.module.js +1 -1
- package/yup/dist/yup.module.js.map +1 -1
- package/yup/dist/yup.umd.js +1 -1
- package/yup/dist/yup.umd.js.map +1 -1
- package/yup/src/yup.ts +22 -11
- 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 +40 -9
- 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/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
package/typebox/src/typebox.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
|
2
|
+
import { Static, Type } from '@sinclair/typebox';
|
|
2
3
|
import { TypeCheck } from '@sinclair/typebox/compiler';
|
|
3
4
|
import { Value, type ValueError } from '@sinclair/typebox/value';
|
|
4
|
-
import {
|
|
5
|
-
|
|
5
|
+
import {
|
|
6
|
+
FieldError,
|
|
7
|
+
FieldErrors,
|
|
8
|
+
FieldValues,
|
|
9
|
+
Resolver,
|
|
10
|
+
appendErrors,
|
|
11
|
+
} from 'react-hook-form';
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
function parseErrorSchema(
|
|
8
14
|
_errors: ValueError[],
|
|
9
15
|
validateAllFieldCriteria: boolean,
|
|
10
|
-
)
|
|
16
|
+
) {
|
|
11
17
|
const errors: Record<string, FieldError> = {};
|
|
12
18
|
for (; _errors.length; ) {
|
|
13
19
|
const error = _errors[0];
|
|
@@ -37,10 +43,29 @@ const parseErrorSchema = (
|
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
return errors;
|
|
40
|
-
}
|
|
46
|
+
}
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Creates a resolver for react-hook-form using Typebox schema validation
|
|
50
|
+
* @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against
|
|
51
|
+
* @param {Object} options - Additional resolver configuration
|
|
52
|
+
* @param {string} [options.mode='async'] - Validation mode
|
|
53
|
+
* @returns {Resolver<Static<Schema>>} A resolver function compatible with react-hook-form
|
|
54
|
+
* @example
|
|
55
|
+
* const schema = Type.Object({
|
|
56
|
+
* name: Type.String(),
|
|
57
|
+
* age: Type.Number()
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* useForm({
|
|
61
|
+
* resolver: typeboxResolver(schema)
|
|
62
|
+
* });
|
|
63
|
+
*/
|
|
64
|
+
export function typeboxResolver<
|
|
65
|
+
TFieldValues extends FieldValues,
|
|
66
|
+
Schema extends ReturnType<typeof Type.Object<TFieldValues>>,
|
|
67
|
+
>(schema: Schema | TypeCheck<Schema>): Resolver<Static<Schema>> {
|
|
68
|
+
return async (values, _, options) => {
|
|
44
69
|
const errors = Array.from(
|
|
45
70
|
schema instanceof TypeCheck
|
|
46
71
|
? schema.Errors(values)
|
|
@@ -67,3 +92,4 @@ export const typeboxResolver: Resolver =
|
|
|
67
92
|
),
|
|
68
93
|
};
|
|
69
94
|
};
|
|
95
|
+
}
|
|
@@ -1,2 +1,19 @@
|
|
|
1
1
|
import type { Resolver } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a resolver for react-hook-form using TypeSchema validation
|
|
4
|
+
* @param {any} schema - The TypeSchema to validate against
|
|
5
|
+
* @param {any} _ - Unused parameter
|
|
6
|
+
* @param {Object} resolverOptions - Additional resolver configuration
|
|
7
|
+
* @param {string} [resolverOptions.mode='async'] - Validation mode
|
|
8
|
+
* @returns {Resolver} A resolver function compatible with react-hook-form
|
|
9
|
+
* @example
|
|
10
|
+
* const schema = z.object({
|
|
11
|
+
* name: z.string().required(),
|
|
12
|
+
* age: z.number().required(),
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* useForm({
|
|
16
|
+
* resolver: typeschemaResolver(schema)
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
2
19
|
export declare const typeschemaResolver: Resolver;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@hookform/resolvers"),r=require("@typeschema/main"),s=require("react-hook-form"),a=function(e,r){for(var a={};e.length;){var t=e[0];if(t.path){var o=t.path.join(".");if(a[o]||(a[o]={message:t.message,type:""}),r){var i=a[o].types,n=i&&i[""];a[o]=s.appendErrors(o,r,a,"",n?[].concat(n,t.message):t.message)}e.shift()}}return a};exports.typeschemaResolver=function(s,t,o){return void 0===o&&(o={}),function(t,i,n){try{return Promise.resolve(r.validate(s,t)).then(function(r){return n.shouldUseNativeValidation&&e.validateFieldsNatively({},n),r.success?{errors:{},values:o.raw?t:r.data}:{values:{},errors:e.toNestErrors(a(r.issues,!n.shouldUseNativeValidation&&"all"===n.criteriaMode),n)}})}catch(e){return Promise.reject(e)}}};
|
|
1
|
+
var e=require("@hookform/resolvers"),r=require("@typeschema/main"),s=require("react-hook-form"),a=function(e,r){for(var a={};e.length;){var t=e[0];if(t.path){var o=t.path.join(".");if(a[o]||(a[o]={message:t.message,type:""}),r){var i=a[o].types,n=i&&i[""];a[o]=s.appendErrors(o,r,a,"",n?[].concat(n,t.message):t.message)}e.shift()}}return a};exports.typeschemaResolver=function(s,t,o){return void 0===o&&(o={}),function(t,i,n){try{return Promise.resolve(r.validate(s,t)).then(function(r){return n.shouldUseNativeValidation&&e.validateFieldsNatively({},n),r.success?{errors:{},values:o.raw?Object.assign({},t):r.data}:{values:{},errors:e.toNestErrors(a(r.issues,!n.shouldUseNativeValidation&&"all"===n.criteriaMode),n)}})}catch(e){return Promise.reject(e)}}};
|
|
2
2
|
//# sourceMappingURL=typeschema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeschema.js","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw
|
|
1
|
+
{"version":3,"file":"typeschema.js","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\n/**\n * Creates a resolver for react-hook-form using TypeSchema validation\n * @param {any} schema - The TypeSchema to validate against\n * @param {any} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = z.object({\n * name: z.string().required(),\n * age: z.number().required(),\n * });\n *\n * useForm({\n * resolver: typeschemaResolver(schema)\n * });\n */\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.data as any),\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","typeschemaErrors","validateAllFieldCriteria","errors","length","error","path","_path","join","message","type","types","messages","appendErrors","concat","shift","schema","_","resolverOptions","values","options","Promise","resolve","validate","then","result","shouldUseNativeValidation","validateFieldsNatively","success","raw","Object","assign","data","toNestErrors","issues","criteriaMode","e","reject"],"mappings":"gGAMMA,EAAmB,SACvBC,EACAC,GAIA,IAFA,IAAMC,EAAqC,CAAE,EAEtCF,EAAiBG,QAAU,CAChC,IAAMC,EAAQJ,EAAiB,GAE/B,GAAKI,EAAMC,KAAX,CAGA,IAAMC,EAAQF,EAAMC,KAAKE,KAAK,KAM9B,GAJKL,EAAOI,KACVJ,EAAOI,GAAS,CAAEE,QAASJ,EAAMI,QAASC,KAAM,KAG9CR,EAA0B,CAC5B,IAAMS,EAAQR,EAAOI,GAAOI,MACtBC,EAAWD,GAASA,EAAM,IAEhCR,EAAOI,GAASM,EAAYA,aAC1BN,EACAL,EACAC,EACA,GACAS,EACK,GAAgBE,OAAOF,EAAsBP,EAAMI,SACpDJ,EAAMI,QAEd,CAEAR,EAAiBc,OAtBjB,CAuBF,CAEA,OAAOZ,CACT,6BAoBE,SAACa,EAAQC,EAAGC,GACLC,YADoB,IAAfD,IAAAA,EAAkB,IACvBC,SAAAA,EAAQF,EAAGG,GAAO,WAAIC,QAAAC,QACNC,WAASP,EAAQG,IAAOK,KAAA,SAAvCC,GAEmE,OAAzEL,EAAQM,2BAA6BC,yBAAuB,CAAA,EAAIP,GAE5DK,EAAOG,QACF,CACLzB,OAAQ,CAAA,EACRgB,OAAQD,EAAgBW,IACpBC,OAAOC,OAAO,GAAIZ,GACjBM,EAAOO,MAIT,CACLb,OAAQ,CAAA,EACRhB,OAAQ8B,EAAAA,aACNjC,EACEyB,EAAOS,QACNd,EAAQM,2BAAsD,QAAzBN,EAAQe,cAEhDf,GAEH,EACH,CAAC,MAAAgB,GAAAf,OAAAA,QAAAgB,OAAAD,EAAA,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as
|
|
1
|
+
import{validateFieldsNatively as e,toNestErrors as r}from"@hookform/resolvers";import{validate as t}from"@typeschema/main";import{appendErrors as o}from"react-hook-form";var s=function(e,r){for(var t={};e.length;){var s=e[0];if(s.path){var a=s.path.join(".");if(t[a]||(t[a]={message:s.message,type:""}),r){var i=t[a].types,n=i&&i[""];t[a]=o(a,r,t,"",n?[].concat(n,s.message):s.message)}e.shift()}}return t},a=function(o,a,i){return void 0===i&&(i={}),function(a,n,m){try{return Promise.resolve(t(o,a)).then(function(t){return m.shouldUseNativeValidation&&e({},m),t.success?{errors:{},values:i.raw?Object.assign({},a):t.data}:{values:{},errors:r(s(t.issues,!m.shouldUseNativeValidation&&"all"===m.criteriaMode),m)}})}catch(e){return Promise.reject(e)}}};export{a as typeschemaResolver};
|
|
2
2
|
//# sourceMappingURL=typeschema.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as s,toNestErrors as e}from"@hookform/resolvers";import{validate as o}from"@typeschema/main";import{appendErrors as t}from"react-hook-form";const a=(s,e)=>{const o={};for(;s.length;){const a=s[0];if(!a.path)continue;const r=a.path.join(".");if(o[r]||(o[r]={message:a.message,type:""}),e){const s=o[r].types,i=s&&s[""];o[r]=t(r,e,o,"",i?[].concat(i,a.message):a.message)}s.shift()}return o},r=(t,r,i={})=>async(r,n,c)=>{const m=await o(t,r);return c.shouldUseNativeValidation&&s({},c),m.success?{errors:{},values:i.raw?r:m.data}:{values:{},errors:e(a(m.issues,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)}};export{r as typeschemaResolver};
|
|
1
|
+
import{validateFieldsNatively as s,toNestErrors as e}from"@hookform/resolvers";import{validate as o}from"@typeschema/main";import{appendErrors as t}from"react-hook-form";const a=(s,e)=>{const o={};for(;s.length;){const a=s[0];if(!a.path)continue;const r=a.path.join(".");if(o[r]||(o[r]={message:a.message,type:""}),e){const s=o[r].types,i=s&&s[""];o[r]=t(r,e,o,"",i?[].concat(i,a.message):a.message)}s.shift()}return o},r=(t,r,i={})=>async(r,n,c)=>{const m=await o(t,r);return c.shouldUseNativeValidation&&s({},c),m.success?{errors:{},values:i.raw?Object.assign({},r):m.data}:{values:{},errors:e(a(m.issues,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)}};export{r as typeschemaResolver};
|
|
2
2
|
//# sourceMappingURL=typeschema.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeschema.modern.mjs","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw
|
|
1
|
+
{"version":3,"file":"typeschema.modern.mjs","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\n/**\n * Creates a resolver for react-hook-form using TypeSchema validation\n * @param {any} schema - The TypeSchema to validate against\n * @param {any} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = z.object({\n * name: z.string().required(),\n * age: z.number().required(),\n * });\n *\n * useForm({\n * resolver: typeschemaResolver(schema)\n * });\n */\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.data as any),\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","typeschemaErrors","validateAllFieldCriteria","errors","length","error","path","_path","join","message","type","types","messages","appendErrors","concat","shift","typeschemaResolver","schema","_","resolverOptions","async","values","options","result","validate","shouldUseNativeValidation","validateFieldsNatively","success","raw","Object","assign","data","toNestErrors","issues","criteriaMode"],"mappings":"0KAMA,MAAMA,EAAmBA,CACvBC,EACAC,KAEA,MAAMC,EAAqC,CAAE,EAE7C,KAAOF,EAAiBG,QAAU,CAChC,MAAMC,EAAQJ,EAAiB,GAE/B,IAAKI,EAAMC,KACT,SAEF,MAAMC,EAAQF,EAAMC,KAAKE,KAAK,KAM9B,GAJKL,EAAOI,KACVJ,EAAOI,GAAS,CAAEE,QAASJ,EAAMI,QAASC,KAAM,KAG9CR,EAA0B,CAC5B,MAAMS,EAAQR,EAAOI,GAAOI,MACtBC,EAAWD,GAASA,EAAM,IAEhCR,EAAOI,GAASM,EACdN,EACAL,EACAC,EACA,GACAS,EACK,GAAgBE,OAAOF,EAAsBP,EAAMI,SACpDJ,EAAMI,QAEd,CAEAR,EAAiBc,OACnB,CAEA,OAAOZ,GAoBIa,EACXA,CAACC,EAAQC,EAAGC,EAAkB,CAAA,IAC9BC,MAAOC,EAAQH,EAAGI,KAChB,MAAMC,QAAeC,EAASP,EAAQI,GAItC,OAFAC,EAAQG,2BAA6BC,EAAuB,CAAA,EAAIJ,GAE5DC,EAAOI,QACF,CACLxB,OAAQ,CAAA,EACRkB,OAAQF,EAAgBS,IACpBC,OAAOC,OAAO,CAAA,EAAIT,GACjBE,EAAOQ,MAIT,CACLV,OAAQ,CAAE,EACVlB,OAAQ6B,EACNhC,EACEuB,EAAOU,QACNX,EAAQG,2BAAsD,QAAzBH,EAAQY,cAEhDZ"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{validateFieldsNatively as
|
|
1
|
+
import{validateFieldsNatively as e,toNestErrors as r}from"@hookform/resolvers";import{validate as t}from"@typeschema/main";import{appendErrors as o}from"react-hook-form";var s=function(e,r){for(var t={};e.length;){var s=e[0];if(s.path){var a=s.path.join(".");if(t[a]||(t[a]={message:s.message,type:""}),r){var i=t[a].types,n=i&&i[""];t[a]=o(a,r,t,"",n?[].concat(n,s.message):s.message)}e.shift()}}return t},a=function(o,a,i){return void 0===i&&(i={}),function(a,n,m){try{return Promise.resolve(t(o,a)).then(function(t){return m.shouldUseNativeValidation&&e({},m),t.success?{errors:{},values:i.raw?Object.assign({},a):t.data}:{values:{},errors:r(s(t.issues,!m.shouldUseNativeValidation&&"all"===m.criteriaMode),m)}})}catch(e){return Promise.reject(e)}}};export{a as typeschemaResolver};
|
|
2
2
|
//# sourceMappingURL=typeschema.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeschema.module.js","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw
|
|
1
|
+
{"version":3,"file":"typeschema.module.js","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\n/**\n * Creates a resolver for react-hook-form using TypeSchema validation\n * @param {any} schema - The TypeSchema to validate against\n * @param {any} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = z.object({\n * name: z.string().required(),\n * age: z.number().required(),\n * });\n *\n * useForm({\n * resolver: typeschemaResolver(schema)\n * });\n */\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.data as any),\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","typeschemaErrors","validateAllFieldCriteria","errors","length","error","path","_path","join","message","type","types","messages","appendErrors","concat","shift","typeschemaResolver","schema","_","resolverOptions","values","options","Promise","resolve","validate","then","result","shouldUseNativeValidation","validateFieldsNatively","success","raw","Object","assign","data","toNestErrors","issues","criteriaMode","e","reject"],"mappings":"0KAMA,IAAMA,EAAmB,SACvBC,EACAC,GAIA,IAFA,IAAMC,EAAqC,CAAE,EAEtCF,EAAiBG,QAAU,CAChC,IAAMC,EAAQJ,EAAiB,GAE/B,GAAKI,EAAMC,KAAX,CAGA,IAAMC,EAAQF,EAAMC,KAAKE,KAAK,KAM9B,GAJKL,EAAOI,KACVJ,EAAOI,GAAS,CAAEE,QAASJ,EAAMI,QAASC,KAAM,KAG9CR,EAA0B,CAC5B,IAAMS,EAAQR,EAAOI,GAAOI,MACtBC,EAAWD,GAASA,EAAM,IAEhCR,EAAOI,GAASM,EACdN,EACAL,EACAC,EACA,GACAS,EACK,GAAgBE,OAAOF,EAAsBP,EAAMI,SACpDJ,EAAMI,QAEd,CAEAR,EAAiBc,OAtBjB,CAuBF,CAEA,OAAOZ,CACT,EAmBaa,EACX,SAACC,EAAQC,EAAGC,GACLC,YADoB,IAAfD,IAAAA,EAAkB,IACvBC,SAAAA,EAAQF,EAAGG,GAAO,WAAIC,QAAAC,QACNC,EAASP,EAAQG,IAAOK,KAAA,SAAvCC,GAEmE,OAAzEL,EAAQM,2BAA6BC,EAAuB,CAAA,EAAIP,GAE5DK,EAAOG,QACF,CACL1B,OAAQ,CAAA,EACRiB,OAAQD,EAAgBW,IACpBC,OAAOC,OAAO,GAAIZ,GACjBM,EAAOO,MAIT,CACLb,OAAQ,CAAA,EACRjB,OAAQ+B,EACNlC,EACE0B,EAAOS,QACNd,EAAQM,2BAAsD,QAAzBN,EAAQe,cAEhDf,GAEH,EACH,CAAC,MAAAgB,GAAAf,OAAAA,QAAAgB,OAAAD,EAAA,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers"),require("@typeschema/main"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","@typeschema/main","react-hook-form"],o):o((e||self).hookformResolversTypeschema={},e.hookformResolvers,e.main,e.ReactHookForm)}(this,function(e,o,r,s){var t=function(e,o){for(var r={};e.length;){var t=e[0];if(t.path){var a=t.path.join(".");if(r[a]||(r[a]={message:t.message,type:""}),o){var i=r[a].types,n=i&&i[""];r[a]=s.appendErrors(a,o,r,"",n?[].concat(n,t.message):t.message)}e.shift()}}return r};e.typeschemaResolver=function(e,s,a){return void 0===a&&(a={}),function(s,i,n){try{return Promise.resolve(r.validate(e,s)).then(function(e){return n.shouldUseNativeValidation&&o.validateFieldsNatively({},n),e.success?{errors:{},values:a.raw?s:e.data}:{values:{},errors:o.toNestErrors(t(e.issues,!n.shouldUseNativeValidation&&"all"===n.criteriaMode),n)}})}catch(e){return Promise.reject(e)}}}});
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers"),require("@typeschema/main"),require("react-hook-form")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","@typeschema/main","react-hook-form"],o):o((e||self).hookformResolversTypeschema={},e.hookformResolvers,e.main,e.ReactHookForm)}(this,function(e,o,r,s){var t=function(e,o){for(var r={};e.length;){var t=e[0];if(t.path){var a=t.path.join(".");if(r[a]||(r[a]={message:t.message,type:""}),o){var i=r[a].types,n=i&&i[""];r[a]=s.appendErrors(a,o,r,"",n?[].concat(n,t.message):t.message)}e.shift()}}return r};e.typeschemaResolver=function(e,s,a){return void 0===a&&(a={}),function(s,i,n){try{return Promise.resolve(r.validate(e,s)).then(function(e){return n.shouldUseNativeValidation&&o.validateFieldsNatively({},n),e.success?{errors:{},values:a.raw?Object.assign({},s):e.data}:{values:{},errors:o.toNestErrors(t(e.issues,!n.shouldUseNativeValidation&&"all"===n.criteriaMode),n)}})}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=typeschema.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeschema.umd.js","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw
|
|
1
|
+
{"version":3,"file":"typeschema.umd.js","sources":["../src/typeschema.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { ValidationIssue } from '@typeschema/core';\nimport { validate } from '@typeschema/main';\nimport { FieldError, FieldErrors, appendErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrorSchema = (\n typeschemaErrors: ValidationIssue[],\n validateAllFieldCriteria: boolean,\n): FieldErrors => {\n const errors: Record<string, FieldError> = {};\n\n for (; typeschemaErrors.length; ) {\n const error = typeschemaErrors[0];\n\n if (!error.path) {\n continue;\n }\n const _path = error.path.join('.');\n\n if (!errors[_path]) {\n errors[_path] = { message: error.message, type: '' };\n }\n\n if (validateAllFieldCriteria) {\n const types = errors[_path].types;\n const messages = types && types[''];\n\n errors[_path] = appendErrors(\n _path,\n validateAllFieldCriteria,\n errors,\n '',\n messages\n ? ([] as string[]).concat(messages as string[], error.message)\n : error.message,\n ) as FieldError;\n }\n\n typeschemaErrors.shift();\n }\n\n return errors;\n};\n\n/**\n * Creates a resolver for react-hook-form using TypeSchema validation\n * @param {any} schema - The TypeSchema to validate against\n * @param {any} _ - Unused parameter\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {string} [resolverOptions.mode='async'] - Validation mode\n * @returns {Resolver} A resolver function compatible with react-hook-form\n * @example\n * const schema = z.object({\n * name: z.string().required(),\n * age: z.number().required(),\n * });\n *\n * useForm({\n * resolver: typeschemaResolver(schema)\n * });\n */\nexport const typeschemaResolver: Resolver =\n (schema, _, resolverOptions = {}) =>\n async (values, _, options) => {\n const result = await validate(schema, values);\n\n options.shouldUseNativeValidation && validateFieldsNatively({}, options);\n\n if (result.success) {\n return {\n errors: {} as FieldErrors,\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.data as any),\n };\n }\n\n return {\n values: {},\n errors: toNestErrors(\n parseErrorSchema(\n result.issues,\n !options.shouldUseNativeValidation && options.criteriaMode === 'all',\n ),\n options,\n ),\n };\n };\n"],"names":["parseErrorSchema","typeschemaErrors","validateAllFieldCriteria","errors","length","error","path","_path","join","message","type","types","messages","appendErrors","concat","shift","schema","_","resolverOptions","values","options","Promise","resolve","validate","then","result","shouldUseNativeValidation","validateFieldsNatively","success","raw","Object","assign","data","toNestErrors","issues","criteriaMode","e","reject"],"mappings":"ubAMA,IAAMA,EAAmB,SACvBC,EACAC,GAIA,IAFA,IAAMC,EAAqC,CAAE,EAEtCF,EAAiBG,QAAU,CAChC,IAAMC,EAAQJ,EAAiB,GAE/B,GAAKI,EAAMC,KAAX,CAGA,IAAMC,EAAQF,EAAMC,KAAKE,KAAK,KAM9B,GAJKL,EAAOI,KACVJ,EAAOI,GAAS,CAAEE,QAASJ,EAAMI,QAASC,KAAM,KAG9CR,EAA0B,CAC5B,IAAMS,EAAQR,EAAOI,GAAOI,MACtBC,EAAWD,GAASA,EAAM,IAEhCR,EAAOI,GAASM,EAAYA,aAC1BN,EACAL,EACAC,EACA,GACAS,EACK,GAAgBE,OAAOF,EAAsBP,EAAMI,SACpDJ,EAAMI,QAEd,CAEAR,EAAiBc,OAtBjB,CAuBF,CAEA,OAAOZ,CACT,uBAoBE,SAACa,EAAQC,EAAGC,GACLC,YADoB,IAAfD,IAAAA,EAAkB,IACvBC,SAAAA,EAAQF,EAAGG,GAAO,WAAIC,QAAAC,QACNC,WAASP,EAAQG,IAAOK,KAAA,SAAvCC,GAEmE,OAAzEL,EAAQM,2BAA6BC,yBAAuB,CAAA,EAAIP,GAE5DK,EAAOG,QACF,CACLzB,OAAQ,CAAA,EACRgB,OAAQD,EAAgBW,IACpBC,OAAOC,OAAO,GAAIZ,GACjBM,EAAOO,MAIT,CACLb,OAAQ,CAAA,EACRhB,OAAQ8B,EAAAA,aACNjC,EACEyB,EAAOS,QACNd,EAAQM,2BAAsD,QAAzBN,EAAQe,cAEhDf,GAEH,EACH,CAAC,MAAAgB,GAAAf,OAAAA,QAAAgB,OAAAD,EAAA,CAAA,CAAA"}
|
|
@@ -43,6 +43,23 @@ const parseErrorSchema = (
|
|
|
43
43
|
return errors;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Creates a resolver for react-hook-form using TypeSchema validation
|
|
48
|
+
* @param {any} schema - The TypeSchema to validate against
|
|
49
|
+
* @param {any} _ - Unused parameter
|
|
50
|
+
* @param {Object} resolverOptions - Additional resolver configuration
|
|
51
|
+
* @param {string} [resolverOptions.mode='async'] - Validation mode
|
|
52
|
+
* @returns {Resolver} A resolver function compatible with react-hook-form
|
|
53
|
+
* @example
|
|
54
|
+
* const schema = z.object({
|
|
55
|
+
* name: z.string().required(),
|
|
56
|
+
* age: z.number().required(),
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* useForm({
|
|
60
|
+
* resolver: typeschemaResolver(schema)
|
|
61
|
+
* });
|
|
62
|
+
*/
|
|
46
63
|
export const typeschemaResolver: Resolver =
|
|
47
64
|
(schema, _, resolverOptions = {}) =>
|
|
48
65
|
async (values, _, options) => {
|
|
@@ -53,7 +70,9 @@ export const typeschemaResolver: Resolver =
|
|
|
53
70
|
if (result.success) {
|
|
54
71
|
return {
|
|
55
72
|
errors: {} as FieldErrors,
|
|
56
|
-
values: resolverOptions.raw
|
|
73
|
+
values: resolverOptions.raw
|
|
74
|
+
? Object.assign({}, values)
|
|
75
|
+
: (result.data as any),
|
|
57
76
|
};
|
|
58
77
|
}
|
|
59
78
|
|
package/valibot/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { FieldValues, Resolver } from 'react-hook-form';
|
|
2
|
+
import { BaseSchema, BaseSchemaAsync, Config, InferIssue, InferOutput } from 'valibot';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resolver for react-hook-form using Valibot schema validation
|
|
5
|
+
* @param {BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>} schema - The Valibot schema to validate against
|
|
6
|
+
* @param {Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>} [schemaOptions] - Optional Valibot validation options
|
|
7
|
+
* @param {Object} resolverOptions - Additional resolver configuration
|
|
8
|
+
* @param {('sync' | 'async')} [resolverOptions.mode] - Validation mode
|
|
9
|
+
* @param {boolean} [resolverOptions.raw] - If true, returns raw values rather than validated results
|
|
10
|
+
* @returns {Resolver<InferOutput<typeof schema>>} A resolver function compatible with react-hook-form
|
|
11
|
+
* @example
|
|
12
|
+
* const schema = valibot.object({
|
|
13
|
+
* name: valibot.string().minLength(2),
|
|
14
|
+
* age: valibot.number().min(18)
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* useForm({
|
|
18
|
+
* resolver: valibotResolver(schema)
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
export declare function valibotResolver<TFieldValues extends FieldValues>(schema: BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>, schemaOptions?: Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>, resolverOptions?: {
|
|
22
|
+
/**
|
|
23
|
+
* @default async
|
|
24
|
+
*/
|
|
25
|
+
mode?: 'sync' | 'async';
|
|
26
|
+
/**
|
|
27
|
+
* Return the raw input values rather than the parsed values.
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
raw?: boolean;
|
|
31
|
+
}): Resolver<InferOutput<typeof schema>>;
|
package/valibot/dist/valibot.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@hookform/resolvers"),r=require("react-hook-form"),s=require("valibot");exports.valibotResolver=function(t,o,a){return void 0===a&&(a={}),function(i,u,n){try{var v=!n.shouldUseNativeValidation&&"all"===n.criteriaMode;return Promise.resolve(s.safeParseAsync(t,i,Object.assign({},o,{abortPipeEarly:!v}))).then(function(t){if(t.issues){for(var o={};t.issues.length;){var u=t.issues[0],
|
|
1
|
+
var e=require("@hookform/resolvers"),r=require("react-hook-form"),s=require("valibot");exports.valibotResolver=function(t,o,a){return void 0===a&&(a={}),function(i,u,n){try{var v=!n.shouldUseNativeValidation&&"all"===n.criteriaMode;return Promise.resolve(s.safeParseAsync(t,i,Object.assign({},o,{abortPipeEarly:!v}))).then(function(t){if(t.issues){for(var o={};t.issues.length;){var u=t.issues[0],c=s.getDotPath(u);if(c&&(o[c]||(o[c]={message:u.message,type:u.type}),v)){var l=o[c].types,f=l&&l[u.type];o[c]=r.appendErrors(c,v,o,u.type,f?[].concat(f,u.message):u.message)}t.issues.shift()}return{values:{},errors:e.toNestErrors(o,n)}}return{values:a.raw?Object.assign({},i):t.output,errors:{}}})}catch(e){return Promise.reject(e)}}};
|
|
2
2
|
//# sourceMappingURL=valibot.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valibot.js","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"valibot.js","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { getDotPath, safeParseAsync } from 'valibot';\nimport {\n BaseSchema,\n BaseSchemaAsync,\n Config,\n InferIssue,\n InferOutput,\n} from 'valibot';\n\n/**\n * Creates a resolver for react-hook-form using Valibot schema validation\n * @param {BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>} schema - The Valibot schema to validate against\n * @param {Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>} [schemaOptions] - Optional Valibot validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {('sync' | 'async')} [resolverOptions.mode] - Validation mode\n * @param {boolean} [resolverOptions.raw] - If true, returns raw values rather than validated results\n * @returns {Resolver<InferOutput<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = valibot.object({\n * name: valibot.string().minLength(2),\n * age: valibot.number().min(18)\n * });\n *\n * useForm({\n * resolver: valibotResolver(schema)\n * });\n */\nexport function valibotResolver<TFieldValues extends FieldValues>(\n schema:\n | BaseSchema<TFieldValues, TFieldValues, any>\n | BaseSchemaAsync<TFieldValues, TFieldValues, any>,\n schemaOptions?: Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >,\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'sync' | 'async';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<InferOutput<typeof schema>> {\n return async (values, _, options) => {\n // Check if we should validate all field criteria\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n // Parse values with Valibot schema\n const result = await safeParseAsync(\n schema,\n values,\n Object.assign({}, schemaOptions, {\n abortPipeEarly: !validateAllFieldCriteria,\n }),\n );\n\n // If there are issues, return them as errors\n if (result.issues) {\n // Create errors object\n const errors: Record<string, FieldError> = {};\n\n // Iterate over issues to add them to errors object\n for (; result.issues.length; ) {\n const issue = result.issues[0];\n // Create dot path from issue\n const path = getDotPath(issue);\n\n if (path) {\n // Add first error of path to errors object\n if (!errors[path]) {\n errors[path] = { message: issue.message, type: issue.type };\n }\n\n // If configured, add all errors of path to errors object\n if (validateAllFieldCriteria) {\n const types = errors[path].types;\n const messages = types && types[issue.type];\n errors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n errors,\n issue.type,\n messages\n ? ([] as string[]).concat(\n messages as string | string[],\n issue.message,\n )\n : issue.message,\n ) as FieldError;\n }\n }\n\n result.issues.shift();\n }\n\n // Return resolver result with errors\n return {\n values: {},\n errors: toNestErrors(errors, options),\n } as const;\n }\n\n // Otherwise, return resolver result with values\n return {\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.output as FieldValues),\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","Promise","resolve","safeParseAsync","Object","assign","abortPipeEarly","then","result","issues","errors","length","issue","path","getDotPath","message","type","types","messages","appendErrors","concat","shift","toNestErrors","raw","output","e","reject"],"mappings":"+GAkCM,SACJA,EAGAC,EAGAC,GAYA,YAZAA,IAAAA,IAAAA,EAUI,CAAA,GAEJ,SAAcC,EAAQC,EAAGC,GAAW,IAElC,IAAMC,GACHD,EAAQE,2BAAsD,QAAzBF,EAAQG,aAAuB,OAAAC,QAAAC,QAGlDC,EAAAA,eACnBX,EACAG,EACAS,OAAOC,OAAO,CAAE,EAAEZ,EAAe,CAC/Ba,gBAAiBR,MAEpBS,KAAA,SANKC,GASN,GAAIA,EAAOC,OAAQ,CAKjB,IAHA,IAAMC,EAAqC,CAAA,EAGpCF,EAAOC,OAAOE,QAAU,CAC7B,IAAMC,EAAQJ,EAAOC,OAAO,GAEtBI,EAAOC,EAAAA,WAAWF,GAExB,GAAIC,IAEGH,EAAOG,KACVH,EAAOG,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAMJ,EAAMI,OAInDlB,GAA0B,CAC5B,IAAMmB,EAAQP,EAAOG,GAAMI,MACrBC,EAAWD,GAASA,EAAML,EAAMI,MACtCN,EAAOG,GAAQM,EAAAA,aACbN,EACAf,EACAY,EACAE,EAAMI,KACNE,EACK,GAAgBE,OACfF,EACAN,EAAMG,SAERH,EAAMG,QAEd,CAGFP,EAAOC,OAAOY,OAChB,CAGA,MAAO,CACL1B,OAAQ,CAAA,EACRe,OAAQY,EAAAA,aAAaZ,EAAQb,GAEjC,CAGA,MAAO,CACLF,OAAQD,EAAgB6B,IACpBnB,OAAOC,OAAO,CAAA,EAAIV,GACjBa,EAAOgB,OACZd,OAAQ,CAAA,EACR,EACJ,CAAC,MAAAe,UAAAxB,QAAAyB,OAAAD,EACH,CAAA,CAAA"}
|
package/valibot/dist/valibot.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as e}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";import{safeParseAsync as s,getDotPath as t}from"valibot";
|
|
1
|
+
import{toNestErrors as e}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";import{safeParseAsync as s,getDotPath as t}from"valibot";function o(o,i,a){return void 0===a&&(a={}),function(n,u,m){try{var c=!m.shouldUseNativeValidation&&"all"===m.criteriaMode;return Promise.resolve(s(o,n,Object.assign({},i,{abortPipeEarly:!c}))).then(function(s){if(s.issues){for(var o={};s.issues.length;){var i=s.issues[0],u=t(i);if(u&&(o[u]||(o[u]={message:i.message,type:i.type}),c)){var f=o[u].types,l=f&&f[i.type];o[u]=r(u,c,o,i.type,l?[].concat(l,i.message):i.message)}s.issues.shift()}return{values:{},errors:e(o,m)}}return{values:a.raw?Object.assign({},n):s.output,errors:{}}})}catch(e){return Promise.reject(e)}}}export{o as valibotResolver};
|
|
2
2
|
//# sourceMappingURL=valibot.module.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as s}from"@hookform/resolvers";import{appendErrors as e}from"react-hook-form";import{safeParseAsync as
|
|
1
|
+
import{toNestErrors as s}from"@hookform/resolvers";import{appendErrors as e}from"react-hook-form";import{safeParseAsync as t,getDotPath as r}from"valibot";function o(o,a,i={}){return async(n,u,c)=>{const m=!c.shouldUseNativeValidation&&"all"===c.criteriaMode,p=await t(o,n,Object.assign({},a,{abortPipeEarly:!m}));if(p.issues){const t={};for(;p.issues.length;){const s=p.issues[0],o=r(s);if(o&&(t[o]||(t[o]={message:s.message,type:s.type}),m)){const r=t[o].types,a=r&&r[s.type];t[o]=e(o,m,t,s.type,a?[].concat(a,s.message):s.message)}p.issues.shift()}return{values:{},errors:s(t,c)}}return{values:i.raw?Object.assign({},n):p.output,errors:{}}}}export{o as valibotResolver};
|
|
2
2
|
//# sourceMappingURL=valibot.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valibot.modern.mjs","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"valibot.modern.mjs","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { getDotPath, safeParseAsync } from 'valibot';\nimport {\n BaseSchema,\n BaseSchemaAsync,\n Config,\n InferIssue,\n InferOutput,\n} from 'valibot';\n\n/**\n * Creates a resolver for react-hook-form using Valibot schema validation\n * @param {BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>} schema - The Valibot schema to validate against\n * @param {Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>} [schemaOptions] - Optional Valibot validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {('sync' | 'async')} [resolverOptions.mode] - Validation mode\n * @param {boolean} [resolverOptions.raw] - If true, returns raw values rather than validated results\n * @returns {Resolver<InferOutput<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = valibot.object({\n * name: valibot.string().minLength(2),\n * age: valibot.number().min(18)\n * });\n *\n * useForm({\n * resolver: valibotResolver(schema)\n * });\n */\nexport function valibotResolver<TFieldValues extends FieldValues>(\n schema:\n | BaseSchema<TFieldValues, TFieldValues, any>\n | BaseSchemaAsync<TFieldValues, TFieldValues, any>,\n schemaOptions?: Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >,\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'sync' | 'async';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<InferOutput<typeof schema>> {\n return async (values, _, options) => {\n // Check if we should validate all field criteria\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n // Parse values with Valibot schema\n const result = await safeParseAsync(\n schema,\n values,\n Object.assign({}, schemaOptions, {\n abortPipeEarly: !validateAllFieldCriteria,\n }),\n );\n\n // If there are issues, return them as errors\n if (result.issues) {\n // Create errors object\n const errors: Record<string, FieldError> = {};\n\n // Iterate over issues to add them to errors object\n for (; result.issues.length; ) {\n const issue = result.issues[0];\n // Create dot path from issue\n const path = getDotPath(issue);\n\n if (path) {\n // Add first error of path to errors object\n if (!errors[path]) {\n errors[path] = { message: issue.message, type: issue.type };\n }\n\n // If configured, add all errors of path to errors object\n if (validateAllFieldCriteria) {\n const types = errors[path].types;\n const messages = types && types[issue.type];\n errors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n errors,\n issue.type,\n messages\n ? ([] as string[]).concat(\n messages as string | string[],\n issue.message,\n )\n : issue.message,\n ) as FieldError;\n }\n }\n\n result.issues.shift();\n }\n\n // Return resolver result with errors\n return {\n values: {},\n errors: toNestErrors(errors, options),\n } as const;\n }\n\n // Otherwise, return resolver result with values\n return {\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.output as FieldValues),\n errors: {},\n };\n };\n}\n"],"names":["valibotResolver","schema","schemaOptions","resolverOptions","values","_","options","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","result","safeParseAsync","Object","assign","abortPipeEarly","issues","errors","length","issue","path","getDotPath","message","type","types","messages","appendErrors","concat","shift","toNestErrors","raw","output"],"mappings":"2JAkCgB,SAAAA,EACdC,EAGAC,EAGAC,EAUI,CAAA,GAEJ,OAAcC,MAAAA,EAAQC,EAAGC,KAEvB,MAAMC,GACHD,EAAQE,2BAAsD,QAAzBF,EAAQG,aAG1CC,QAAeC,EACnBV,EACAG,EACAQ,OAAOC,OAAO,CAAE,EAAEX,EAAe,CAC/BY,gBAAiBP,KAKrB,GAAIG,EAAOK,OAAQ,CAEjB,MAAMC,EAAqC,CAAA,EAG3C,KAAON,EAAOK,OAAOE,QAAU,CAC7B,MAAMC,EAAQR,EAAOK,OAAO,GAEtBI,EAAOC,EAAWF,GAExB,GAAIC,IAEGH,EAAOG,KACVH,EAAOG,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAMJ,EAAMI,OAInDf,GAA0B,CAC5B,MAAMgB,EAAQP,EAAOG,GAAMI,MACrBC,EAAWD,GAASA,EAAML,EAAMI,MACtCN,EAAOG,GAAQM,EACbN,EACAZ,EACAS,EACAE,EAAMI,KACNE,EACK,GAAgBE,OACfF,EACAN,EAAMG,SAERH,EAAMG,QAEd,CAGFX,EAAOK,OAAOY,OAChB,CAGA,MAAO,CACLvB,OAAQ,CAAE,EACVY,OAAQY,EAAaZ,EAAQV,GAEjC,CAGA,MAAO,CACLF,OAAQD,EAAgB0B,IACpBjB,OAAOC,OAAO,CAAA,EAAIT,GACjBM,EAAOoB,OACZd,OAAQ,IAGd"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toNestErrors as e}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";import{safeParseAsync as s,getDotPath as t}from"valibot";
|
|
1
|
+
import{toNestErrors as e}from"@hookform/resolvers";import{appendErrors as r}from"react-hook-form";import{safeParseAsync as s,getDotPath as t}from"valibot";function o(o,i,a){return void 0===a&&(a={}),function(n,u,m){try{var c=!m.shouldUseNativeValidation&&"all"===m.criteriaMode;return Promise.resolve(s(o,n,Object.assign({},i,{abortPipeEarly:!c}))).then(function(s){if(s.issues){for(var o={};s.issues.length;){var i=s.issues[0],u=t(i);if(u&&(o[u]||(o[u]={message:i.message,type:i.type}),c)){var f=o[u].types,l=f&&f[i.type];o[u]=r(u,c,o,i.type,l?[].concat(l,i.message):i.message)}s.issues.shift()}return{values:{},errors:e(o,m)}}return{values:a.raw?Object.assign({},n):s.output,errors:{}}})}catch(e){return Promise.reject(e)}}}export{o as valibotResolver};
|
|
2
2
|
//# sourceMappingURL=valibot.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valibot.module.js","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"valibot.module.js","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { getDotPath, safeParseAsync } from 'valibot';\nimport {\n BaseSchema,\n BaseSchemaAsync,\n Config,\n InferIssue,\n InferOutput,\n} from 'valibot';\n\n/**\n * Creates a resolver for react-hook-form using Valibot schema validation\n * @param {BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>} schema - The Valibot schema to validate against\n * @param {Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>} [schemaOptions] - Optional Valibot validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {('sync' | 'async')} [resolverOptions.mode] - Validation mode\n * @param {boolean} [resolverOptions.raw] - If true, returns raw values rather than validated results\n * @returns {Resolver<InferOutput<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = valibot.object({\n * name: valibot.string().minLength(2),\n * age: valibot.number().min(18)\n * });\n *\n * useForm({\n * resolver: valibotResolver(schema)\n * });\n */\nexport function valibotResolver<TFieldValues extends FieldValues>(\n schema:\n | BaseSchema<TFieldValues, TFieldValues, any>\n | BaseSchemaAsync<TFieldValues, TFieldValues, any>,\n schemaOptions?: Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >,\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'sync' | 'async';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<InferOutput<typeof schema>> {\n return async (values, _, options) => {\n // Check if we should validate all field criteria\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n // Parse values with Valibot schema\n const result = await safeParseAsync(\n schema,\n values,\n Object.assign({}, schemaOptions, {\n abortPipeEarly: !validateAllFieldCriteria,\n }),\n );\n\n // If there are issues, return them as errors\n if (result.issues) {\n // Create errors object\n const errors: Record<string, FieldError> = {};\n\n // Iterate over issues to add them to errors object\n for (; result.issues.length; ) {\n const issue = result.issues[0];\n // Create dot path from issue\n const path = getDotPath(issue);\n\n if (path) {\n // Add first error of path to errors object\n if (!errors[path]) {\n errors[path] = { message: issue.message, type: issue.type };\n }\n\n // If configured, add all errors of path to errors object\n if (validateAllFieldCriteria) {\n const types = errors[path].types;\n const messages = types && types[issue.type];\n errors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n errors,\n issue.type,\n messages\n ? ([] as string[]).concat(\n messages as string | string[],\n issue.message,\n )\n : issue.message,\n ) as FieldError;\n }\n }\n\n result.issues.shift();\n }\n\n // Return resolver result with errors\n return {\n values: {},\n errors: toNestErrors(errors, options),\n } as const;\n }\n\n // Otherwise, return resolver result with values\n return {\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.output as FieldValues),\n errors: {},\n };\n };\n}\n"],"names":["valibotResolver","schema","schemaOptions","resolverOptions","values","_","options","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","Promise","resolve","safeParseAsync","Object","assign","abortPipeEarly","then","result","issues","errors","length","issue","path","getDotPath","message","type","types","messages","appendErrors","concat","shift","toNestErrors","raw","output","e","reject"],"mappings":"2JAkCM,SAAUA,EACdC,EAGAC,EAGAC,GAYA,YAZAA,IAAAA,IAAAA,EAUI,CAAA,GAEJ,SAAcC,EAAQC,EAAGC,GAAW,IAElC,IAAMC,GACHD,EAAQE,2BAAsD,QAAzBF,EAAQG,aAAuB,OAAAC,QAAAC,QAGlDC,EACnBX,EACAG,EACAS,OAAOC,OAAO,CAAE,EAAEZ,EAAe,CAC/Ba,gBAAiBR,MAEpBS,KAAA,SANKC,GASN,GAAIA,EAAOC,OAAQ,CAKjB,IAHA,IAAMC,EAAqC,CAAA,EAGpCF,EAAOC,OAAOE,QAAU,CAC7B,IAAMC,EAAQJ,EAAOC,OAAO,GAEtBI,EAAOC,EAAWF,GAExB,GAAIC,IAEGH,EAAOG,KACVH,EAAOG,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAMJ,EAAMI,OAInDlB,GAA0B,CAC5B,IAAMmB,EAAQP,EAAOG,GAAMI,MACrBC,EAAWD,GAASA,EAAML,EAAMI,MACtCN,EAAOG,GAAQM,EACbN,EACAf,EACAY,EACAE,EAAMI,KACNE,EACK,GAAgBE,OACfF,EACAN,EAAMG,SAERH,EAAMG,QAEd,CAGFP,EAAOC,OAAOY,OAChB,CAGA,MAAO,CACL1B,OAAQ,CAAA,EACRe,OAAQY,EAAaZ,EAAQb,GAEjC,CAGA,MAAO,CACLF,OAAQD,EAAgB6B,IACpBnB,OAAOC,OAAO,CAAA,EAAIV,GACjBa,EAAOgB,OACZd,OAAQ,CAAA,EACR,EACJ,CAAC,MAAAe,UAAAxB,QAAAyB,OAAAD,EACH,CAAA,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers"),require("react-hook-form"),require("valibot")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","react-hook-form","valibot"],o):o((e||self).hookformResolversValibot={},e.hookformResolvers,e.ReactHookForm,e.valibot)}(this,function(e,o,r,s){e.valibotResolver=function(e,t,i){return void 0===i&&(i={}),function(a,n,f){try{var u=!f.shouldUseNativeValidation&&"all"===f.criteriaMode;return Promise.resolve(s.safeParseAsync(e,a,Object.assign({},t,{abortPipeEarly:!u}))).then(function(e){if(e.issues){for(var t={};e.issues.length;){var n=e.issues[0],l=s.getDotPath(n);if(l&&(t[l]||(t[l]={message:n.message,type:n.type}),u)){var c=t[l].types,v=c&&c[n.type];t[l]=r.appendErrors(l,u,t,n.type,v?[].concat(v,n.message):n.message)}e.issues.shift()}return{values:{},errors:o.toNestErrors(t,f)}}return{values:i.raw?a:e.output,errors:{}}})}catch(e){return Promise.reject(e)}}}});
|
|
1
|
+
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers"),require("react-hook-form"),require("valibot")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers","react-hook-form","valibot"],o):o((e||self).hookformResolversValibot={},e.hookformResolvers,e.ReactHookForm,e.valibot)}(this,function(e,o,r,s){e.valibotResolver=function(e,t,i){return void 0===i&&(i={}),function(a,n,f){try{var u=!f.shouldUseNativeValidation&&"all"===f.criteriaMode;return Promise.resolve(s.safeParseAsync(e,a,Object.assign({},t,{abortPipeEarly:!u}))).then(function(e){if(e.issues){for(var t={};e.issues.length;){var n=e.issues[0],l=s.getDotPath(n);if(l&&(t[l]||(t[l]={message:n.message,type:n.type}),u)){var c=t[l].types,v=c&&c[n.type];t[l]=r.appendErrors(l,u,t,n.type,v?[].concat(v,n.message):n.message)}e.issues.shift()}return{values:{},errors:o.toNestErrors(t,f)}}return{values:i.raw?Object.assign({},a):e.output,errors:{}}})}catch(e){return Promise.reject(e)}}}});
|
|
2
2
|
//# sourceMappingURL=valibot.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valibot.umd.js","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {
|
|
1
|
+
{"version":3,"file":"valibot.umd.js","sources":["../src/valibot.ts"],"sourcesContent":["import { toNestErrors } from '@hookform/resolvers';\nimport {\n FieldError,\n FieldValues,\n Resolver,\n appendErrors,\n} from 'react-hook-form';\nimport { getDotPath, safeParseAsync } from 'valibot';\nimport {\n BaseSchema,\n BaseSchemaAsync,\n Config,\n InferIssue,\n InferOutput,\n} from 'valibot';\n\n/**\n * Creates a resolver for react-hook-form using Valibot schema validation\n * @param {BaseSchema<TFieldValues, TFieldValues, any> | BaseSchemaAsync<TFieldValues, TFieldValues, any>} schema - The Valibot schema to validate against\n * @param {Partial<Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>>} [schemaOptions] - Optional Valibot validation options\n * @param {Object} resolverOptions - Additional resolver configuration\n * @param {('sync' | 'async')} [resolverOptions.mode] - Validation mode\n * @param {boolean} [resolverOptions.raw] - If true, returns raw values rather than validated results\n * @returns {Resolver<InferOutput<typeof schema>>} A resolver function compatible with react-hook-form\n * @example\n * const schema = valibot.object({\n * name: valibot.string().minLength(2),\n * age: valibot.number().min(18)\n * });\n *\n * useForm({\n * resolver: valibotResolver(schema)\n * });\n */\nexport function valibotResolver<TFieldValues extends FieldValues>(\n schema:\n | BaseSchema<TFieldValues, TFieldValues, any>\n | BaseSchemaAsync<TFieldValues, TFieldValues, any>,\n schemaOptions?: Partial<\n Omit<Config<InferIssue<typeof schema>>, 'abortPipeEarly' | 'skipPipe'>\n >,\n resolverOptions: {\n /**\n * @default async\n */\n mode?: 'sync' | 'async';\n /**\n * Return the raw input values rather than the parsed values.\n * @default false\n */\n raw?: boolean;\n } = {},\n): Resolver<InferOutput<typeof schema>> {\n return async (values, _, options) => {\n // Check if we should validate all field criteria\n const validateAllFieldCriteria =\n !options.shouldUseNativeValidation && options.criteriaMode === 'all';\n\n // Parse values with Valibot schema\n const result = await safeParseAsync(\n schema,\n values,\n Object.assign({}, schemaOptions, {\n abortPipeEarly: !validateAllFieldCriteria,\n }),\n );\n\n // If there are issues, return them as errors\n if (result.issues) {\n // Create errors object\n const errors: Record<string, FieldError> = {};\n\n // Iterate over issues to add them to errors object\n for (; result.issues.length; ) {\n const issue = result.issues[0];\n // Create dot path from issue\n const path = getDotPath(issue);\n\n if (path) {\n // Add first error of path to errors object\n if (!errors[path]) {\n errors[path] = { message: issue.message, type: issue.type };\n }\n\n // If configured, add all errors of path to errors object\n if (validateAllFieldCriteria) {\n const types = errors[path].types;\n const messages = types && types[issue.type];\n errors[path] = appendErrors(\n path,\n validateAllFieldCriteria,\n errors,\n issue.type,\n messages\n ? ([] as string[]).concat(\n messages as string | string[],\n issue.message,\n )\n : issue.message,\n ) as FieldError;\n }\n }\n\n result.issues.shift();\n }\n\n // Return resolver result with errors\n return {\n values: {},\n errors: toNestErrors(errors, options),\n } as const;\n }\n\n // Otherwise, return resolver result with values\n return {\n values: resolverOptions.raw\n ? Object.assign({}, values)\n : (result.output as FieldValues),\n errors: {},\n };\n };\n}\n"],"names":["schema","schemaOptions","resolverOptions","values","_","options","validateAllFieldCriteria","shouldUseNativeValidation","criteriaMode","Promise","resolve","safeParseAsync","Object","assign","abortPipeEarly","then","result","issues","errors","length","issue","path","getDotPath","message","type","types","messages","appendErrors","concat","shift","toNestErrors","raw","output","e","reject"],"mappings":"ubAkCM,SACJA,EAGAC,EAGAC,GAYA,YAZAA,IAAAA,IAAAA,EAUI,CAAA,GAEJ,SAAcC,EAAQC,EAAGC,GAAW,IAElC,IAAMC,GACHD,EAAQE,2BAAsD,QAAzBF,EAAQG,aAAuB,OAAAC,QAAAC,QAGlDC,EAAAA,eACnBX,EACAG,EACAS,OAAOC,OAAO,CAAE,EAAEZ,EAAe,CAC/Ba,gBAAiBR,MAEpBS,KAAA,SANKC,GASN,GAAIA,EAAOC,OAAQ,CAKjB,IAHA,IAAMC,EAAqC,CAAA,EAGpCF,EAAOC,OAAOE,QAAU,CAC7B,IAAMC,EAAQJ,EAAOC,OAAO,GAEtBI,EAAOC,EAAAA,WAAWF,GAExB,GAAIC,IAEGH,EAAOG,KACVH,EAAOG,GAAQ,CAAEE,QAASH,EAAMG,QAASC,KAAMJ,EAAMI,OAInDlB,GAA0B,CAC5B,IAAMmB,EAAQP,EAAOG,GAAMI,MACrBC,EAAWD,GAASA,EAAML,EAAMI,MACtCN,EAAOG,GAAQM,EAAAA,aACbN,EACAf,EACAY,EACAE,EAAMI,KACNE,EACK,GAAgBE,OACfF,EACAN,EAAMG,SAERH,EAAMG,QAEd,CAGFP,EAAOC,OAAOY,OAChB,CAGA,MAAO,CACL1B,OAAQ,CAAA,EACRe,OAAQY,EAAAA,aAAaZ,EAAQb,GAEjC,CAGA,MAAO,CACLF,OAAQD,EAAgB6B,IACpBnB,OAAOC,OAAO,CAAA,EAAIV,GACjBa,EAAOgB,OACZd,OAAQ,CAAA,EACR,EACJ,CAAC,MAAAe,UAAAxB,QAAAyB,OAAAD,EACH,CAAA,CAAA"}
|
package/valibot/package.json
CHANGED
|
@@ -59,3 +59,29 @@ test("form's validation with Valibot and TypeScript's integration", async () =>
|
|
|
59
59
|
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
|
|
60
60
|
expect(handleSubmit).not.toHaveBeenCalled();
|
|
61
61
|
});
|
|
62
|
+
|
|
63
|
+
export function TestComponentManualType({
|
|
64
|
+
onSubmit,
|
|
65
|
+
}: {
|
|
66
|
+
onSubmit: (data: FormData) => void;
|
|
67
|
+
}) {
|
|
68
|
+
const {
|
|
69
|
+
register,
|
|
70
|
+
handleSubmit,
|
|
71
|
+
formState: { errors },
|
|
72
|
+
} = useForm<v.InferInput<typeof schema>, undefined, FormData>({
|
|
73
|
+
resolver: valibotResolver(schema), // Useful to check TypeScript regressions
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
78
|
+
<input {...register('username')} />
|
|
79
|
+
{errors.username && <span role="alert">{errors.username.message}</span>}
|
|
80
|
+
|
|
81
|
+
<input {...register('password')} />
|
|
82
|
+
{errors.password && <span role="alert">{errors.password.message}</span>}
|
|
83
|
+
|
|
84
|
+
<button type="submit">submit</button>
|
|
85
|
+
</form>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
@@ -49,8 +49,12 @@ export const schemaError = v.variant('type', [
|
|
|
49
49
|
v.object({ type: v.literal('b') }),
|
|
50
50
|
]);
|
|
51
51
|
|
|
52
|
-
export const validSchemaErrorData = { type: 'a' }
|
|
53
|
-
|
|
52
|
+
export const validSchemaErrorData = { type: 'a' } as v.InferOutput<
|
|
53
|
+
typeof schemaError
|
|
54
|
+
>;
|
|
55
|
+
export const invalidSchemaErrorData = { type: 'c' } as any as v.InferOutput<
|
|
56
|
+
typeof schemaError
|
|
57
|
+
>;
|
|
54
58
|
|
|
55
59
|
export const validData = {
|
|
56
60
|
username: 'Doe',
|
|
@@ -73,7 +77,7 @@ export const invalidData = {
|
|
|
73
77
|
birthYear: 'birthYear',
|
|
74
78
|
like: { id: 'z' },
|
|
75
79
|
tags: [1, 2, 3],
|
|
76
|
-
}
|
|
80
|
+
} as any as v.InferOutput<typeof schema>;
|
|
77
81
|
|
|
78
82
|
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
79
83
|
username: {
|
|
@@ -130,7 +130,7 @@ describe('valibotResolver', () => {
|
|
|
130
130
|
expect(result).toEqual({
|
|
131
131
|
errors: {
|
|
132
132
|
type: {
|
|
133
|
-
message: 'Invalid type: Expected "a" | "b" but received "c"',
|
|
133
|
+
message: 'Invalid type: Expected ("a" | "b") but received "c"',
|
|
134
134
|
ref: undefined,
|
|
135
135
|
type: 'variant',
|
|
136
136
|
},
|
package/valibot/src/index.ts
CHANGED