@@ -4,13 +4,13 @@ import (
44 "context"
55 "encoding/base64"
66 "encoding/json"
7- "fmt"
87 "math"
98 "reflect"
109 "strings"
1110 "testing"
1211
1312 "github.com/stretchr/testify/require"
13+ "gopkg.in/yaml.v3"
1414)
1515
1616type schemaExample struct {
@@ -45,23 +45,25 @@ func testSchema(t *testing.T, example schemaExample) func(*testing.T) {
4545 require .NoError (t , err )
4646 require .Equal (t , dataUnserialized , dataSchema )
4747 }
48- for _ , value := range example .AllValid {
49- err := validateSchema (t , schema , value )
50- require .NoError (t , err )
51- }
52- for _ , value := range example .AllInvalid {
53- err := validateSchema (t , schema , value )
54- require .Error (t , err )
48+ for validateFuncIndex , validateFunc := range validateSchemaFuncs {
49+ for index , value := range example .AllValid {
50+ err := validateFunc (t , schema , value )
51+ require .NoErrorf (t , err , "ValidateFunc #%d, AllValid #%d: %#v" , validateFuncIndex , index , value )
52+ }
53+ for index , value := range example .AllInvalid {
54+ err := validateFunc (t , schema , value )
55+ require .Errorf (t , err , "ValidateFunc #%d, AllInvalid #%d: %#v" , validateFuncIndex , index , value )
56+ }
5557 }
5658 // NaN and Inf aren't valid JSON but are handled
57- for _ , value := range []interface {}{math .NaN (), math .Inf (- 1 ), math .Inf (+ 1 )} {
59+ for index , value := range []interface {}{math .NaN (), math .Inf (- 1 ), math .Inf (+ 1 )} {
5860 err := schema .VisitJSON (value )
59- require .Error (t , err )
61+ require .Errorf (t , err , "NaNAndInf #%d: %#v" , index , value )
6062 }
6163 }
6264}
6365
64- func validateSchema (t * testing.T , schema * Schema , value interface {}, opts ... SchemaValidationOption ) error {
66+ func validateSchemaJSON (t * testing.T , schema * Schema , value interface {}, opts ... SchemaValidationOption ) error {
6567 data , err := json .Marshal (value )
6668 require .NoError (t , err )
6769 var val interface {}
@@ -70,6 +72,22 @@ func validateSchema(t *testing.T, schema *Schema, value interface{}, opts ...Sch
7072 return schema .VisitJSON (val , opts ... )
7173}
7274
75+ func validateSchemaYAML (t * testing.T , schema * Schema , value interface {}, opts ... SchemaValidationOption ) error {
76+ data , err := yaml .Marshal (value )
77+ require .NoError (t , err )
78+ var val interface {}
79+ err = yaml .Unmarshal (data , & val )
80+ require .NoError (t , err )
81+ return schema .VisitJSON (val , opts ... )
82+ }
83+
84+ type validateSchemaFunc func (t * testing.T , schema * Schema , value interface {}, opts ... SchemaValidationOption ) error
85+
86+ var validateSchemaFuncs = []validateSchemaFunc {
87+ validateSchemaJSON ,
88+ validateSchemaYAML ,
89+ }
90+
7391var schemaExamples = []schemaExample {
7492 {
7593 Title : "EMPTY SCHEMA" ,
@@ -234,7 +252,56 @@ var schemaExamples = []schemaExample{
234252 map [string ]interface {}{},
235253 },
236254 },
237-
255+ {
256+ Title : "INTEGER OPTIONAL INT64 FORMAT" ,
257+ Schema : NewInt64Schema (),
258+ Serialization : map [string ]interface {}{
259+ "type" : "integer" ,
260+ "format" : "int64" ,
261+ },
262+ AllValid : []interface {}{
263+ 1 ,
264+ 256 ,
265+ 65536 ,
266+ int64 (math .MaxInt32 ) + 10 ,
267+ int64 (math .MinInt32 ) - 10 ,
268+ },
269+ AllInvalid : []interface {}{
270+ nil ,
271+ false ,
272+ 3.5 ,
273+ true ,
274+ "" ,
275+ []interface {}{},
276+ map [string ]interface {}{},
277+ },
278+ },
279+ {
280+ Title : "INTEGER OPTIONAL INT32 FORMAT" ,
281+ Schema : NewInt32Schema (),
282+ Serialization : map [string ]interface {}{
283+ "type" : "integer" ,
284+ "format" : "int32" ,
285+ },
286+ AllValid : []interface {}{
287+ 1 ,
288+ 256 ,
289+ 65536 ,
290+ int64 (math .MaxInt32 ),
291+ int64 (math .MaxInt32 ),
292+ },
293+ AllInvalid : []interface {}{
294+ nil ,
295+ false ,
296+ 3.5 ,
297+ int64 (math .MaxInt32 ) + 10 ,
298+ int64 (math .MinInt32 ) - 10 ,
299+ true ,
300+ "" ,
301+ []interface {}{},
302+ map [string ]interface {}{},
303+ },
304+ },
238305 {
239306 Title : "STRING" ,
240307 Schema : NewStringSchema ().
@@ -350,7 +417,7 @@ var schemaExamples = []schemaExample{
350417 AllInvalid : []interface {}{
351418 nil ,
352419 " " ,
353- "\n " ,
420+ "\n \n " , // a \n is ok for JSON but not for YAML decoder/encoder
354421 "%" ,
355422 },
356423 },
@@ -1074,28 +1141,30 @@ func TestSchemasMultiError(t *testing.T) {
10741141func testSchemaMultiError (t * testing.T , example schemaMultiErrorExample ) func (* testing.T ) {
10751142 return func (t * testing.T ) {
10761143 schema := example .Schema
1077- for i , value := range example .Values {
1078- err := validateSchema (t , schema , value , MultiErrors ())
1079- require .Error (t , err )
1080- require .IsType (t , MultiError {}, err )
1144+ for validateFuncIndex , validateFunc := range validateSchemaFuncs {
1145+ for i , value := range example .Values {
1146+ err := validateFunc (t , schema , value , MultiErrors ())
1147+ require .Errorf (t , err , "ValidateFunc #%d, value #%d: %#" , validateFuncIndex , i , value )
1148+ require .IsType (t , MultiError {}, err )
10811149
1082- merr , _ := err .(MultiError )
1083- expected := example .ExpectedErrors [i ]
1084- require .True (t , len (merr ) > 0 )
1085- require .Len (t , merr , len (expected ))
1086- for _ , e := range merr {
1087- require .IsType (t , & SchemaError {}, e )
1088- var found bool
1089- scherr , _ := e .(* SchemaError )
1090- for _ , expectedErr := range expected {
1091- expectedScherr , _ := expectedErr .(* SchemaError )
1092- if reflect .DeepEqual (expectedScherr .reversePath , scherr .reversePath ) &&
1093- expectedScherr .SchemaField == scherr .SchemaField {
1094- found = true
1095- break
1150+ merr , _ := err .(MultiError )
1151+ expected := example .ExpectedErrors [i ]
1152+ require .True (t , len (merr ) > 0 )
1153+ require .Len (t , merr , len (expected ))
1154+ for _ , e := range merr {
1155+ require .IsType (t , & SchemaError {}, e )
1156+ var found bool
1157+ scherr , _ := e .(* SchemaError )
1158+ for _ , expectedErr := range expected {
1159+ expectedScherr , _ := expectedErr .(* SchemaError )
1160+ if reflect .DeepEqual (expectedScherr .reversePath , scherr .reversePath ) &&
1161+ expectedScherr .SchemaField == scherr .SchemaField {
1162+ found = true
1163+ break
1164+ }
10961165 }
1166+ require .Truef (t , found , "ValidateFunc #%d, value #%d: missing %s error on %s" , validateFunc , i , scherr .SchemaField , strings .Join (scherr .JSONPointer (), "." ))
10971167 }
1098- require .True (t , found , fmt .Sprintf ("missing %s error on %s" , scherr .SchemaField , strings .Join (scherr .JSONPointer (), "." )))
10991168 }
11001169 }
11011170 }
0 commit comments