diff --git a/check_valid.go b/check_valid.go index e40d72a..62ee13c 100644 --- a/check_valid.go +++ b/check_valid.go @@ -3,7 +3,7 @@ package commons import ( "encoding/json" "log" - + "github.com/ianlancetaylor/jsonschema" "github.com/ianlancetaylor/jsonschema/draft7" ) @@ -22,6 +22,7 @@ func Validate(schema string, dataAny any) error { vldtor, err := jsonschema.SchemaFromJSON(draft7.SchemaID, nil, v) if err != nil { + log.Printf("Failed to load schema %v", err) return err } @@ -29,8 +30,10 @@ func Validate(schema string, dataAny any) error { valid := vldtor.Validate(data) if valid != nil { + log.Printf("Invalid json: %v", valid) + return valid } return nil -} +} \ No newline at end of file diff --git a/check_valid_test.go b/check_valid_test.go index d7489a7..d48af50 100644 --- a/check_valid_test.go +++ b/check_valid_test.go @@ -44,3 +44,4 @@ func TestPassingValidate (t *testing.T) { t.Fatal("Should have not thrown an error") } } + diff --git a/go.mod b/go.mod index e30f138..5b64255 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,14 @@ go 1.25.0 require ( github.com/go-viper/mapstructure/v2 v2.5.0 github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5 + github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 github.com/sergi/go-diff v1.4.0 + github.com/xeipuuv/gojsonschema v1.2.0 go.mongodb.org/mongo-driver/v2 v2.5.0 golang.org/x/text v0.30.0 ) + +require ( + github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect + github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect +) diff --git a/go.sum b/go.sum index 83ce7e4..937cce9 100644 --- a/go.sum +++ b/go.sum @@ -12,11 +12,20 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4= +github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY= github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= +github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= +github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= diff --git a/json.go b/json.go index d24f974..7fd86c0 100644 --- a/json.go +++ b/json.go @@ -60,4 +60,17 @@ func JsonSprint(a any) string { func JsonPrint(a any) { log.Printf("%s", JsonSprint(a)) +} + +func JsonToMap(s string) (map[string]any, error) { + b := []byte(s) + + var schema map[string]any + if err := json.Unmarshal(b, &schema); err != nil { + log.Printf("Failed to parse json: %v", err) + + return nil, err + } + + return schema, nil } \ No newline at end of file diff --git a/struct.go b/struct.go index 09f23ab..8177797 100644 --- a/struct.go +++ b/struct.go @@ -217,7 +217,13 @@ func BsonAnyToMap(v any) any { m[k] = BsonAnyToMap(v2) } return m - + case map[string]any: + m := map[string]any{} + val := v.(map[string]any) + for i, v2 := range val { + m[i] = BsonAnyToMap(v2) + } + return m case bson.A: val := v.(bson.A) arr := make([]any, len(val)) @@ -229,6 +235,8 @@ func BsonAnyToMap(v any) any { default: return v } + + return v } func BsonToMap(b bson.M) map[string]any {