@@ -3,6 +3,7 @@ package helpers
33import (
44 "testing"
55
6+ "github.com/stretchr/testify/assert"
67 "github.com/stretchr/testify/require"
78
89 "github.com/pb33f/libopenapi-validator/config"
@@ -61,3 +62,52 @@ func Test_ObjectSchema(t *testing.T) {
6162 require .Nil (t , err , "Failed to compile Schema: %v" , err )
6263 require .NotNil (t , jsch , "Did not return a compiled schema" )
6364}
65+
66+ func Test_ValidJSONSchemaWithInvalidContent (t * testing.T ) {
67+ // An example of a dubious JSON Schema
68+ const badSchema = `{
69+ "type": "you-dont-know-me",
70+ "format": "date",
71+ "minLength": 10
72+ }`
73+
74+ jsch , err := NewCompiledSchema ("test" , []byte (badSchema ), nil )
75+
76+ assert .NotNil (t , err , "Expected an error to be thrown" )
77+ assert .Nil (t , jsch , "invalid schema compiled!" )
78+ }
79+
80+ func Test_MalformedSONSchema (t * testing.T ) {
81+ // An example of a JSON schema with malformed JSON
82+ const badSchema = `{
83+ "type": "you-dont-know-me",
84+ "format": "date"
85+ "minLength": 10
86+ }`
87+
88+ jsch , err := NewCompiledSchema ("test" , []byte (badSchema ), nil )
89+
90+ assert .NotNil (t , err , "Expected an error to be thrown" )
91+ assert .Nil (t , jsch , "invalid schema compiled!" )
92+ }
93+
94+ func Test_ValidJSONSchemaWithIncompleteContent (t * testing.T ) {
95+ // An example of a dJSON schema with references to non-existent stuff
96+ const incompleteSchema = `{
97+ "type": "object",
98+ "title" : "unresolvable",
99+ "properties" : {
100+ "name" : {
101+ "type": "string",
102+ },
103+ "species" : {
104+ "$ref": "#/$defs/speciesEnum"
105+ }
106+ }
107+ }`
108+
109+ jsch , err := NewCompiledSchema ("test" , []byte (incompleteSchema ), nil )
110+
111+ assert .NotNil (t , err , "Expected an error to be thrown" )
112+ assert .Nil (t , jsch , "invalid schema compiled!" )
113+ }
0 commit comments