Skip to content

Add support for date/time formats #98

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
merged 8 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ This will create `schema1.go` (declared as `package myproject`) and `stuff/schem
schema $id full import URL
```

### Special types

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.

## Status

While not finished, go-jsonschema can be used today. Aside from some minor features, only specific validations remain to be fully implemented.
Expand Down Expand Up @@ -138,7 +150,7 @@ While not finished, go-jsonschema can be used today. Aside from some minor featu
- [ ] `oneOf`
- [ ] `not`
- [ ] Semantic formats (§7.3)
- [ ] Dates and times
- [x] Dates and times
- [ ] Email addresses
- [ ] Hostnames
- [ ] IP addresses
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0Bq
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20231005195138-3e424a577f31 h1:9k5exFQKQglLo+RoP+4zMjOFE14P6+vyR0baDAi0Rcs=
golang.org/x/exp v0.0.0-20231005195138-3e424a577f31/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
4 changes: 4 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
45 changes: 45 additions & 0 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,51 @@ func PrimitiveTypeFromJSONSchemaType(jsType, format string, pointer bool) (Type,
},
}

case "date-time":
t = NamedType{
Package: &Package{
QualifiedName: "time",
Imports: []Import{
{
QualifiedName: "time",
},
},
},
Decl: &TypeDecl{
Name: "Time",
},
}

case "date":
t = NamedType{
Package: &Package{
QualifiedName: "types",
Imports: []Import{
{
QualifiedName: "github.com/atombender/go-jsonschema/types",
},
},
},
Decl: &TypeDecl{
Name: "SerializableDate",
},
}

case "time":
t = NamedType{
Package: &Package{
QualifiedName: "types",
Imports: []Import{
{
QualifiedName: "github.com/atombender/go-jsonschema/types",
},
},
},
Decl: &TypeDecl{
Name: "SerializableTime",
},
}

default:
t = PrimitiveType{"string"}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/data/core/array/array.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/data/core/date/date.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/data/core/date/date.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://example.com/object",
"type": "object",
"title": "object",
"properties": {
"myObject": {
"type": "object",
"required": ["myDate"],
"properties": {
"myDate": {
"type": "string",
"format": "date"
}
}
}
}
}
35 changes: 35 additions & 0 deletions tests/data/core/dateTime/dateTime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/data/core/dateTime/dateTime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://example.com/object",
"type": "object",
"title": "object",
"properties": {
"myObject": {
"type": "object",
"required": ["myDateTime"],
"properties": {
"myDateTime": {
"type": "string",
"format": "date-time"
}
}
}
}
}
4 changes: 2 additions & 2 deletions tests/data/core/refExternalFile/refExternalFile.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"type": "object",
"properties": {
"myExternalThing": {
"$ref": "./ref.json#/$defs/Thing"
"$ref": "../ref/ref.json#/$defs/Thing"
},
"someOtherExternalThing": {
"$ref": "./ref.json#/$defs/Thing"
"$ref": "../ref/ref.json#/$defs/Thing"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "object",
"properties": {
"myExternalThing": {
"$ref": "./ref.json#/$defs/Thing"
"$ref": "../ref/ref.json#/$defs/Thing"
},
"myThing": {
"$ref": "#/$defs/Thing"
Expand Down
35 changes: 35 additions & 0 deletions tests/data/core/time/time.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/data/core/time/time.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://example.com/object",
"type": "object",
"title": "object",
"properties": {
"myObject": {
"type": "object",
"required": [
"myTime"
],
"properties": {
"myTime": {
"type": "string",
"format": "time"
}
}
}
}
}
4 changes: 2 additions & 2 deletions tests/data/nameFromTitle/refExternalFile/refExternalFile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"type": "object",
"properties": {
"myExternalThing": {
"$ref": "./ref.json#/$defs/Thing"
"$ref": "../ref/ref.json#/$defs/Thing"
},
"someOtherExternalThing": {
"$ref": "./ref.json#/$defs/Thing"
"$ref": "../ref/ref.json#/$defs/Thing"
}
}
}
4 changes: 4 additions & 0 deletions tests/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func testExamples(t *testing.T, cfg generator.Config, dataDir string) {
}

for _, file := range fileInfos {
if file.IsDir() {
testExamples(t, cfg, filepath.Join(dataDir, file.Name()))
}

if strings.HasSuffix(file.Name(), ".json") {
fileName := filepath.Join(dataDir, file.Name())
if strings.HasSuffix(file.Name(), ".FAIL.json") {
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20231005195138-3e424a577f31 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
4 changes: 2 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0Bq
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20231005195138-3e424a577f31 h1:9k5exFQKQglLo+RoP+4zMjOFE14P6+vyR0baDAi0Rcs=
golang.org/x/exp v0.0.0-20231005195138-3e424a577f31/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading