Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using RawLoader, int with exclusiveMinimum 0 validates with 0 value #341

Open
Helyk opened this issue Nov 18, 2021 · 1 comment
Open

Comments

@Helyk
Copy link

Helyk commented Nov 18, 2021

Hello. Here is an example code to confirm the issue:

package main

import (
	"fmt"

	"github.com/xeipuuv/gojsonschema"
)

var schema = `{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "a": {"type": "integer", "exclusiveMinimum": 0}
    }
}`

var doc = struct {
	A int
}{
	A: 0,
}

func main() {
	schemaLoader := gojsonschema.NewBytesLoader([]byte(schema))
	documentLoader := gojsonschema.NewRawLoader(doc) // <- note RawLoader here

	result, err := gojsonschema.Validate(schemaLoader, documentLoader)
	if err != nil {
		panic(err.Error())
	}

	if result.Valid() {
		fmt.Printf("The document is valid\n")
	} else {
		fmt.Printf("The document is not valid. see errors :\n")
		for _, desc := range result.Errors() {
			fmt.Printf("- %s\n", desc)
		}
	}
}

I saved in on playground so that you can run it and see that it results in The document is valid, while value of A is zero, which is prohibited by "exclusiveMinimum": 0 so it shouldn't validate.

To compare with, ByteLoader with same data gives appropriate error.

@Helyk Helyk changed the title When using RawLoader, int with exclusiveMinimum 0 validates with value When using RawLoader, int with exclusiveMinimum 0 validates with 0 value Nov 18, 2021
@Helyk
Copy link
Author

Helyk commented Nov 18, 2021

Uh I thought it might be due to 0 being int's zero-value and some omitempty behavior or because of A being capital but now I tried to alter those factors and it still validates no matter what.

See example:

package main

import (
	"fmt"

	"github.com/xeipuuv/gojsonschema"
)

var schema = `{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "A": {"type": "integer", "exclusiveMinimum": 1}
    },
    "required": ["A"]
}`

var doc = struct {
	B int
}{
	B: 1,
}

func main() {
	schemaLoader := gojsonschema.NewBytesLoader([]byte(schema))
	documentLoader := gojsonschema.NewRawLoader(doc)

	result, err := gojsonschema.Validate(schemaLoader, documentLoader)
	if err != nil {
		panic(err.Error())
	}

	if result.Valid() {
		fmt.Printf("The document is valid\n")
	} else {
		fmt.Printf("The document is not valid. see errors :\n")
		for _, desc := range result.Errors() {
			fmt.Printf("- %s\n", desc)
		}
	}
}

// The document is valid

I'm confused now. Why it validates? Am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant