-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Standard Schema spec (#3850)
* Implement Standard Schema * Remove dep * WIP * Fix CI * Update to latest standard-schema * Add standard-schema/spec as devDep
- Loading branch information
1 parent
963386d
commit 69a1798
Showing
9 changed files
with
560 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// @ts-ignore TS6133 | ||
import { expect } from "https://deno.land/x/[email protected]/mod.ts"; | ||
const test = Deno.test; | ||
import { util } from "../helpers/util.ts"; | ||
|
||
import * as z from "../index.ts"; | ||
|
||
import type { StandardSchemaV1 } from "@standard-schema/spec"; | ||
|
||
test("assignability", () => { | ||
const _s1: StandardSchemaV1 = z.string(); | ||
const _s2: StandardSchemaV1<string> = z.string(); | ||
const _s3: StandardSchemaV1<string, string> = z.string(); | ||
const _s4: StandardSchemaV1<unknown, string> = z.string(); | ||
[_s1, _s2, _s3, _s4]; | ||
}); | ||
|
||
test("type inference", () => { | ||
const stringToNumber = z.string().transform((x) => x.length); | ||
type input = StandardSchemaV1.InferInput<typeof stringToNumber>; | ||
util.assertEqual<input, string>(true); | ||
type output = StandardSchemaV1.InferOutput<typeof stringToNumber>; | ||
util.assertEqual<output, number>(true); | ||
}); | ||
|
||
test("valid parse", () => { | ||
const schema = z.string(); | ||
const result = schema["~standard"]["validate"]("hello"); | ||
if (result instanceof Promise) { | ||
throw new Error("Expected sync result"); | ||
} | ||
expect(result.issues).toEqual(undefined); | ||
if (result.issues) { | ||
throw new Error("Expected no issues"); | ||
} else { | ||
expect(result.value).toEqual("hello"); | ||
} | ||
}); | ||
|
||
test("invalid parse", () => { | ||
const schema = z.string(); | ||
const result = schema["~standard"]["validate"](1234); | ||
if (result instanceof Promise) { | ||
throw new Error("Expected sync result"); | ||
} | ||
expect(result.issues).toBeDefined(); | ||
if (!result.issues) { | ||
throw new Error("Expected issues"); | ||
} | ||
expect(result.issues.length).toEqual(1); | ||
expect(result.issues[0].path).toEqual([]); | ||
}); | ||
|
||
test("valid parse async", async () => { | ||
const schema = z.string().refine(async () => true); | ||
const _result = schema["~standard"]["validate"]("hello"); | ||
if (_result instanceof Promise) { | ||
const result = await _result; | ||
expect(result.issues).toEqual(undefined); | ||
if (result.issues) { | ||
throw new Error("Expected no issues"); | ||
} else { | ||
expect(result.value).toEqual("hello"); | ||
} | ||
} else { | ||
throw new Error("Expected async result"); | ||
} | ||
}); | ||
|
||
test("invalid parse async", async () => { | ||
const schema = z.string().refine(async () => false); | ||
const _result = schema["~standard"]["validate"]("hello"); | ||
if (_result instanceof Promise) { | ||
const result = await _result; | ||
expect(result.issues).toBeDefined(); | ||
if (!result.issues) { | ||
throw new Error("Expected issues"); | ||
} | ||
expect(result.issues.length).toEqual(1); | ||
expect(result.issues[0].path).toEqual([]); | ||
} else { | ||
throw new Error("Expected async result"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* The Standard Schema interface. | ||
*/ | ||
export type StandardSchemaV1<Input = unknown, Output = Input> = { | ||
/** | ||
* The Standard Schema properties. | ||
*/ | ||
readonly "~standard": StandardSchemaV1.Props<Input, Output>; | ||
}; | ||
|
||
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"]; | ||
|
||
// biome-ignore lint/complexity/noUselessEmptyExport: needed for granular visibility control of TS namespace | ||
export {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
import { z } from "./src"; | ||
|
||
z; | ||
|
||
const schema = z | ||
.string() | ||
.transform((input) => input || undefined) | ||
.optional() | ||
.default("default"); | ||
|
||
type Input = z.input<typeof schema>; // string | undefined | ||
type Output = z.output<typeof schema>; // string | ||
|
||
const result = schema.safeParse(""); | ||
|
||
console.log(result); // { success: true, data: undefined } |
Oops, something went wrong.