Skip to content
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

feat(validation): implement Standard Schema #964

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
72 changes: 59 additions & 13 deletions docs/2.utils/1.request.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ app.use("/", (event) => {

### `getValidatedQuery(event, validate)`

Get the query param from the request URL validated with validate function.
Get the query param from the request URL validated with validate function or validation schema.

You can use a simple function to validate the query object or a library like `zod` to define a schema.
You can use a simple function to validate the query object or a library like `valibot` or `zod` to define a schema.

**Example:**

Expand All @@ -148,24 +148,38 @@ app.use("/", async (event) => {
**Example:**

```ts
import { z } from "zod";
import * as v from "valibot";
app.use("/", async (event) => {
const query = await getValidatedQuery(
event,
v.object({
key: v.string(),
}),
);
});
```

**Example:**

```ts
import * as z from "zod";
app.use("/", async (event) => {
const query = await getValidatedQuery(
event,
z.object({
key: z.string(),
}).parse,
}),
);
});
```

### `getValidatedRouterParams(event, validate, opts: { decode? })`

Get matched route params and validate with validate function.
Get matched route params and validate with validate function or validation schema.

If `decode` option is `true`, it will decode the matched route params using `decodeURI`.

You can use a simple function to validate the params object or a library like `zod` to define a schema.
You can use a simple function to validate the params object or a library like `valibot` or `zod` to define a schema.

**Example:**

Expand All @@ -180,13 +194,27 @@ app.use("/", async (event) => {
**Example:**

```ts
import { z } from "zod";
import * as v from "valibot";
app.use("/", async (event) => {
const params = await getValidatedRouterParams(
event,
v.object({
key: v.string(),
}),
);
});
```

**Example:**

```ts
import * as z from "zod";
app.use("/", async (event) => {
const params = await getValidatedRouterParams(
event,
z.object({
key: z.string(),
}).parse,
}),
);
});
```
Expand Down Expand Up @@ -237,9 +265,9 @@ app.use("/", async (event) => {

### `readValidatedBody(event, validate)`

Tries to read the request body via `readBody`, then uses the provided validation function and either throws a validation error or returns the result.
Tries to read the request body via `readBody`, then uses the provided validation function or schema and either throws a validation error or returns the result.

You can use a simple function to validate the body or use a library like `zod` to define a schema.
You can use a simple function to validate the body or use a library like `valibot` or `zod` to define the schema.

**Example:**

Expand All @@ -254,10 +282,28 @@ app.use("/", async (event) => {
**Example:**

```ts
import { z } from "zod";
import * as v from "valibot";
app.use("/", async (event) => {
const body = await readValidatedBody(
event,
v.object({
name: v.string(),
}),
);
});
```

**Example:**

```ts
import * as z from "zod";
app.use("/", async (event) => {
const objectSchema = z.object();
const body = await readValidatedBody(event, objectSchema.safeParse);
const body = await readValidatedBody(
event,
z.object({
name: z.string(),
}),
);
});
```

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@
"react-dom": "^19.0.0",
"typescript": "^5.7.2",
"unbuild": "3.0.0-rc.11",
"valibot": "1.0.0-beta.14",
"vitest": "^2.1.8",
"zod": "^3.23.8"
"zod": "^3.24.1"
},
"peerDependencies": {
"crossws": "^0.2.4"
Expand Down
43 changes: 32 additions & 11 deletions pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ export type { RequestFingerprintOptions } from "./utils/fingerprint";
export type { ServeStaticOptions, StaticAssetMeta } from "./utils/static";

// Validate
export type { ValidateFunction, ValidateResult } from "./utils/validate";
export type {
StandardSchemaV1,
ValidateFunction,
ValidateResult,
} from "./utils/validate";
79 changes: 79 additions & 0 deletions src/types/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,82 @@ export type ValidateResult<T> = T | true | false | void;
export type ValidateFunction<T> = (
data: unknown,
) => ValidateResult<T> | Promise<ValidateResult<T>>;

/**
* From the Standard Schema Specification.
*
* src: https://github.com/standard-schema/standard-schema
* License: MIT
*/

/** The Standard Schema interface. */
export interface StandardSchemaV1<Input = unknown, Output = Input> {
/** The Standard Schema properties. */
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
export declare namespace StandardSchemaV1 {
/** The Standard Schema properties interface. */
export interface Props<Input = unknown, Output = Input> {
/** The version number of the standard. */
readonly version: 1;
/** The vendor name of the schema library. */
readonly vendor: string;
/** Validates unknown input values. */
readonly validate: (
value: unknown,
) => Result<Output> | Promise<Result<Output>>;
/** Inferred types associated with the schema. */
readonly types?: Types<Input, Output> | undefined;
}

/** The result interface of the validate function. */
export type Result<Output> = SuccessResult<Output> | FailureResult;

/** The result interface if validation succeeds. */
export interface SuccessResult<Output> {
/** The typed output value. */
readonly value: Output;
/** The non-existent issues. */
readonly issues?: undefined;
}

/** The result interface if validation fails. */
export interface FailureResult {
/** The issues of failed validation. */
readonly issues: ReadonlyArray<Issue>;
}

/** The issue interface of the failure output. */
export interface Issue {
/** The error message of the issue. */
readonly message: string;
/** The path of the issue, if any. */
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
}

/** The path segment interface of the issue. */
export interface PathSegment {
/** The key representing a path segment. */
readonly key: PropertyKey;
}

/** The Standard Schema types interface. */
export interface Types<Input = unknown, Output = Input> {
/** The input type of the schema. */
readonly input: Input;
/** The output type of the schema. */
readonly output: Output;
}

/** Infers the input type of a Standard Schema. */
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<
Schema["~standard"]["types"]
>["input"];

/** Infers the output type of a Standard Schema. */
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<
Schema["~standard"]["types"]
>["output"];
}
Loading