Skip to content

Commit

Permalink
moar schema validation options.
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Apr 18, 2023
1 parent 8304b65 commit 009333d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
22 changes: 17 additions & 5 deletions schema_validation/validate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,35 @@ import (
"strings"
)

func ValidateSchema(schema *base.Schema, payload []byte) (bool, []*errors.ValidationError) {
func ValidateSchemaString(schema *base.Schema, payload string) (bool, []*errors.ValidationError) {
return validateSchema(schema, []byte(payload), nil)
}

func ValidateSchemaObject(schema *base.Schema, payload interface{}) (bool, []*errors.ValidationError) {
return validateSchema(schema, nil, payload)
}

func ValidateSchemaBytes(schema *base.Schema, payload []byte) (bool, []*errors.ValidationError) {
return validateSchema(schema, payload, nil)
}

func validateSchema(schema *base.Schema, payload []byte, decodedObject interface{}) (bool, []*errors.ValidationError) {

var validationErrors []*errors.ValidationError

// render the schema, to be used for validation
renderedSchema, _ := schema.RenderInline()
jsonSchema, _ := utils.ConvertYAMLtoJSON(renderedSchema)

var decodedObj interface{}
_ = json.Unmarshal(payload, &decodedObj)

if decodedObject == nil {
_ = json.Unmarshal(payload, &decodedObject)
}
compiler := jsonschema.NewCompiler()
_ = compiler.AddResource("schema.json", strings.NewReader(string(jsonSchema)))
jsch, _ := compiler.Compile("schema.json")

// 4. validate the object against the schema
scErrs := jsch.Validate(decodedObj)
scErrs := jsch.Validate(decodedObject)
if scErrs != nil {
jk := scErrs.(*jsonschema.ValidationError)

Expand Down
63 changes: 57 additions & 6 deletions schema_validation/validate_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,45 @@ paths:

}

func TestValidateSchema_SimpleValid_String(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
name:
type: string
patties:
type: integer
vegetarian:
type: boolean`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()

body := map[string]interface{}{
"name": "Big Mac",
"patties": 2,
"vegetarian": true,
}

bodyBytes, _ := json.Marshal(body)
sch := m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors := ValidateSchemaString(sch.Schema(), string(bodyBytes))

assert.True(t, valid)
assert.Len(t, errors, 0)

}

func TestValidateSchema_SimpleValid(t *testing.T) {
spec := `openapi: 3.1.0
paths:
Expand Down Expand Up @@ -77,7 +116,7 @@ paths:
sch := m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors := ValidateSchema(sch.Schema(), bodyBytes)
valid, errors := ValidateSchemaBytes(sch.Schema(), bodyBytes)

assert.True(t, valid)
assert.Len(t, errors, 0)
Expand Down Expand Up @@ -116,7 +155,7 @@ paths:
sch := m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors := ValidateSchema(sch.Schema(), bodyBytes)
valid, errors := ValidateSchemaBytes(sch.Schema(), bodyBytes)

assert.False(t, valid)
assert.Len(t, errors, 1)
Expand Down Expand Up @@ -207,7 +246,7 @@ paths:
sch := m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors := ValidateSchema(sch.Schema(), bodyBytes)
valid, errors := ValidateSchemaBytes(sch.Schema(), bodyBytes)

assert.True(t, valid)
assert.Len(t, errors, 0)
Expand All @@ -217,7 +256,7 @@ paths:
sch = m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors = ValidateSchema(sch.Schema(), bodyBytes)
valid, errors = ValidateSchemaBytes(sch.Schema(), bodyBytes)

assert.True(t, valid)
assert.Len(t, errors, 0)
Expand Down Expand Up @@ -306,7 +345,13 @@ paths:
sch := m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors := ValidateSchema(sch.Schema(), bodyBytes)
valid, errors := ValidateSchemaBytes(sch.Schema(), bodyBytes)

assert.False(t, valid)
assert.Len(t, errors, 1)
assert.Len(t, errors[0].SchemaValidationErrors, 3)

valid, errors = ValidateSchemaObject(sch.Schema(), cakePlease)

assert.False(t, valid)
assert.Len(t, errors, 1)
Expand All @@ -317,7 +362,13 @@ paths:
sch = m.Model.Paths.PathItems["/burgers/createBurger"].Post.RequestBody.Content["application/json"].Schema

// validate!
valid, errors = ValidateSchema(sch.Schema(), bodyBytes)
valid, errors = ValidateSchemaBytes(sch.Schema(), bodyBytes)

assert.False(t, valid)
assert.Len(t, errors, 1)
assert.Len(t, errors[0].SchemaValidationErrors, 3)

valid, errors = ValidateSchemaObject(sch.Schema(), death)

assert.False(t, valid)
assert.Len(t, errors, 1)
Expand Down

0 comments on commit 009333d

Please sign in to comment.