示例#1
0
// NewSchemaValidator creates a new schema validator
func NewSchemaValidator(schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry) *SchemaValidator {
	if schema == nil {
		return nil
	}

	if rootSchema == nil {
		rootSchema = schema
	}

	if schema.ID != "" || schema.Ref.String() != "" || schema.Ref.IsRoot() {
		err := spec.ExpandSchema(schema, rootSchema, nil)
		if err != nil {
			panic(err)
		}
	}
	s := SchemaValidator{Path: root, in: "body", Schema: schema, Root: rootSchema, KnownFormats: formats}
	s.validators = []valueValidator{
		s.typeValidator(),
		s.schemaPropsValidator(),
		s.stringValidator(),
		s.formatValidator(),
		s.numberValidator(),
		s.sliceValidator(),
		s.commonValidator(),
		s.objectValidator(),
	}
	return &s
}
示例#2
0
func TestJSONSchemaSuite(t *testing.T) {
	go func() {
		err := http.ListenAndServe("localhost:1234", http.FileServer(http.Dir(jsonSchemaFixturesPath+"/remotes")))
		if err != nil {
			panic(err.Error())
		}
	}()
	files, err := ioutil.ReadDir(jsonSchemaFixturesPath)
	if err != nil {
		t.Fatal(err)
	}

	for _, f := range files {
		if f.IsDir() {
			continue
		}
		fileName := f.Name()
		specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
		if isEnabled(specName) {

			t.Log("Running " + specName)
			b, _ := ioutil.ReadFile(filepath.Join(jsonSchemaFixturesPath, fileName))

			var testDescriptions []schemaTestT
			json.Unmarshal(b, &testDescriptions)

			for _, testDescription := range testDescriptions {

				err := spec.ExpandSchema(testDescription.Schema, nil, nil /*new(noopResCache)*/)
				if assert.NoError(t, err, testDescription.Description+" should expand cleanly") {

					validator := NewSchemaValidator(testDescription.Schema, nil, "data", strfmt.Default)
					for _, test := range testDescription.Tests {

						result := validator.Validate(test.Data)
						assert.NotNil(t, result, test.Description+" should validate")
						if test.Valid {
							assert.Empty(t, result.Errors, test.Description+" should not have errors")
						} else {
							assert.NotEmpty(t, result.Errors, test.Description+" should have errors")
						}
					}
				}
			}
		}
	}
}
示例#3
0
func TestSchemaFixtures(t *testing.T) {
	files, err := ioutil.ReadDir(schemaFixturesPath)
	if err != nil {
		t.Fatal(err)
	}

	for _, f := range files {
		if f.IsDir() {
			continue
		}
		fileName := f.Name()
		specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))

		t.Log("Running " + specName)
		b, _ := ioutil.ReadFile(filepath.Join(schemaFixturesPath, fileName))

		var testDescriptions []schemasTestT
		json.Unmarshal(b, &testDescriptions)

		for _, testDescription := range testDescriptions {

			err := spec.ExpandSchema(testDescription.Schema, nil, nil /*new(noopResCache)*/)
			if assert.NoError(t, err) {

				validator := NewSchemaValidator(testDescription.Schema, nil, "data", strfmt.Default)
				valid := validator.Validate(testDescription.Valid)
				if assert.NotNil(t, valid, specName+" should validate") {
					assert.Empty(t, valid.Errors, specName+".valid should not have errors")
				}
				invalid := validator.Validate(testDescription.Invalid)
				if assert.NotNil(t, invalid, specName+" should validate") {
					assert.NotEmpty(t, invalid.Errors, specName+".invalid should have errors")
				}
			}
		}
	}
}