Skip to content

fix(typebox): improve type interface #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions typebox/src/typebox.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import { Static, StaticDecode, Type } from '@sinclair/typebox';
import { Static, StaticDecode, TObject } from '@sinclair/typebox';
import { TypeCheck } from '@sinclair/typebox/compiler';
import { Value, type ValueError } from '@sinclair/typebox/value';
import {
FieldError,
FieldErrors,
FieldValues,
Resolver,
appendErrors,
} from 'react-hook-form';
import { FieldError, Resolver, appendErrors } from 'react-hook-form';

function parseErrorSchema(
_errors: ValueError[],
Expand Down Expand Up @@ -45,22 +39,6 @@ function parseErrorSchema(
return errors;
}

export function typeboxResolver<Input extends FieldValues, Context>(
typecheck: TypeCheck<ReturnType<typeof Type.Object<Input>>>,
): Resolver<
Static<ReturnType<typeof typecheck.Schema>>,
Context,
StaticDecode<ReturnType<typeof typecheck.Schema>>
>;

export function typeboxResolver<Input extends FieldValues, Context>(
schema: ReturnType<typeof Type.Object<Input>>,
): Resolver<Static<typeof schema>, Context, StaticDecode<typeof schema>>;

export function typeboxResolver<Input extends FieldValues, Context, Output>(
schema: ReturnType<typeof Type.Object<Input>>,
): Resolver<Static<typeof schema>, Context, Output>;

/**
* Creates a resolver for react-hook-form using Typebox schema validation
* @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against
Expand All @@ -77,10 +55,10 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
* resolver: typeboxResolver(schema)
* });
*/
export function typeboxResolver<Input extends FieldValues, Context, Output>(
schema: ReturnType<typeof Type.Object<Input>>,
): Resolver<Static<typeof schema>, Context, Output | Static<typeof schema>> {
return async (values: Static<typeof schema>, _, options) => {
export function typeboxResolver<Schema extends TObject, Context>(
schema: Schema | TypeCheck<Schema>,
): Resolver<Static<Schema>, Context, StaticDecode<Schema>> {
return async (values: Static<Schema>, _, options) => {
const errors = Array.from(
schema instanceof TypeCheck
? schema.Errors(values)
Expand All @@ -91,7 +69,7 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(

if (!errors.length) {
return {
errors: {} as FieldErrors,
errors: {},
values,
};
}
Expand Down
Loading