Skip to content

Commit

Permalink
Addresses #424 and #423
Browse files Browse the repository at this point in the history
An off my one issue, I used the wrong comparitor and then did not build the correct tests required that would have caight it. This has now been fixed.

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Jan 20, 2024
1 parent 7062757 commit 90fec2e
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 8 deletions.
8 changes: 4 additions & 4 deletions functions/openapi/schema_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul
}

if schema.Value.Minimum != nil {
if *schema.Value.Minimum <= 0 {
if *schema.Value.Minimum < 0 {
result := st.buildResult("`minimum` should be a number greater than or equal to `0`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "minimum"),
schema, schema.Value.GoLow().Minimum.KeyNode, context)
Expand All @@ -92,7 +92,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul
}

if schema.Value.Maximum != nil {
if *schema.Value.Maximum <= 0 {
if *schema.Value.Maximum < 0 {
result := st.buildResult("`maximum` should be a number greater than or equal to `0`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maximum"),
schema, schema.Value.GoLow().Maximum.KeyNode, context)
Expand All @@ -109,7 +109,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul
}

if schema.Value.ExclusiveMinimum != nil {
if schema.Value.ExclusiveMinimum.IsB() && schema.Value.ExclusiveMinimum.B <= 0 {
if schema.Value.ExclusiveMinimum.IsB() && schema.Value.ExclusiveMinimum.B < 0 {
result := st.buildResult("`exclusiveMinimum` should be a number greater than or equal to `0`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "exclusiveMinimum"),
schema, schema.Value.GoLow().ExclusiveMinimum.KeyNode, context)
Expand All @@ -118,7 +118,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul
}

if schema.Value.ExclusiveMaximum != nil {
if schema.Value.ExclusiveMaximum.IsB() && schema.Value.ExclusiveMaximum.B <= 0 {
if schema.Value.ExclusiveMaximum.IsB() && schema.Value.ExclusiveMaximum.B < 0 {
result := st.buildResult("`exclusiveMaximum` should be a number greater than or equal to `0`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "exclusiveMaximum"),
schema, schema.Value.GoLow().ExclusiveMaximum.KeyNode, context)
Expand Down
162 changes: 161 additions & 1 deletion functions/openapi/schema_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,110 @@ components:
assert.Equal(t, "$.components.schemas['Gum'].minimum", res[0].Path)
}

func TestSchemaType_Minimum_Zero(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: number
minimum: 0`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}

m, _ := document.BuildV3Model()
path := "$"

drDocument := drModel.NewDrDocument(m)

rule := buildOpenApiTestRuleAction(path, "schema-type-check", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)

ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule

def := SchemaTypeCheck{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 0)
}

func TestSchemaType_Maximum_Zero(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: number
maximum: 0`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}

m, _ := document.BuildV3Model()
path := "$"

drDocument := drModel.NewDrDocument(m)

rule := buildOpenApiTestRuleAction(path, "schema-type-check", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)

ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule

def := SchemaTypeCheck{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 0)
}

func TestSchemaType_Maximum(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: number
maximum: -2`
maximum: 0`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}

m, _ := document.BuildV3Model()
path := "$"

drDocument := drModel.NewDrDocument(m)

rule := buildOpenApiTestRuleAction(path, "schema-type-check", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)

ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule

def := SchemaTypeCheck{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 0)
}

func TestSchemaType_Maximum_Negative(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: number
maximum: -50`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
Expand Down Expand Up @@ -385,6 +481,38 @@ components:
assert.Equal(t, "$.components.schemas['Gum'].exclusiveMinimum", res[0].Path)
}

func TestSchemaType_ExclusiveMinimum_Zero(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: number
exclusiveMinimum: 0`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}

m, _ := document.BuildV3Model()
path := "$"

drDocument := drModel.NewDrDocument(m)

rule := buildOpenApiTestRuleAction(path, "schema-type-check", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)

ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule

def := SchemaTypeCheck{}
res := def.RunRule(nil, ctx)
assert.Len(t, res, 0)

}

func TestSchemaType_ExclusiveMaximum(t *testing.T) {

yml := `openapi: 3.1
Expand Down Expand Up @@ -419,6 +547,38 @@ components:
assert.Equal(t, "$.components.schemas['Gum'].exclusiveMaximum", res[0].Path)
}

func TestSchemaType_ExclusiveMaximum_Zero(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: number
exclusiveMaximum: 0`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}

m, _ := document.BuildV3Model()
path := "$"

drDocument := drModel.NewDrDocument(m)

rule := buildOpenApiTestRuleAction(path, "schema-type-check", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)

ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule

def := SchemaTypeCheck{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 0)
}

func TestSchemaType_ExclusiveMinMaximum(t *testing.T) {

yml := `openapi: 3.1
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/pb33f/doctor v0.0.3
github.com/pb33f/libopenapi v0.15.0
github.com/pb33f/libopenapi v0.15.1
github.com/pb33f/libopenapi-validator v0.0.40
github.com/pterm/pterm v0.12.75
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/doctor v0.0.3 h1:EeZhRssFoqIgRswVyGYAK0l+AQRQ3akrRCDJEa5z+Yg=
github.com/pb33f/doctor v0.0.3/go.mod h1:yBs5hFHAoo/eeFvKN9sWwmHmgEPJ2SaotYOJc05GdMU=
github.com/pb33f/libopenapi v0.15.0 h1:AoBYIY3HXqDDF8O9kcudlqWaRFZZJmgtueE649oHzIw=
github.com/pb33f/libopenapi v0.15.0/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
github.com/pb33f/libopenapi v0.15.1 h1:DNWGhDDsvffPS8DhLkYErKZzbbtSvT7oZNBQSldWf+8=
github.com/pb33f/libopenapi v0.15.1/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
github.com/pb33f/libopenapi-validator v0.0.40 h1:oS/kPLnzX0GgtjrQkwsaqN2m/xHiYONWqRQ57gQ2K5U=
github.com/pb33f/libopenapi-validator v0.0.40/go.mod h1:VZ95obL+qvwoe6H2mhPW6NjJ3ip9q6BhP45sZupU7wM=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
Expand Down

0 comments on commit 90fec2e

Please sign in to comment.