Skip to content

Commit

Permalink
feat: support async validation (#31)
Browse files Browse the repository at this point in the history
* feat: support async validation

* Modify remove unnecessary type

* Add wrapAsync test

* Modify change condition for executing safeParseAsync function

* Add async validation test

* Update valibot to 0.32.0

* Remove unused  option from
  • Loading branch information
chimame authored Jun 14, 2024
1 parent 393f64e commit fa21f28
Show file tree
Hide file tree
Showing 20 changed files with 1,101 additions and 263 deletions.
190 changes: 107 additions & 83 deletions coercion.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { AllSchema, ObjectSchema, UnknownSchema } from "./types/schema";
import {
unknown as valibotUnknown,
pipe,
transform,
pipeAsync,
transform as vTransform,
type SchemaWithPipe,
type TransformAction,
type PipeItem,
type BaseIssue,
type GenericSchema,
type GenericSchemaAsync,
type SchemaWithPipeAsync,
} from "valibot";

/**
Expand Down Expand Up @@ -36,6 +38,35 @@ export function coerceString(
}
}

/**
* Reconstruct the provided schema with additional preprocessing steps
* This coerce empty values to undefined and transform strings to the correct type
* @param type The schema to be coerced
* @param transform The transformation function
* @returns The coerced schema
*/
function coerce<T extends GenericSchema | GenericSchemaAsync>(
type: T,
transform?: (text: string) => unknown,
) {
// `expects` is required to generate error messages for `TupleSchema`, so it is passed to `UnkonwSchema` for coercion.
const unknown = { ...valibotUnknown(), expects: type.expects };

if (type.async) {
return pipeAsync(
unknown,
vTransform((output) => coerceString(output, transform)),
type,
);
}

return pipe(
unknown,
vTransform((output) => coerceString(output, transform)),
type,
);
}

/**
* Helpers for coercing file
* Modify the value only if it's a file, otherwise return the value as-is
Expand All @@ -60,21 +91,39 @@ export function coerceFile(file: unknown) {
* @returns The coerced schema with the original pipe
*/
function generateReturnSchema<
T extends AllSchema,
T extends GenericSchema | GenericSchemaAsync,
E extends
| AllSchema
| GenericSchema
| GenericSchemaAsync
| SchemaWithPipe<
[AllSchema, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
[GenericSchema, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>
| SchemaWithPipeAsync<
[
GenericSchema | GenericSchemaAsync,
...PipeItem<unknown, unknown, BaseIssue<unknown>>[],
]
>,
>(
originalSchema:
| T
| SchemaWithPipe<[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]>,
| (T extends GenericSchema
? SchemaWithPipe<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>
: SchemaWithPipeAsync<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>),
coercionSchema: E,
):
| E
| SchemaWithPipe<[E, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]> {
) {
if ("pipe" in originalSchema) {
if (originalSchema.async && coercionSchema.async) {
return pipeAsync(
coercionSchema,
// @ts-expect-error
...originalSchema.pipe.slice(1),
);
}
return pipe(
coercionSchema,
// @ts-expect-error
Expand All @@ -89,28 +138,26 @@ function generateReturnSchema<
* Reconstruct the provided schema with additional preprocessing steps
* This coerce empty values to undefined and transform strings to the correct type
*/
export function enableTypeCoercion<T extends AllSchema>(
export function enableTypeCoercion<
T extends GenericSchema | GenericSchemaAsync,
>(
type:
| T
| SchemaWithPipe<[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]>,
| (T extends GenericSchema
? SchemaWithPipe<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>
: SchemaWithPipeAsync<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>),
):
| SchemaWithPipe<
[
UnknownSchema,
TransformAction<unknown, unknown | unknown[]>,
(
| T
| SchemaWithPipe<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>
),
]
>
| ReturnType<typeof coerce>
| ReturnType<typeof generateReturnSchema>
| T
| SchemaWithPipe<[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]> {
// `expects` is required to generate error messages for `TupleSchema`, so it is passed to `UnkonwSchema` for coercion.
const unknown = { ...valibotUnknown(), expects: type.expects };
| (T extends GenericSchema
? SchemaWithPipe<[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]>
: SchemaWithPipeAsync<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>) {
const originalSchema = "pipe" in type ? type.pipe[0] : type;

if (
Expand All @@ -119,52 +166,28 @@ export function enableTypeCoercion<T extends AllSchema>(
type.type === "enum" ||
type.type === "undefined"
) {
return pipe(
unknown,
transform((output) => coerceString(output)),
type,
);
return coerce(type);
} else if (type.type === "number") {
return pipe(
unknown,
transform((output) => coerceString(output, Number)),
type,
);
return coerce(type, Number);
} else if (type.type === "boolean") {
return pipe(
unknown,
transform((output) =>
coerceString(output, (text) => (text === "on" ? true : text)),
),
type,
);
return coerce(type, (text) => (text === "on" ? true : text));
} else if (type.type === "date") {
return pipe(
unknown,
transform((output) =>
coerceString(output, (timestamp) => {
const date = new Date(timestamp);

// z.date() does not expose a quick way to set invalid_date error
// This gets around it by returning the original string if it's invalid
// See https://github.com/colinhacks/zod/issues/1526
if (Number.isNaN(date.getTime())) {
return timestamp;
}

return date;
}),
),
type,
);
return coerce(type, (timestamp) => {
const date = new Date(timestamp);

// z.date() does not expose a quick way to set invalid_date error
// This gets around it by returning the original string if it's invalid
// See https://github.com/colinhacks/zod/issues/1526
if (Number.isNaN(date.getTime())) {
return timestamp;
}

return date;
});
} else if (type.type === "bigint") {
return pipe(
unknown,
transform((output) => coerceString(output, BigInt)),
type,
);
return coerce(type, BigInt);
} else if (type.type === "array") {
const arraySchema: typeof type = {
const arraySchema = {
...originalSchema,
// @ts-expect-error
item: enableTypeCoercion(originalSchema.item),
Expand All @@ -182,43 +205,48 @@ export function enableTypeCoercion<T extends AllSchema>(
const wrapSchema = enableTypeCoercion(type.wrapped);

if ("pipe" in wrapSchema) {
// `expects` is required to generate error messages for `TupleSchema`, so it is passed to `UnkonwSchema` for coercion.
const unknown = { ...valibotUnknown(), expects: type.expects };
if (type.async) {
return pipeAsync(unknown, wrapSchema.pipe[1], type);
}
return pipe(unknown, wrapSchema.pipe[1], type);
}

const wrappedSchema: typeof type = {
const wrappedSchema = {
...originalSchema,
// @ts-expect-error
wrapped: enableTypeCoercion(originalSchema.wrapped),
};

return generateReturnSchema(type, wrappedSchema);
} else if (type.type === "union" || type.type === "intersect") {
const unionSchema: typeof type = {
const unionSchema = {
...originalSchema,
// @ts-expect-error
options: originalSchema.options.map((option) =>
enableTypeCoercion(option as ObjectSchema),
enableTypeCoercion(option as GenericSchema),
),
};
return generateReturnSchema(type, unionSchema);
} else if (type.type === "variant") {
const variantSchema: typeof type = {
const variantSchema = {
...originalSchema,
// @ts-expect-error
options: originalSchema.options.map((option) =>
enableTypeCoercion(option as ObjectSchema),
enableTypeCoercion(option as GenericSchema),
),
};
return generateReturnSchema(type, variantSchema);
} else if (type.type === "tuple") {
const tupleSchema: typeof type = {
const tupleSchema = {
...originalSchema,
// @ts-expect-error
items: originalSchema.items.map((option) => enableTypeCoercion(option)),
};
return generateReturnSchema(type, tupleSchema);
} else if (type.type === "tuple_with_rest") {
const tupleWithRestSchema: typeof type = {
const tupleWithRestSchema = {
...originalSchema,
// @ts-expect-error
items: originalSchema.items.map((option) => enableTypeCoercion(option)),
Expand All @@ -227,23 +255,19 @@ export function enableTypeCoercion<T extends AllSchema>(
};
return generateReturnSchema(type, tupleWithRestSchema);
} else if (type.type === "object") {
const objectSchema: typeof type = {
const objectSchema = {
...originalSchema,
entries: Object.fromEntries(
// @ts-expect-error
Object.entries(originalSchema.entries).map(([key, def]) => [
key,
enableTypeCoercion(def as AllSchema),
enableTypeCoercion(def as GenericSchema),
]),
),
};

return generateReturnSchema(type, objectSchema);
}

return pipe(
unknown,
transform((output) => coerceString(output)),
type,
);
return coerce(type);
}
17 changes: 5 additions & 12 deletions constraint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Constraint } from "@conform-to/dom";
import type { AllSchema } from "./types/schema";
import type { SchemaWithPipe, PipeItem, BaseIssue } from "valibot";
import type { GenericSchema, GenericSchemaAsync } from "valibot";

const keys: Array<keyof Constraint> = [
"required",
Expand All @@ -13,17 +12,11 @@ const keys: Array<keyof Constraint> = [
"pattern",
];

export function getValibotConstraint<T extends AllSchema>(
schema:
| T
| SchemaWithPipe<[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]>,
): Record<string, Constraint> {
export function getValibotConstraint<
T extends GenericSchema | GenericSchemaAsync,
>(schema: T): Record<string, Constraint> {
function updateConstraint(
schema:
| T
| SchemaWithPipe<
[T, ...PipeItem<unknown, unknown, BaseIssue<unknown>>[]]
>,
schema: T,

data: Record<string, Constraint>,
name = "",
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
],
"peerDependencies": {
"@conform-to/dom": "^1.0.0",
"valibot": ">= 0.31.0"
"valibot": ">= 0.32.0"
},
"devDependencies": {
"@biomejs/biome": "^1.3.3",
Expand All @@ -45,7 +45,7 @@
"semantic-release": "^22.0.8",
"tsup": "^8.0.0",
"typescript": "^5.2.2",
"valibot": "^0.31.0",
"valibot": "^0.32.0",
"vitest": "1.4.0"
}
}
Loading

0 comments on commit fa21f28

Please sign in to comment.