示例#1
0
// Spec validates a spec document
// It validates the spec json against the json schema for swagger
// and then validates a number of extra rules that can't be expressed in json schema:
//
// 	- definition can't declare a property that's already defined by one of its ancestors
// 	- definition's ancestor can't be a descendant of the same model
// 	- each api path should be non-verbatim (account for path param names) unique per method
// 	- each security reference should contain only unique scopes
// 	- each security scope in a security definition should be unique
// 	- each path parameter should correspond to a parameter placeholder and vice versa
// 	- each referencable defintion must have references
// 	- each definition property listed in the required array must be defined in the properties of the model
// 	- each parameter should have a unique `name` and `type` combination
// 	- each operation should have only 1 parameter of type body
// 	- each reference must point to a valid object
// 	- every default value that is specified must validate against the schema for that property
// 	- items property is required for all schemas/definitions of type `array`
func Spec(doc *spec.Document, formats strfmt.Registry) error {
	errs, _ /*warns*/ := validate.NewSpecValidator(doc.Schema(), formats).Validate(doc)
	if errs.HasErrors() {
		return errors.CompositeValidationError(errs.Errors...)
	}
	return nil
}
示例#2
0
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())
	}
}
示例#3
0
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())
	}
}
示例#4
0
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())
	}
}
示例#5
0
func TestIssue123(t *testing.T) {
	fp := filepath.Join("..", "fixtures", "bugs", "123", "swagger.yml")

	// as swagger spec
	doc, err := spec.YAMLSpec(fp)
	if assert.NoError(t, err) {
		validator := intvalidate.NewSpecValidator(doc.Schema(), strfmt.Default)
		res, _ := validator.Validate(doc)
		for _, e := range res.Errors {
			fmt.Println(e)
		}
		assert.True(t, res.IsValid())
	}
}
示例#6
0
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")
		}
	}
}