Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomassa committed May 11, 2022
2 parents 45ebb21 + 44d0e44 commit 28073aa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func validateCmd(c *cli.Context) error {
}

val := validator.NewValidator()
err = val.InitSchemaFromURL(validator.OAS31_SCHEMA_URL)
err = val.InitSchemaFromFile(nil)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions differentiator/differentiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ func (d *differentiator) Diff(jsonFile file.JsonFile, jsonFile2 file.JsonFile) (

err := d.validator.Validate(jsonFile)
if err != nil {
return nil, fmt.Errorf("%s is not a valid 3.1 OAS file", jsonFile.GetPath())
return nil, fmt.Errorf("%s is not a valid 3.1 OAS file: %s", jsonFile.GetPath(), err.Error())
}

err = d.validator.Validate(jsonFile2)
if err != nil {
return nil, fmt.Errorf("%s is not a valid 3.1 OAS file", jsonFile2.GetPath())
return nil, fmt.Errorf("%s is not a valid 3.1 OAS file: %s", jsonFile2.GetPath(), err.Error())
}

output := NewChangelogOutput(start, jsonFile.GetPath(), jsonFile2.GetPath(), d.opts)
Expand Down
35 changes: 33 additions & 2 deletions validator/oas31.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
package validator

import (
"embed"
"fmt"
"io/ioutil"
"os"

"github.com/santhosh-tekuri/jsonschema/v5"
file "github.com/up9inc/oas-diff/json"
)

const (
OAS31_SCHEMA_URL = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v3.1/schema.json"
OAS31_SCHEMA_FILE = "validator/oas31.json"
OAS31_SCHEMA_FILE = "oas31.json"
)

//go:embed oas31.json
var schemaFileFS embed.FS

func (v *validator) InitSchemaFromFile(schemaFile file.JsonFile) error {
if schemaFile == nil {
data, err := schemaFileFS.ReadFile(OAS31_SCHEMA_FILE)
if err != nil {
return err
}

// we have to create a temp file because the lib only accepts a file or a url
tempFile, err := ioutil.TempFile("", fmt.Sprintf("*.%s", OAS31_SCHEMA_FILE))
if err != nil {
return err
}
defer os.Remove(tempFile.Name())

n, err := tempFile.Write(data)
if err != nil {
return err
}

if n != len(data) {
return fmt.Errorf("InitSchemaFromFile failed to write %d bytes to %s", len(data), tempFile.Name())
}

schemaFile = file.NewJsonFile(tempFile.Name())
}

err := schemaFile.ValidatePath()
if err != nil {
return err
Expand Down Expand Up @@ -43,7 +74,7 @@ func (v *validator) InitSchemaFromURL(url string) error {

func (v *validator) GetSchemaProperty(key string) (*jsonschema.Schema, error) {
if v.schema == nil {
err := v.InitSchemaFromURL(OAS31_SCHEMA_URL)
err := v.InitSchemaFromFile(nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewValidator() Validator {

func (v *validator) Validate(jsonFile file.JsonFile) error {
if v.schema == nil {
err := v.InitSchemaFromURL(OAS31_SCHEMA_URL)
err := v.InitSchemaFromFile(nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 28073aa

Please sign in to comment.