Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mpppk committed Dec 10, 2024
1 parent 91d928b commit 9404e48
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
32 changes: 20 additions & 12 deletions src/valibot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ export type ValibotApiResSchema<
SC extends keyof AResponses & StatusCode,
> = AResponses[SC] extends AnyV ? AResponses[SC] : never;

type ValibotRequestValidatorsGenerator<E extends ValibotApiEndpoints> = <
Path extends string,
M extends string,
>(
input: ValidatorsRawInput<Path, M>,
) => Result<ToValibotValidators<E, Path, M>, ValidatorInputError>;
type ValibotResponseValidatorsGenerator<E extends ValibotApiEndpoints> = <
Path extends string,
M extends string,
SC extends number,
>(
input: ResponseValidatorsRawInput<Path, M, SC>,
) => Result<
ToValibotResponseValidators<ApiResponses<E, Path, M>, SC>,
ValidatorInputError
>;
/**
* Create a new validator for the given endpoints.
*
Expand All @@ -133,7 +149,7 @@ export type ValibotApiResSchema<
export const newValibotValidator = <E extends ValibotApiEndpoints>(
endpoints: E,
) => {
const { req, res } = createValidator(
return createValidator(
endpoints,
(spec: ValibotApiSpec, input, key) =>
toResult(v.safeParse(spec[key]!, input[key])),
Expand All @@ -142,17 +158,9 @@ export const newValibotValidator = <E extends ValibotApiEndpoints>(
// FIXME: schemaがundefinedの場合の処理
return toResult(v.safeParse(schema!, input[key]));
},
);
return {
req: req as <Path extends string, M extends string>(
input: ValidatorsRawInput<Path, M>,
) => Result<ToValibotValidators<E, Path, M>, ValidatorInputError>,
res: res as <Path extends string, M extends string, SC extends number>(
input: ResponseValidatorsRawInput<Path, M, SC>,
) => Result<
ToValibotResponseValidators<ApiResponses<E, Path, M>, SC>,
ValidatorInputError
>,
) as {
req: ValibotRequestValidatorsGenerator<E>;
res: ValibotResponseValidatorsGenerator<E>;
};
};

Expand Down
33 changes: 21 additions & 12 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,30 @@ export type ToApiResponses<AR extends ZodAnyApiResponses> = {
};
};

type ZodRequestValidatorsGenerator<E extends ZodApiEndpoints> = <
Path extends string,
M extends string,
>(
input: ValidatorsRawInput<Path, M>,
) => Result<ToZodValidators<E, Path, M>, ValidatorInputError>;
type ZodResponseValidatorsGenerator<E extends ZodApiEndpoints> = <
Path extends string,
M extends string,
SC extends number,
>(
input: ResponseValidatorsRawInput<Path, M, SC>,
) => Result<
ToZodResponseValidators<ApiResponses<E, Path, M>, SC>,
ValidatorInputError
>;

/**
* Create a new validator for the given endpoints.
*
* @param endpoints API endpoints
*/
export const newZodValidator = <E extends ZodApiEndpoints>(endpoints: E) => {
const { req, res } = createValidator(
return createValidator(
endpoints,
(spec: ZodApiSpec, input, key) =>
toResult(spec[key]!.safeParse(input[key])),
Expand All @@ -136,17 +153,9 @@ export const newZodValidator = <E extends ZodApiEndpoints>(endpoints: E) => {
// FIXME: schemaがundefinedの場合の処理
return toResult(schema!.safeParse(input[key]));
},
);
return {
req: req as <Path extends string, M extends string>(
input: ValidatorsRawInput<Path, M>,
) => Result<ToZodValidators<E, Path, M>, ValidatorInputError>,
res: res as <Path extends string, M extends string, SC extends number>(
input: ResponseValidatorsRawInput<Path, M, SC>,
) => Result<
ToZodResponseValidators<ApiResponses<E, Path, M>, SC>,
ValidatorInputError
>,
) as {
req: ZodRequestValidatorsGenerator<E>;
res: ZodResponseValidatorsGenerator<E>;
};
};

Expand Down

0 comments on commit 9404e48

Please sign in to comment.