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

Validator not check property is nil #15

Open
bearx3f opened this issue Oct 27, 2017 · 1 comment
Open

Validator not check property is nil #15

bearx3f opened this issue Oct 27, 2017 · 1 comment

Comments

@bearx3f
Copy link

bearx3f commented Oct 27, 2017

I found some issue about validator It's not check the property is nil before try to get it's properties name and here is reproduce code

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "properties": {
        "prop_1": {
            "type": "string"
        },
        "prop_2": {
            "type": "string"
        },
        "child": {
            "type": "object",
            "properties": {
                "child_prop_1": {
                    "type": "string"
                }
            }
        }
    },
    "required": ["prop1"]
}
package restbot_test

import (
	"fmt"
	"testing"

	schema "github.com/lestrrat/go-jsschema"
	"github.com/lestrrat/go-jsschema/validator"
)

type TestSubject struct {
	Prop1 string            `json:"prop_1,omitempty"`
	Prop2 string            `json:"prop_2,omitempty"`
	Child *TestChildSubject `json:"child,omitempty"`
}

type TestChildSubject struct {
	ChildProp1 string `json:"child_prop_1,omitempty"`
}

func Test_Validator(t *testing.T) {
	data := TestSubject{
		Prop1: "aaa",
	}
	schemaFileName := "validate_schema.json"

	s, err := schema.ReadFile(schemaFileName)
	if err != nil {
		t.Fatal(fmt.Errorf("failed to read schema: %s", err))
	}
	v := validator.New(s)
	if err := v.Validate(data); err != nil {
		t.Fatal(fmt.Errorf("failed to validate data: %s", err))
	}
}

And error message

--- FAIL: Test_Validator (0.00s)
	z:\go\src\booboo-bear.com\restbot\validate_test.go:34: 
failed to validate data: validator 0xc04210dec0 
failed: object property 'child' validation 
failed: cannot get property names from this value (Kind: invalid)
FAIL
FAIL	booboo-bear.com/restbot	0.479s
Error: Tests failed.
@bearx3f
Copy link
Author

bearx3f commented Oct 27, 2017

i'm using this code to workaround but i don't how it's affect.

diff --git a/object.go b/object.go
index 274ce1f..7d04086 100644
--- a/object.go
+++ b/object.go
@@ -392,6 +392,8 @@ func (o *ObjectConstraint) Validate(v interface{}) (err error) {
                                out = mv.Call(nil)
                                pval = out[0]
                        }
+               case (pval.Kind() == reflect.Ptr || pval.Kind() == reflect.Interface) && pval.IsNil():
+                       // If we got nil, we're done for
                default:
                        // Everything else, we have *something*
                        propExists = true

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