@standard-schema/spec
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
@@ -1,15 +0,15 @@ | ||
declare namespace v1 { | ||
/** | ||
* The Standard Schema interface. | ||
*/ | ||
type StandardSchemaV1<Input = unknown, Output = Input> = { | ||
/** | ||
* The Standard Schema interface. | ||
* The Standard Schema properties. | ||
*/ | ||
interface StandardSchema<Input = unknown, Output = Input> { | ||
/** | ||
* The Standard Schema properties. | ||
*/ | ||
readonly "~standard": StandardSchemaProps<Input, Output>; | ||
} | ||
readonly "~standard": StandardSchemaV1.Props<Input, Output>; | ||
}; | ||
declare namespace StandardSchemaV1 { | ||
/** | ||
* The Standard Schema properties interface. | ||
*/ | ||
interface StandardSchemaProps<Input = unknown, Output = Input> { | ||
export interface Props<Input = unknown, Output = Input> { | ||
/** | ||
@@ -26,7 +26,7 @@ * The version number of the standard. | ||
*/ | ||
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>; | ||
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>; | ||
/** | ||
* Inferred types associated with the schema. | ||
*/ | ||
readonly types?: StandardTypes<Input, Output> | undefined; | ||
readonly types?: Types<Input, Output> | undefined; | ||
} | ||
@@ -36,7 +36,7 @@ /** | ||
*/ | ||
type StandardResult<Output> = StandardSuccessResult<Output> | StandardFailureResult; | ||
export type Result<Output> = SuccessResult<Output> | FailureResult; | ||
/** | ||
* The result interface if validation succeeds. | ||
*/ | ||
interface StandardSuccessResult<Output> { | ||
export interface SuccessResult<Output> { | ||
/** | ||
@@ -54,7 +54,7 @@ * The typed output value. | ||
*/ | ||
interface StandardFailureResult { | ||
export interface FailureResult { | ||
/** | ||
* The issues of failed validation. | ||
*/ | ||
readonly issues: ReadonlyArray<StandardIssue>; | ||
readonly issues: ReadonlyArray<Issue>; | ||
} | ||
@@ -64,3 +64,3 @@ /** | ||
*/ | ||
interface StandardIssue { | ||
export interface Issue { | ||
/** | ||
@@ -73,3 +73,3 @@ * The error message of the issue. | ||
*/ | ||
readonly path?: ReadonlyArray<PropertyKey | StandardPathSegment> | undefined; | ||
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; | ||
} | ||
@@ -79,3 +79,3 @@ /** | ||
*/ | ||
interface StandardPathSegment { | ||
export interface PathSegment { | ||
/** | ||
@@ -87,5 +87,5 @@ * The key representing a path segment. | ||
/** | ||
* The base types interface of Standard Schema. | ||
* The Standard Schema types interface. | ||
*/ | ||
interface StandardTypes<Input, Output> { | ||
export interface Types<Input = unknown, Output = Input> { | ||
/** | ||
@@ -103,9 +103,10 @@ * The input type of the schema. | ||
*/ | ||
type InferInput<Schema extends StandardSchema> = NonNullable<Schema["~standard"]["types"]>["input"]; | ||
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"]; | ||
/** | ||
* Infers the output type of a Standard Schema. | ||
*/ | ||
type InferOutput<Schema extends StandardSchema> = NonNullable<Schema["~standard"]["types"]>["output"]; | ||
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"]; | ||
export { }; | ||
} | ||
export { v1 }; | ||
export { StandardSchemaV1 }; |
{ | ||
"name": "@standard-schema/spec", | ||
"description": "A standard interface for TypeScript schema validation libraries", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": "Colin McDonnell", |
@@ -7,36 +7,27 @@ # Standard Schema Spec | ||
The `StandardSchema` interface is a set of validation-related properties that must be defined under a key called `~standard`. | ||
The Standard Schema interface is a set of validation-related properties that must be defined under a key called `~standard`. | ||
```ts | ||
/** | ||
* The Standard Schema interface. | ||
*/ | ||
interface StandardSchema<Input = unknown, Output = Input> { | ||
/** | ||
* The Standard Schema properties. | ||
*/ | ||
readonly "~standard": StandardSchemaProps<Input, Output>; | ||
export interface StandardSchemaV1<Input = unknown, Output = Input> { | ||
readonly "~standard": { | ||
/** | ||
* 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 Standard Schema properties interface. | ||
*/ | ||
interface StandardSchemaProps<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) => StandardResult<Output> | Promise<StandardResult<Output>>; | ||
/** | ||
* Inferred types associated with the schema. | ||
*/ | ||
readonly types?: StandardTypes<Input, Output> | undefined; | ||
} | ||
``` | ||
@@ -56,3 +47,3 @@ | ||
Schemas libraries that want to support Standard Schema must implement its interface. This includes adding the `~standard` property. To make this process easier, schema libraries can optionally extend their interface from the `StandardSchema` interface. | ||
Schemas libraries that want to support Standard Schema must implement its interface. This includes adding the `~standard` property. To make this process easier, schema libraries can optionally extend their interface from the `StandardSchemaV1` interface. | ||
@@ -62,6 +53,6 @@ > It doesn't matter whether your schema library returns plain objects, functions, or class instances. The only thing that matters is that the `~standard` property is defined somehow. | ||
```ts | ||
import type { v1 } from "@standard-schema/spec"; | ||
import type { StandardSchemaV1 } from "@standard-schema/spec"; | ||
// Step 1: Define the schema interface | ||
interface StringSchema extends v1.StandardSchema<string> { | ||
interface StringSchema extends StandardSchemaV1<string> { | ||
type: "string"; | ||
@@ -89,7 +80,7 @@ message: string; | ||
Instead of implementing the `StandardSchema` interface natively into your library code, you can also just add it on top and reuse your existing functions and methods within the `validate` function. | ||
Instead of implementing the `StandardSchemaV1` interface natively into your library code, you can also just add it on top and reuse your existing functions and methods within the `validate` function. | ||
### Third Party | ||
Other than for schema library authors, we recommend third party authors to install the `@standard-schema/spec` package when implementing Standard Schema into their libraries. This package provides the `StandardSchema` interface and the `InferInput` and `InferOutput` utility types. | ||
Other than for schema library authors, we recommend third party authors to install the `@standard-schema/spec` package when implementing Standard Schema into their libraries. This package provides the `StandardSchemaV1` interface and the `InferInput` and `InferOutput` utility types. | ||
@@ -106,13 +97,13 @@ ```sh | ||
After that you can accept any schemas that implement the Standard Schema interface as part of your API. We recommend using a generic that extends the `StandardSchema` interface in most cases to be able to infer the type information of the schema. | ||
After that you can accept any schemas that implement the Standard Schema interface as part of your API. We recommend using a generic that extends the `StandardSchemaV1` interface in most cases to be able to infer the type information of the schema. | ||
```ts | ||
import type { v1 } from "@standard-schema/spec"; | ||
import type { StandardSchemaV1 } from "@standard-schema/spec"; | ||
// Step 1: Define the schema generic | ||
function createEndpoint<TSchema extends v1.StandardSchema, TOutput>( | ||
function createEndpoint<TSchema extends StandardSchemaV1, TOutput>( | ||
// Step 2: Use the generic to accept a schema | ||
schema: TSchema, | ||
// Step 3: Infer the output type from the generic | ||
handler: (data: v1.InferOutput<TSchema>) => Promise<TOutput>, | ||
handler: (data: StandardSchemaV1.InferOutput<TSchema>) => Promise<TOutput>, | ||
) { | ||
@@ -141,5 +132,5 @@ return async (data: unknown) => { | ||
```ts | ||
import type { v1 } from "@standard-schema/spec"; | ||
import type { StandardSchemaV1 } from "@standard-schema/spec"; | ||
async function getFormErrors(schema: v1.StandardSchema, data: unknown) { | ||
async function getFormErrors(schema: StandardSchemaV1, data: unknown) { | ||
const result = await schema["~standard"].validate(data); | ||
@@ -173,7 +164,7 @@ const formErrors: string[] = []; | ||
```ts | ||
import type { v1 } from "@standard-schema/spec"; | ||
import type { StandardSchemaV1 } from "@standard-schema/spec"; | ||
class SchemaError extends Error { | ||
public readonly issues: ReadonlyArray<v1.StandardIssue>; | ||
constructor(issues: ReadonlyArray<v1.StandardIssue>) { | ||
public readonly issues: ReadonlyArray<StandardSchemaV1.Issue>; | ||
constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>) { | ||
super(issues[0].message); | ||
@@ -185,6 +176,6 @@ this.name = "SchemaError"; | ||
async function validateInput<TSchema extends v1.StandardSchema>( | ||
async function validateInput<TSchema extends StandardSchemaV1>( | ||
schema: TSchema, | ||
data: unknown, | ||
): Promise<v1.InferOutput<TSchema>> { | ||
): Promise<StandardSchemaV1.InferOutput<TSchema>> { | ||
const result = await schema["~standard"].validate(data); | ||
@@ -209,2 +200,4 @@ if (result.issues) { | ||
- [GQLoom](https://github.com/modevol-com/gqloom): Weave GraphQL schema and resolvers using Standard Schema. | ||
- [Nuxt UI](https://github.com/nuxt/ui): A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS. | ||
- [TanStack Router](https://github.com/tanstack/router): A fully type-safe React router with built-in data fetching, stale-while revalidate caching and first-class search-param APIs. | ||
@@ -215,3 +208,2 @@ - [tRPC](https://github.com/trpc/trpc): 🧙♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. | ||
### The Problem | ||
@@ -263,5 +255,5 @@ | ||
```ts | ||
import type { v1 } from "@standard-schema/spec"; | ||
import type { StandardSchemaV1 } from "@standard-schema/spec"; | ||
function validateInput(schema: v1.StandardSchema, data: unknown) { | ||
function validateInput(schema: StandardSchemaV1, data: unknown) { | ||
const result = schema["~standard"].validate(data); | ||
@@ -268,0 +260,0 @@ if (result instanceof Promise) { |
Sorry, the diff of this file is not supported yet
22022
120
255