Skip to content
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

add new built-in format #44

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 94 additions & 0 deletions fixtures/built_in_format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/built-in-format",
"$ref": "#/$defs/BuiltInFormat",
"$defs": {
"BuiltInFormat": {
"properties": {
"Date": {
"type": "string",
"format": "date"
},
"Time": {
"type": "string",
"format": "time"
},
"DataTime": {
"type": "string",
"format": "date-time"
},
"Duration": {
"type": "integer"
},
"Email": {
"type": "string",
"format": "email"
},
"IDNEmail": {
"type": "string",
"format": "idn-email"
},
"Hostname": {
"type": "string",
"format": "hostname"
},
"IDNHostname": {
"type": "string",
"format": "idn-hostname"
},
"IPv4": {
"type": "string",
"format": "ipv4"
},
"IPv6": {
"type": "string",
"format": "ipv6"
},
"UUID": {
"type": "string",
"format": "uuid"
},
"URI": {
"type": "string",
"format": "uri"
},
"URIReference": {
"type": "string",
"format": "uri-reference"
},
"IRI": {
"type": "string",
"format": "iri"
},
"IRIReference": {
"type": "string",
"format": "iri-reference"
},
"RegEx": {
"type": "string",
"format": "regex"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"Date",
"Time",
"DataTime",
"Duration",
"Email",
"IDNEmail",
"Hostname",
"IDNHostname",
"IPv4",
"IPv6",
"UUID",
"URI",
"URIReference",
"IRI",
"IRIReference",
"RegEx"
]
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.16

require (
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709
github.com/stretchr/testify v1.8.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func (t *Schema) stringKeywords(tags []string) {
t.Pattern = val
case "format":
switch val {
case "date-time", "email", "hostname", "ipv4", "ipv6", "uri", "uuid":
case "date", "date-time", "duration", "email", "idn-email", "hostname", "idn-hostname", "iri", "iri-reference", "ipv4", "ipv6", "time", "uri", "uri-reference", "uuid", "regex":
t.Format = val
break
}
Expand Down
30 changes: 30 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,33 @@ func TestArrayFormat(t *testing.T) {
pt := p.Items.Format
require.Equal(t, pt, "uri")
}

func TestBuiltInFormat(t *testing.T) {
// reference: https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats
type BuiltInFormat struct {
Date time.Time `jsonschema:"format=date"`
Time time.Time `jsonschema:"format=time"`
DataTime time.Time `jsonschema:"format=date-time"`
Duration time.Duration `jsonschema:"format=duration"`

Email string `jsonschema:"format=email"`
IDNEmail string `jsonschema:"format=idn-email"`

Hostname string `jsonschema:"format=hostname"`
IDNHostname string `jsonschema:"format=idn-hostname"`

IPv4 string `jsonschema:"format=ipv4"`
IPv6 string `jsonschema:"format=ipv6"`

UUID string `jsonschema:"format=uuid"`
URI string `jsonschema:"format=uri"`
URIReference string `jsonschema:"format=uri-reference"`
IRI string `jsonschema:"format=iri"`
IRIReference string `jsonschema:"format=iri-reference"`

RegEx string `jsonschema:"format=regex"`
}

r := new(Reflector)
compareSchemaOutput(t, "fixtures/built_in_format.json", r, &BuiltInFormat{})
}