Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/invopop/jsonschema
go 1.18

require (
github.com/stoewer/go-strcase v1.3.1
github.com/stretchr/testify v1.8.1
github.com/wk8/go-ordered-map/v2 v2.1.8
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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/stoewer/go-strcase v1.3.1 h1:iS0MdW+kVTxgMoE1LAZyMiYJFKlOzLooE4MxjirtkAs=
github.com/stoewer/go-strcase v1.3.1/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
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/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
6 changes: 4 additions & 2 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"strconv"
"strings"
"time"

"github.com/stoewer/go-strcase"
)

// customSchemaImpl is used to detect if the type provides it's own
Expand Down Expand Up @@ -87,7 +89,7 @@ type Reflector struct {

// AssignAnchor when true will use the original struct's name as an anchor inside
// every definition, including the root schema. These can be useful for having a
// reference to the original struct's name in CamelCase instead of the snake-case used
// reference to the original struct's name in CamelCase instead of the kebab-case used
// by default for URI compatibility.
//
// Anchors do not appear to be widely used out in the wild, so at this time the
Expand Down Expand Up @@ -205,7 +207,7 @@ func (r *Reflector) ReflectFromType(t reflect.Type) *Schema {
}
}
if baseSchemaID != EmptyID {
s.ID = baseSchemaID.Add(ToSnakeCase(name))
s.ID = baseSchemaID.Add(strcase.KebabCase(name))
}
}

Expand Down
15 changes: 0 additions & 15 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
package jsonschema

import (
"regexp"
"strings"

orderedmap "github.com/wk8/go-ordered-map/v2"
)

var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

// ToSnakeCase converts the provided string into snake case using dashes.
// This is useful for Schema IDs and definitions to be coherent with
// common JSON Schema examples.
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}-${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}-${2}")
return strings.ToLower(snake)
}

// NewProperties is a helper method to instantiate a new properties ordered
// map.
func NewProperties() *orderedmap.OrderedMap[string, *Schema] {
Expand Down