This fork is only an temporary fork until some pull requests have been integrated into https://github.com/omissis/go-jsonschema.
- omissis#401 - fix multipleOf validation fails for small multipleOf with a higher number to validate due to precision
- omissis#406 - Add support for time zone offsets in json format "time" fields
If you need to use this fork instead of the original repo, add a replace statement in your go.mod:
require (
github.com/atombender/go-jsonschema v0.18.0
...
)
...
replace github.com/atombender/go-jsonschema v0.18.0 => github.com/deep-creek/go-jsonschema v0.18.0-patched.1
If you replaced the package inside you go.mod, you can also just use go run to generate the en-/decoding stubs:
go run github.com/atombender/go-jsonschema schema.json
It will simply use the information inside go.mod or even go.work, as long as it is in the current working directory or below from which you called go run.
go-jsonschema is a tool to generate Go data types from JSON Schema definitions.
This tool generates Go data types and structs that corresponds to definitions in the schema, along with unmarshalling code that validates the input JSON according to the schema's validation rules.
-
Download: Get a release here.
-
Install from source: To install with Go 1.23+:
go get github.com/atombender/go-jsonschema/...
go install github.com/atombender/go-jsonschema@latest
- Install with Brew: To install with Homebrew:
brew tap omissis/go-jsonschema
brew install go-jsonschema
This project makes use of go workspaces in order to ease testing of the generated code during development while keeping the codebase as tidy and maintainable as possible. It's an unusual choice, but it allows to not only test the code-generation logic, but also the generated code itself.
At its most basic:
go-jsonschema -p main schema.json
This will write a Go source file to standard output, declared under the package main
.
You can generate code for multiple schemas in one run, optionally writing to different files inside different packages:
$ go-jsonschema \
--schema-package=https://example.com/schema1=github.com/myuser/myproject \
--schema-output=https://example.com/schema1=schema1.go \
--schema-package=https://example.com/schema2=github.com/myuser/myproject/stuff \
--schema-output=https://example.com/schema2=stuff/schema2.go \
schema1.json schema2.json
This will create schema1.go
(declared as package myproject
) and stuff/schema2.go
(declared as package stuff
).
If schema1.json
refers to schema2.json
or viceversa, the two Go files will import the other depended-on package.
Note the flag format:
--schema-package=https://example.com/schema1=github.com/myuser/myproject \
^ ^
| |
schema $id full import URL
In a few cases, special types are used to help with serializing/deserializing data frrom JSON. Namely a custom types is provided for the following semantic types:
SerializableDate
SerializableTime
These types are needed because there is no native type provided by Go which properly handles them.
While not finished, go-jsonschema can be used today. Aside from some minor features, only specific validations remain to be fully implemented.
- Core (RFC draft)
- Data model (§4.2.1)
-
null
-
boolean
-
object
-
array
-
number
- Option to use
json.Number
- Option to use
-
string
-
- Location identifiers (§8.2.3)
- References against top-level names:
#/$defs/someName
- References against nested names:
#/$defs/someName/$defs/someOtherName
- References against top-level names in external files:
myschema.json#/$defs/someName
- References against nested names:
myschema.json#/$defs/someName/$defs/someOtherName
- References against top-level names:
- Comments (§9)
- Data model (§4.2.1)
- Validation (RFC draft)
- Schema annotations (§10)
-
description
-
default
(only for struct fields) -
readOnly
-
writeOnly
-
(N/A)title
-
(N/A)examples
-
- General validation (§6.1)
-
enum
-
type
(single) -
type
(multiple; note: partial support, limited validation) -
const
-
- Numeric validation (§6.2)
-
multipleOf
-
maximum
-
exclusiveMaximum
-
minimum
-
exclusiveMinimum
-
- String validation (§6.3)
-
maxLength
-
minLength
-
pattern
-
- Array validation (§6.4)
-
items
-
maxItems
-
minItems
-
uniqueItems
-
additionalItems
-
contains
-
- Object validation (§6.5)
-
required
-
properties
-
patternProperties
-
dependencies
-
propertyNames
-
maxProperties
-
minProperties
-
- Conditional subschemas (§6.6)
-
if
-
then
-
else
-
- Boolean subschemas (§6.7)
-
allOf
-
anyOf
-
oneOf
-
not
-
- Semantic formats (§7.3)
- Dates and times
- Email addresses
- Hostnames
- IP addresses
- Resource identifiers
- URI-template
- JSON pointers
- Regex
- Schema annotations (§10)
MIT license. See LICENSE
file.