func TestValidatesExamplesAgainstSchema(t *testing.T) { tests := []string{ "response", "response-ref", } for _, tt := range tests { doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "valid-example-"+tt+".json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateExamplesValidAgainstSchema() assert.Empty(t, res.Errors, tt+" should not have errors") } doc, err = spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "invalid-example-"+tt+".json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateExamplesValidAgainstSchema() assert.NotEmpty(t, res.Errors, tt+" should have errors") assert.Len(t, res.Errors, 1, tt+" should have 1 error") } } }
func TestValidateCircularAncestry(t *testing.T) { doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "direct-circular-ancestor.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDuplicatePropertyNames() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } doc, err = spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "indirect-circular-ancestor.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDuplicatePropertyNames() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } doc, err = spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "recursive-circular-ancestor.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDuplicatePropertyNames() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } }
func TestValidateReferencesValid(t *testing.T) { doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "valid-ref.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateReferencesValid() assert.Empty(t, res.Errors) } doc, err = spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "invalid-ref.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateReferencesValid() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } }
func TestValidateBodyFormDataParams(t *testing.T) { doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "invalid-formdata-body-params.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDefaultValueValidAgainstSchema() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } }
func TestIssue63(t *testing.T) { fp := filepath.Join("..", "fixtures", "bugs", "63", "swagger.json") // as swagger spec doc, err := spec.JSONSpec(fp) if assert.NoError(t, err) { validator := intvalidate.NewSpecValidator(doc.Schema(), strfmt.Default) res, _ := validator.Validate(doc) assert.True(t, res.IsValid()) } }
func TestValidateNonEmptyPathParameterNames(t *testing.T) { doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "empty-path-param-name.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateNonEmptyPathParamNames() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } }
func TestIssue62(t *testing.T) { fp := filepath.Join("..", "fixtures", "bugs", "62", "swagger.json") // as swagger spec doc, err := spec.JSONSpec(fp) if assert.NoError(t, err) { validator := intvalidate.NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) res, _ := validator.Validate(doc) assert.NotEmpty(t, res.Errors) assert.True(t, res.HasErrors()) } }
func TestIssue61_ResolvedRef(t *testing.T) { fp := filepath.Join("..", "fixtures", "bugs", "61", "unresolved-ref-for-name.json") // as swagger spec doc, err := spec.JSONSpec(fp) if assert.NoError(t, err) { validator := intvalidate.NewSpecValidator(doc.Schema(), strfmt.Default) res, _ := validator.Validate(doc) assert.Empty(t, res.Errors) assert.True(t, res.IsValid()) } }
func TestValidateDefaultValueAgainstSchema(t *testing.T) { doc, api := petstore.NewAPI(t) validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), api.Formats()) validator.spec = doc res := validator.validateDefaultValueValidAgainstSchema() assert.Empty(t, res.Errors) tests := []string{ "parameter", "parameter-ref", "parameter-items", "header", "header-items", "schema", "schema-ref", "schema-additionalProperties", "schema-patternProperties", "schema-items", "schema-allOf", } for _, tt := range tests { doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "valid-default-value-"+tt+".json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDefaultValueValidAgainstSchema() assert.Empty(t, res.Errors, tt+" should not have errors") } doc, err = spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "invalid-default-value-"+tt+".json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDefaultValueValidAgainstSchema() assert.NotEmpty(t, res.Errors, tt+" should have errors") assert.Len(t, res.Errors, 1, tt+" should have 1 error") } } }
func TestValidateDuplicatePropertyNames(t *testing.T) { // simple allOf doc, err := spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "duplicateprops.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDuplicatePropertyNames() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } // nested allOf doc, err = spec.JSONSpec(filepath.Join("..", "..", "fixtures", "validation", "nestedduplicateprops.json")) if assert.NoError(t, err) { validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default) validator.spec = doc res := validator.validateDuplicatePropertyNames() assert.NotEmpty(t, res.Errors) assert.Len(t, res.Errors, 1) } }
func TestIssue53(t *testing.T) { fp := filepath.Join("..", "fixtures", "bugs", "53", "noswagger.json") jstext, _ := ioutil.ReadFile(fp) // as json schema var sch spec.Schema if assert.NoError(t, json.Unmarshal(jstext, &sch)) { validator := intvalidate.NewSchemaValidator(spec.MustLoadSwagger20Schema(), nil, "", strfmt.Default) res := validator.Validate(&sch) assert.False(t, res.IsValid()) assert.EqualError(t, res.Errors[0], ".swagger in body is required") } // as swagger spec doc, err := spec.JSONSpec(fp) if assert.NoError(t, err) { validator := intvalidate.NewSpecValidator(doc.Schema(), strfmt.Default) res, _ := validator.Validate(doc) if assert.False(t, res.IsValid()) { assert.EqualError(t, res.Errors[0], ".swagger in body is required") } } }