Skip to content

Commit

Permalink
Merge branch 'invopop:main' into regex_with_equals
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotdawg authored Oct 2, 2023
2 parents 8a098cb + ab48bdb commit 080d97d
Show file tree
Hide file tree
Showing 26 changed files with 369 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: "1.17"
go-version: "1.18"
cache: false

- name: Check out code
Expand Down
26 changes: 13 additions & 13 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.16.2'
id: go
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: "1.18"
id: go

- name: Check out code
uses: actions/checkout@v2
- name: Check out code
uses: actions/checkout@v2

- name: Install Dependencies
env:
GOPROXY: https://proxy.golang.org,direct
run: go mod download
- name: Install Dependencies
env:
GOPROXY: https://proxy.golang.org,direct
run: go mod download

- name: Test
run: go test -tags unit -race ./...
- name: Test
run: go test -tags unit -race ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
.idea/
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ linters-settings:
gocritic:
disabled-checks:
- ifElseChain
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
- pattern: 'a[b:len(a)]'
replacement: 'a[b:]'

issues:
max-per-linter: 0
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ This repository is a fork of the original [jsonschema](https://github.com/alecth

## Versions

This project is still under v0 scheme, as per Go convention, breaking changes are likely. Please pin go modules to branches, and reach out if you think something can be improved.
This project is still under v0 scheme, as per Go convention, breaking changes are likely. Please pin go modules to version tags or branches, and reach out if you think something can be improved.

Go version >= 1.18 is required as generics are now being used.

## Example

Expand Down
3 changes: 3 additions & 0 deletions comment_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func ExtractGoComments(base, path string, commentMap map[string]string) error {
}
case *ast.Field:
txt := x.Doc.Text()
if txt == "" {
txt = x.Comment.Text()
}
if typ != "" && txt != "" {
for _, n := range x.Names {
if ast.IsExported(n.String()) {
Expand Down
4 changes: 3 additions & 1 deletion examples/nested/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type (
// Plant represents the plants the user might have and serves as a test
// of structs inside a `type` set.
Plant struct {
Variant string `json:"variant" jsonschema:"title=Variant"` // This comment will be ignored
Variant string `json:"variant" jsonschema:"title=Variant"` // This comment will be used
// Multicellular is true if the plant is multicellular
Multicellular bool `json:"multicellular,omitempty" jsonschema:"title=Multicellular"` // This comment will be ignored
}
)
6 changes: 3 additions & 3 deletions examples/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type User struct {
// Unique sequential identifier.
ID int `json:"id" jsonschema:"required"`
// This comment will be ignored
Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"`
Tags map[string]interface{} `json:"tags,omitempty"`
Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"`
Tags map[string]any `json:"tags,omitempty"`

// An array of pets the user cares for.
Pets nested.Pets `json:"pets"`
Expand Down
16 changes: 8 additions & 8 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

type SampleUser struct {
ID int `json:"id"`
Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"`
Tags map[string]interface{} `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"`
BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
ID int `json:"id"`
Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"`
Tags map[string]any `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"`
BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
Metadata any `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
}

func ExampleReflect() {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/allow_additional_props.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
34 changes: 34 additions & 0 deletions fixtures/array_handling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/array-handler",
"$ref": "#/$defs/ArrayHandler",
"$defs": {
"ArrayHandler": {
"properties": {
"min_len": {
"default": [
"qwerty"
],
"items": {
"type": "string",
"minLength": 2
},
"type": "array"
},
"min_val": {
"items": {
"type": "number",
"minimum": 2.5
},
"type": "array"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"min_len",
"min_val"
]
}
}
}
4 changes: 2 additions & 2 deletions fixtures/custom_base_schema_id.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/defaults_expanded_toplevel.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
8 changes: 7 additions & 1 deletion fixtures/go_comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
"properties": {
"variant": {
"type": "string",
"title": "Variant"
"title": "Variant",
"description": "This comment will be used"
},
"multicellular": {
"type": "boolean",
"title": "Multicellular",
"description": "Multicellular is true if the plant is multicellular"
}
},
"additionalProperties": false,
Expand Down
4 changes: 2 additions & 2 deletions fixtures/ignore_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/no_reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/no_reference_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/required_from_jsontags.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/test_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/test_user_assign_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
"age": {
"type": "integer",
"maximum": 120,
"exclusiveMaximum": true,
"exclusiveMaximum": 121,
"minimum": 18,
"exclusiveMinimum": true
"exclusiveMinimum": 17
},
"email": {
"type": "string",
Expand Down
47 changes: 47 additions & 0 deletions fixtures/unsigned_int_handling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/unsigned-int-handler",
"$ref": "#/$defs/UnsignedIntHandler",
"$defs": {
"UnsignedIntHandler": {
"properties": {
"min_len": {
"items": {
"type": "string",
"minLength": 0
},
"type": "array"
},
"max_len": {
"items": {
"type": "string",
"maxLength": 0
},
"type": "array"
},
"min_items": {
"items": {
"type": "string"
},
"type": "array",
"minItems": 0
},
"max_items": {
"items": {
"type": "string"
},
"type": "array",
"maxItems": 0
}
},
"additionalProperties": false,
"type": "object",
"required": [
"min_len",
"max_len",
"min_items",
"max_items"
]
}
}
}
15 changes: 12 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
module github.com/invopop/jsonschema

go 1.16
go 1.18

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.1
github.com/wk8/go-ordered-map/v2 v2.1.8
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
27 changes: 22 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.0/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/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/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
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/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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=
Loading

0 comments on commit 080d97d

Please sign in to comment.