@@ -351,6 +351,9 @@ func spec() *T {
351351}
352352
353353func TestValidation (t * testing.T ) {
354+ version := `
355+ openapi: 3.0.2
356+ `
354357 info := `
355358info:
356359 title: "Hello World REST APIs"
@@ -378,9 +381,17 @@ paths:
378381 200:
379382 description: "Get a single greeting object"
380383`
381- spec := `
382- openapi: 3.0.2
383- ` + info + paths + `
384+ externalDocs := `
385+ externalDocs:
386+ url: https://root-ext-docs.com
387+ `
388+ tags := `
389+ tags:
390+ - name: "pet"
391+ externalDocs:
392+ url: https://tags-ext-docs.com
393+ `
394+ spec := version + info + paths + externalDocs + tags + `
384395components:
385396 schemas:
386397 GreetingObject:
@@ -396,23 +407,58 @@ components:
396407 type: string
397408`
398409
399- tests := map [string ]string {
400- spec : "" ,
401- strings .Replace (spec , `openapi: 3.0.2` , `` , 1 ): "value of openapi must be a non-empty string" ,
402- strings .Replace (spec , `openapi: 3.0.2` , `openapi: ''` , 1 ): "value of openapi must be a non-empty string" ,
403- strings .Replace (spec , info , `` , 1 ): "invalid info: must be an object" ,
404- strings .Replace (spec , paths , `` , 1 ): "invalid paths: must be an object" ,
410+ tests := []struct {
411+ name string
412+ spec string
413+ expectedErr string
414+ }{
415+ {
416+ name : "no errors" ,
417+ spec : spec ,
418+ },
419+ {
420+ name : "version is missing" ,
421+ spec : strings .Replace (spec , version , "" , 1 ),
422+ expectedErr : "value of openapi must be a non-empty string" ,
423+ },
424+ {
425+ name : "version is empty string" ,
426+ spec : strings .Replace (spec , version , "openapi: ''" , 1 ),
427+ expectedErr : "value of openapi must be a non-empty string" ,
428+ },
429+ {
430+ name : "info section is missing" ,
431+ spec : strings .Replace (spec , info , `` , 1 ),
432+ expectedErr : "invalid info: must be an object" ,
433+ },
434+ {
435+ name : "paths section is missing" ,
436+ spec : strings .Replace (spec , paths , `` , 1 ),
437+ expectedErr : "invalid paths: must be an object" ,
438+ },
439+ {
440+ name : "externalDocs section is invalid" ,
441+ spec : strings .Replace (spec , externalDocs ,
442+ strings .ReplaceAll (externalDocs , "url: https://root-ext-docs.com" , "url: ''" ), 1 ),
443+ expectedErr : "invalid external docs: url is required" ,
444+ },
445+ {
446+ name : "tags section is invalid" ,
447+ spec : strings .Replace (spec , tags ,
448+ strings .ReplaceAll (tags , "url: https://tags-ext-docs.com" , "url: ''" ), 1 ),
449+ expectedErr : "invalid tags: invalid external docs: url is required" ,
450+ },
405451 }
406-
407- for spec , expectedErr := range tests {
408- t .Run (expectedErr , func (t * testing.T ) {
452+ for i := range tests {
453+ tt := tests [ i ]
454+ t .Run (tt . name , func (t * testing.T ) {
409455 doc := & T {}
410- err := yaml .Unmarshal ([]byte (spec ), & doc )
456+ err := yaml .Unmarshal ([]byte (tt . spec ), & doc )
411457 require .NoError (t , err )
412458
413459 err = doc .Validate (context .Background ())
414- if expectedErr != "" {
415- require .EqualError (t , err , expectedErr )
460+ if tt . expectedErr != "" {
461+ require .EqualError (t , err , tt . expectedErr )
416462 } else {
417463 require .NoError (t , err )
418464 }
0 commit comments