func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string) (err error) { var schemaData []byte fullDir, err := substituteUserHome(cacheDir) if err != nil { return err } cacheFile := path.Join(fullDir, prefix, groupVersion, schemaFileName) if len(cacheDir) != 0 { if schemaData, err = ioutil.ReadFile(cacheFile); err != nil && !os.IsNotExist(err) { return err } } if schemaData == nil { schemaData, err = c.Get(). AbsPath("/swaggerapi", prefix, groupVersion). Do(). Raw() if err != nil { return err } if len(cacheDir) != 0 { if err := writeSchemaFile(schemaData, fullDir, cacheFile, prefix, groupVersion); err != nil { return err } } } schema, err := validation.NewSwaggerSchemaFromBytes(schemaData) if err != nil { return err } return schema.ValidateBytes(data) }
func loadSchemaForTest() (validation.Schema, error) { pathToSwaggerSpec := "../../../../api/swagger-spec/" + testapi.Default.Version() + ".json" data, err := ioutil.ReadFile(pathToSwaggerSpec) if err != nil { return nil, err } return validation.NewSwaggerSchemaFromBytes(data) }
func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string) (err error) { var schemaData []byte cacheFile := path.Join(cacheDir, prefix, groupVersion, schemaFileName) if len(cacheDir) != 0 { if schemaData, err = ioutil.ReadFile(cacheFile); err != nil && !os.IsNotExist(err) { return err } } if schemaData == nil { schemaData, err = c.Get(). AbsPath("/swaggerapi", prefix, groupVersion). Do(). Raw() if err != nil { return err } if len(cacheDir) != 0 { if err = os.MkdirAll(path.Join(cacheDir, prefix, groupVersion), 0755); err != nil { return err } tmpFile, err := ioutil.TempFile(cacheDir, "schema") if err != nil { return err } if _, err := io.Copy(tmpFile, bytes.NewBuffer(schemaData)); err != nil { return err } if err := os.Link(tmpFile.Name(), cacheFile); err != nil && !os.IsExist(err) { return err } } } schema, err := validation.NewSwaggerSchemaFromBytes(schemaData) if err != nil { return err } return schema.ValidateBytes(data) }