Skip to content

Commit 0f596bd

Browse files
authored
jsonschema: improve doc (#122)
Improve the package documentation. Add a README and LICENSE, anticipating the relocation of this package to its own module elsewhere.
1 parent 78a66a4 commit 0f596bd

File tree

4 files changed

+106
-18
lines changed

4 files changed

+106
-18
lines changed

jsonschema/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Go MCP SDK Authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

jsonschema/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
TODO: this file should live at the root of the jsonschema-go module,
2+
above the jsonschema package.
3+
4+
# JSON Schema for GO
5+
6+
This module implements the [JSON Schema](https://json-schema.org/) specification.
7+
The `jsonschema` package supports creating schemas, validating JSON values
8+
against a schema, and inferring a schema from a Go struct. See the package
9+
documentation for usage.
10+
11+
## Contributing
12+
13+
This module welcomes external contributions.
14+
It has no dependencies outside of the standard library, and can be built with
15+
the standard Go toolchain. Run `go test ./...` at the module root to run all
16+
the tests.
17+
18+
## Issues
19+
20+
This project uses the [GitHub issue
21+
tracker](https://github.com/TODO/jsonschema-go/issues) for bug reports, feature requests, and other issues.
22+
23+
Please [report
24+
bugs](https://github.com/TODO/jsonschema-go/issues/new). If the SDK is
25+
not working as you expected, it is likely due to a bug or inadequate
26+
documentation, and reporting an issue will help us address this shortcoming.
27+
28+
When reporting a bug, make sure to answer these five questions:
29+
30+
1. What did you do?
31+
2. What did you see?
32+
3. What did you expect to see?
33+
4. What version of the Go MCP SDK are you using?
34+
5. What version of Go are you using (`go version`)?
35+
36+
## License
37+
38+
This project is licensed under the MIT license. See the LICENSE file for details.
39+

jsonschema/doc.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@
55
/*
66
Package jsonschema is an implementation of the [JSON Schema specification],
77
a JSON-based format for describing the structure of JSON data.
8-
The package can be used to read schemas for code generation, and to validate data using the
9-
draft 2020-12 specification. Validation with other drafts or custom meta-schemas
10-
is not supported.
8+
The package can be used to read schemas for code generation, and to validate
9+
data using the draft 2020-12 specification. Validation with other drafts
10+
or custom meta-schemas is not supported.
1111
12-
Construct a [Schema] as you would any Go struct (for example, by writing a struct
13-
literal), or unmarshal a JSON schema into a [Schema] in the usual way (with [encoding/json],
14-
for instance). It can then be used for code generation or other purposes without
15-
further processing.
12+
Construct a [Schema] as you would any Go struct (for example, by writing
13+
a struct literal), or unmarshal a JSON schema into a [Schema] in the usual
14+
way (with [encoding/json], for instance). It can then be used for code
15+
generation or other purposes without further processing.
16+
You can also infer a schema from a Go struct.
17+
18+
# Resolution
19+
20+
A Schema can refer to other schemas, both inside and outside itself. These
21+
references must be resolved before a schema can be used for validation.
22+
Call [Schema.Resolve] to obtain a resolved schema (called a [Resolved]).
23+
If the schema has external references, pass a [ResolveOptions] with a [Loader]
24+
to load them. To validate default values in a schema, set
25+
[ResolveOptions.ValidateDefaults] to true.
1626
1727
# Validation
1828
19-
Before using a Schema to validate a JSON value, you must first resolve it by calling
20-
[Schema.Resolve].
21-
The call [Resolved.Validate] on the result to validate a JSON value.
22-
The value must be a Go value that looks like the result of unmarshaling a JSON
23-
value into an [any] or a struct. For example, the JSON value
29+
Call [Resolved.Validate] to validate a JSON value. The value must be a
30+
Go value that looks like the result of unmarshaling a JSON value into an
31+
[any] or a struct. For example, the JSON value
2432
2533
{"name": "Al", "scores": [90, 80, 100]}
2634
@@ -41,8 +49,11 @@ or as a value of this type:
4149
# Inference
4250
4351
The [For] function returns a [Schema] describing the given Go type.
44-
The type cannot contain any function or channel types, and any map types must have a string key.
45-
For example, calling For on the above Player type results in this schema:
52+
Each field in the struct becomes a property of the schema.
53+
The values of "json" tags are respected: the field's property name is taken
54+
from the tag, and fields omitted from the JSON are omitted from the schema as
55+
well.
56+
For example, `jsonschema.For[Player]()` returns this schema:
4657
4758
{
4859
"properties": {
@@ -58,16 +69,32 @@ For example, calling For on the above Player type results in this schema:
5869
}
5970
}
6071
72+
Use the "jsonschema" struct tag to provide a description for the property:
73+
74+
type Player struct {
75+
Name string `json:"name" jsonschema:"player name"`
76+
Scores []int `json:"scores" jsonschema:"scores of player's games"`
77+
}
78+
6179
# Deviations from the specification
6280
63-
Regular expressions are processed with Go's regexp package, which differs from ECMA 262,
64-
most significantly in not supporting back-references.
81+
Regular expressions are processed with Go's regexp package, which differs
82+
from ECMA 262, most significantly in not supporting back-references.
6583
See [this table of differences] for more.
6684
67-
The value of the "format" keyword is recorded in the Schema, but is ignored during validation.
85+
The "format" keyword described in [section 7 of the validation spec] is recorded
86+
in the Schema, but is ignored during validation.
6887
It does not even produce [annotations].
88+
Use the "pattern" keyword instead: it will work more reliably across JSON Schema
89+
implementations. See [learnjsonschema.com] for more recommendations about "format".
90+
91+
The content keywords described in [section 8 of the validation spec]
92+
are recorded in the schema, but ignored during validation.
6993
7094
[JSON Schema specification]: https://json-schema.org
95+
[section 7 of the validation spec]: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7
96+
[section 8 of the validation spec]: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.8
97+
[learnjsonschema.org]: https://www.learnjsonschema.com/2020-12/format-annotation/format/
7198
[this table of differences] https://github.com/dlclark/regexp2?tab=readme-ov-file#compare-regexp-and-regexp2
7299
[annotations]: https://json-schema.org/draft/2020-12/json-schema-core#name-annotations
73100
*/

jsonschema/infer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ import (
3434
// types, as they are incompatible with the JSON schema spec.
3535
// - maps with key other than 'string'
3636
// - function types
37+
// - channel types
3738
// - complex numbers
3839
// - unsafe pointers
3940
//
4041
// It will return an error if there is a cycle in the types.
4142
//
42-
// For recognizes struct field tags named "jsonschema".
43+
// This function recognizes struct field tags named "jsonschema".
4344
// A jsonschema tag on a field is used as the description for the corresponding property.
4445
// For future compatibility, descriptions must not start with "WORD=", where WORD is a
4546
// sequence of non-whitespace characters.

0 commit comments

Comments
 (0)