-
Notifications
You must be signed in to change notification settings - Fork 116
Standard JSON Schema #134
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
Merged
+1,312
−326
Merged
Standard JSON Schema #134
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
983b9be
Add JSON Schema source spec
colinhacks 77f843e
Add draft-06
colinhacks c4773ca
Extract targets. Move WithJSONSchemaSource into StandardSchemaV1
colinhacks 63ed7a1
Extract params
colinhacks e371de3
Extract StandardJSONSchemaSourceV1 namespace
colinhacks 8e12345
Standardize on ~standard
colinhacks 9c1968b
Remove unused generics
colinhacks 790edc6
WIP
colinhacks a4dc8a0
WIP
colinhacks 9dfe2a5
WIP
colinhacks db3100a
WIP
colinhacks bf1d604
WIP
colinhacks 3d7c646
WIP
colinhacks bcc2bfa
Update examples
colinhacks 3195c84
Remove tool
colinhacks 2a24c27
Updates
colinhacks d6cef33
Add converter proposal
colinhacks 5395210
Drop converter
colinhacks d076546
Clarify openapi 3.0 specifier
colinhacks d7a60b4
Tweak
colinhacks beb8bbb
Examples
colinhacks 22acddf
Flesh out faq
colinhacks c8dea44
FAQ
colinhacks bf67141
WIP
colinhacks f7a2a1a
Tweak
colinhacks c082c55
Tweak
colinhacks 3415c88
Condense
colinhacks ef312ea
Tweak
colinhacks c395014
Drop rfc
colinhacks 13205ca
Clean up unnecessary changes
colinhacks 0c47ca7
Update packages/spec/src/index.ts
colinhacks 22b9de3
Apply suggestions from code review
colinhacks b51c326
Add empty export block
colinhacks a32d8ae
Format
colinhacks a3fad15
Formatting
colinhacks 4beebb4
Update lock
colinhacks c203aa2
Update JSON integrate example
fabian-hiller 616deb6
Update JSON implement example
fabian-hiller 8dc9589
Address reviews
colinhacks 47d12e8
Clean up examples
colinhacks 68e12d2
Set up custom conditions, clean up examples
colinhacks 8d256bf
Update website
colinhacks cf98e66
Update website
colinhacks 25996be
README updates
ssalbdivad d45f967
Update schema.md
colinhacks 3097314
Updates
colinhacks deb6c18
Rebase
colinhacks fddcc15
update umbrella org overview
ssalbdivad 727c24a
update arktype implementation version and pr
ssalbdivad ea2436d
Update JSON Schema compatibility table with Valibot details
fabian-hiller f16ef72
Update website
colinhacks 3a1b9ea
Add examples table
colinhacks b081dbb
Update site
colinhacks 1dbb6d2
Improve docs
colinhacks 1a5ccf3
Improve json-integrate
colinhacks 395ee95
Add json-integrate
colinhacks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| /** | ||
| * This example shows how to implement the Standard JSON Schema interface. | ||
| * It demonstrates creating schemas that support both validation and JSON Schema generation. | ||
| */ | ||
|
|
||
| import type { | ||
| StandardJSONSchemaV1, | ||
| StandardSchemaV1, | ||
| } from "@standard-schema/spec"; | ||
|
|
||
| interface CombinedProps<Input = unknown, Output = Input> | ||
| extends StandardSchemaV1.Props<Input, Output>, | ||
| StandardJSONSchemaV1.Props<Input, Output> {} | ||
|
|
||
| /** | ||
| * An interface that combines StandardJSONSchema and StandardSchema. | ||
| * */ | ||
| interface StandardSchemaWithJSONSchema<Input = unknown, Output = Input> { | ||
| "~standard": CombinedProps<Input, Output>; | ||
| } | ||
|
|
||
| interface MySchema extends StandardSchemaWithJSONSchema<string, string> { | ||
| type: "string"; | ||
| } | ||
|
|
||
| export function stringWithJSON(): MySchema { | ||
| return { | ||
| type: "string", | ||
| "~standard": { | ||
| version: 1, | ||
| vendor: "example-lib", | ||
| validate(value) { | ||
| return typeof value === "string" | ||
| ? { value } | ||
| : { issues: [{ message: "Invalid string", path: [] }] }; | ||
| }, | ||
| jsonSchema: { | ||
| input(params) { | ||
| const schema: Record<string, unknown> = { | ||
| type: "string", | ||
| }; | ||
|
|
||
| // Add schema version based on target | ||
| if (params.target === "draft-2020-12") { | ||
| schema.$schema = "https://json-schema.org/draft/2020-12/schema"; | ||
| } else if (params.target === "draft-07") { | ||
| schema.$schema = "http://json-schema.org/draft-07/schema#"; | ||
| } else { | ||
| throw new Error(`Unsupported target: ${params.target}`); | ||
| } | ||
|
|
||
| return schema; | ||
| }, | ||
| output(params) { | ||
| // input and output are the same in this example | ||
| return this.input(params); | ||
|
colinhacks marked this conversation as resolved.
|
||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| // usage example | ||
| const stringSchema = stringWithJSON(); | ||
|
|
||
| stringSchema["~standard"].jsonSchema.input({ | ||
| target: "draft-2020-12", | ||
| }); | ||
| // => { $schema: "https://json-schema.org/draft/2020-12/schema", type: "string" } | ||
|
|
||
| stringSchema["~standard"].jsonSchema.input({ | ||
| target: "draft-07", | ||
| }); | ||
| // => { $schema: "http://json-schema.org/draft-07/schema#", type: "string" } | ||
This file contains hidden or 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,19 @@ | ||
| import type { StandardJSONSchemaV1 } from "@standard-schema/spec"; | ||
|
|
||
| // Function that accepts any compliant `StandardJSONSchemaV1` | ||
| // and converts it to a JSON Schema. | ||
| export function acceptSchema(schema: StandardJSONSchemaV1): unknown { | ||
| // do stuff, e.g. | ||
| return schema["~standard"].jsonSchema.input({ | ||
| target: "draft-2020-12", | ||
| }); | ||
| } | ||
|
|
||
| export function parseData<T extends StandardJSONSchemaV1>( | ||
| schema: T, | ||
| data: StandardJSONSchemaV1.InferInput<T>, // extract input type | ||
| ) { | ||
| // @ts-expect-error - replace doStuff with your own logic | ||
| const result = doStuff(schema, data); | ||
| return result as StandardJSONSchemaV1.InferOutput<T>; // extract output type | ||
| } |
This file contains hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.