55package jsonschema_test
66
77import (
8+ "strings"
89 "testing"
910
1011 "github.com/google/go-cmp/cmp"
@@ -20,7 +21,7 @@ func forType[T any]() *jsonschema.Schema {
2021 return s
2122}
2223
23- func TestForType (t * testing.T ) {
24+ func TestFor (t * testing.T ) {
2425 type schema = jsonschema.Schema
2526
2627 type S struct {
@@ -127,6 +128,38 @@ func TestForType(t *testing.T) {
127128 }
128129}
129130
131+ func forErr [T any ]() error {
132+ _ , err := jsonschema .For [T ]()
133+ return err
134+ }
135+
136+ func TestForErrors (t * testing.T ) {
137+ type (
138+ s1 struct {
139+ Empty int `jsonschema:""`
140+ }
141+ s2 struct {
142+ Bad int `jsonschema:"$foo=1,bar"`
143+ }
144+ )
145+
146+ for _ , tt := range []struct {
147+ got error
148+ want string
149+ }{
150+ {forErr [map [int ]int ](), "unsupported map key type" },
151+ {forErr [s1 ](), "empty jsonschema tag" },
152+ {forErr [s2 ](), "must not begin with" },
153+ {forErr [func ()](), "unsupported" },
154+ } {
155+ if tt .got == nil {
156+ t .Errorf ("got nil, want error containing %q" , tt .want )
157+ } else if ! strings .Contains (tt .got .Error (), tt .want ) {
158+ t .Errorf ("got %q\n want it to contain %q" , tt .got , tt .want )
159+ }
160+ }
161+ }
162+
130163func falseSchema () * jsonschema.Schema {
131164 return & jsonschema.Schema {Not : & jsonschema.Schema {}}
132165}
0 commit comments