Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2c4e37f

Browse filesBrowse files
authored
Merge pull request #134 from standard-schema/json-schema
Standard JSON Schema
2 parents 87bc20f + 395ee95 commit 2c4e37f
Copy full SHA for 2c4e37f

File tree

Expand file treeCollapse file tree

20 files changed

+1312
-326
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

20 files changed

+1312
-326
lines changed
Open diff view settings
Collapse file

‎biome.json‎

Copy file name to clipboardExpand all lines: biome.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"root": true,
32
"$schema": "https://biomejs.dev/schemas/2.1.3/schema.json",
3+
"root": true,
44
"assist": { "actions": { "source": { "organizeImports": "on" } } },
55
"formatter": {
66
"indentStyle": "space",
@@ -31,4 +31,4 @@
3131
}
3232
}
3333
}
34-
}
34+
}
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "standard-schema",
3-
"description": "A standard interface for TypeScript schema validation libraries",
3+
"description": "A family of specs for interoperable TypeScript",
44
"version": "0.0.0",
55
"license": "MIT",
66
"author": "Colin McDonnell",
Collapse file
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* This example shows how to implement the Standard JSON Schema interface.
3+
* It demonstrates creating schemas that support both validation and JSON Schema generation.
4+
*/
5+
6+
import type {
7+
StandardJSONSchemaV1,
8+
StandardSchemaV1,
9+
} from "@standard-schema/spec";
10+
11+
interface CombinedProps<Input = unknown, Output = Input>
12+
extends StandardSchemaV1.Props<Input, Output>,
13+
StandardJSONSchemaV1.Props<Input, Output> {}
14+
15+
/**
16+
* An interface that combines StandardJSONSchema and StandardSchema.
17+
* */
18+
interface StandardSchemaWithJSONSchema<Input = unknown, Output = Input> {
19+
"~standard": CombinedProps<Input, Output>;
20+
}
21+
22+
interface MySchema extends StandardSchemaWithJSONSchema<string, string> {
23+
type: "string";
24+
}
25+
26+
export function stringWithJSON(): MySchema {
27+
return {
28+
type: "string",
29+
"~standard": {
30+
version: 1,
31+
vendor: "example-lib",
32+
validate(value) {
33+
return typeof value === "string"
34+
? { value }
35+
: { issues: [{ message: "Invalid string", path: [] }] };
36+
},
37+
jsonSchema: {
38+
input(params) {
39+
const schema: Record<string, unknown> = {
40+
type: "string",
41+
};
42+
43+
// Add schema version based on target
44+
if (params.target === "draft-2020-12") {
45+
schema.$schema = "https://json-schema.org/draft/2020-12/schema";
46+
} else if (params.target === "draft-07") {
47+
schema.$schema = "http://json-schema.org/draft-07/schema#";
48+
} else {
49+
throw new Error(`Unsupported target: ${params.target}`);
50+
}
51+
52+
return schema;
53+
},
54+
output(params) {
55+
// input and output are the same in this example
56+
return this.input(params);
57+
},
58+
},
59+
},
60+
};
61+
}
62+
63+
// usage example
64+
const stringSchema = stringWithJSON();
65+
66+
stringSchema["~standard"].jsonSchema.input({
67+
target: "draft-2020-12",
68+
});
69+
// => { $schema: "https://json-schema.org/draft/2020-12/schema", type: "string" }
70+
71+
stringSchema["~standard"].jsonSchema.input({
72+
target: "draft-07",
73+
});
74+
// => { $schema: "http://json-schema.org/draft-07/schema#", type: "string" }
Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { StandardJSONSchemaV1 } from "@standard-schema/spec";
2+
3+
// Function that accepts any compliant `StandardJSONSchemaV1`
4+
// and converts it to a JSON Schema.
5+
export function acceptSchema(schema: StandardJSONSchemaV1): unknown {
6+
// do stuff, e.g.
7+
return schema["~standard"].jsonSchema.input({
8+
target: "draft-2020-12",
9+
});
10+
}
11+
12+
export function parseData<T extends StandardJSONSchemaV1>(
13+
schema: T,
14+
data: StandardJSONSchemaV1.InferInput<T>, // extract input type
15+
) {
16+
// @ts-expect-error - replace doStuff with your own logic
17+
const result = doStuff(schema, data);
18+
return result as StandardJSONSchemaV1.InferOutput<T>; // extract output type
19+
}
Collapse file

‎packages/examples/package.json‎

Copy file name to clipboardExpand all lines: packages/examples/package.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"lint": "pnpm biome lint .",
77
"format": "pnpm biome format --write .",
8-
"check": "pnpm biome check ."
8+
"check": "pnpm biome check .",
9+
"build": "tsup"
910
},
1011
"devDependencies": {
1112
"@standard-schema/spec": "workspace:*",
Collapse file

‎packages/examples/tsconfig.json‎

Copy file name to clipboardExpand all lines: packages/examples/tsconfig.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"allowImportingTsExtensions": true,
4+
"customConditions": ["standard-schema-spec"],
45
"declaration": true,
56
"exactOptionalPropertyTypes": true,
67
"isolatedDeclarations": true,
Collapse file

‎packages/spec/README.md‎

Copy file name to clipboardExpand all lines: packages/spec/README.md
+122-260Lines changed: 122 additions & 260 deletions
  • Display the source diff
  • Display the rich diff
Large diffs are not rendered by default.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.