From 03b1f8a381ea4770a2b59ba59bfe13ebfc23f1e3 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 24 Oct 2024 18:24:27 +0200 Subject: [PATCH 1/3] [ISSUE-376] add validation for json schema [hacktoberfest][checktoberfest] --- go.mod | 10 +++++++++- validation.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b709d7f..c9ad6b0 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,15 @@ module github.com/xeipuuv/gojsonschema +go 1.22.5 + require ( github.com/stretchr/testify v1.3.0 - github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 ) + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.0 // indirect + github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect +) diff --git a/validation.go b/validation.go index 9081bd9..c46335c 100644 --- a/validation.go +++ b/validation.go @@ -27,6 +27,7 @@ package gojsonschema import ( "encoding/json" + "errors" "math/big" "reflect" "regexp" @@ -42,9 +43,22 @@ func Validate(ls JSONLoader, ld JSONLoader) (*Result, error) { if err != nil { return nil, err } + + if err := schema.ValidateSchema(); err != nil { + return nil, err + } + return schema.Validate(ld) } +func (v *Schema) ValidateSchema() error { + if v.rootSchema == nil || v.rootSchema.types.types == nil { + return errors.New("type is missing in root") + } + + return nil +} + // Validate loads and validates a JSON document func (v *Schema) Validate(l JSONLoader) (*Result, error) { root, err := l.LoadJSON() From 7ec044cdaee375bcc6eea5eba32d8bb4a35c58a3 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 24 Oct 2024 18:25:56 +0200 Subject: [PATCH 2/3] [ISSUE-376] add validation for json schema [hacktoberfest][checktoberfest] --- go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/go.mod b/go.mod index c9ad6b0..d1d0f00 100644 --- a/go.mod +++ b/go.mod @@ -11,5 +11,4 @@ require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.0 // indirect - github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect ) From 6167c3886972e514995aacaf79d0df969b672122 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 24 Oct 2024 18:26:33 +0200 Subject: [PATCH 3/3] [ISSUE-376] add validation for json schema [hacktoberfest][checktoberfest] --- go.mod | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index d1d0f00..8d787e7 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,7 @@ module github.com/xeipuuv/gojsonschema -go 1.22.5 - require ( github.com/stretchr/testify v1.3.0 + github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 -) - -require ( - github.com/davecgh/go-spew v1.1.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.1.0 // indirect -) +) \ No newline at end of file