Skip to content

Commit 7fd7aeb

Browse files
JemDaydaveshanley
authored andcommitted
- Propogate errors from helper functions
- Updated to cover all situations where JSON schema compilation occurs. - A few new Unts tests to improve coverage.
1 parent ded9265 commit 7fd7aeb

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

helpers/schema_compiler_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package helpers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pb33f/libopenapi-validator/config"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// A few simple JSON Schemas
11+
const stringSchema = `{
12+
"type": "string",
13+
"format": "date",
14+
"minLength": 10
15+
}`
16+
17+
const objectSchema = `{
18+
"type": "object",
19+
"title" : "Fish",
20+
"properties" : {
21+
"name" : {
22+
"type": "string",
23+
"description": "The given name of the fish"
24+
},
25+
"species" : {
26+
"type" : "string",
27+
"enum" : [ "OTHER", "GUPPY", "PIKE", "BASS" ]
28+
}
29+
}
30+
}`
31+
32+
func Test_SchemaWithNilOptions(t *testing.T) {
33+
jsch, err := NewCompiledSchema("test", []byte(stringSchema), nil)
34+
35+
require.Nil(t, err, "Failed to compile Schema: %v", err)
36+
require.NotNil(t, jsch, "Did not return a compiled schema")
37+
}
38+
39+
func Test_SchemaWithDefaultOptions(t *testing.T) {
40+
valOptions := config.NewValidationOptions()
41+
jsch, err := NewCompiledSchema("test", []byte(stringSchema), valOptions)
42+
43+
require.Nil(t, err, "Failed to compile Schema: %v", err)
44+
require.NotNil(t, jsch, "Did not return a compiled schema")
45+
}
46+
47+
func Test_SchemaWithOptions(t *testing.T) {
48+
valOptions := config.NewValidationOptions(config.WithFormatAssertions(), config.WithContentAssertions())
49+
50+
jsch, err := NewCompiledSchema("test", []byte(stringSchema), valOptions)
51+
52+
require.Nil(t, err, "Failed to compile Schema: %v", err)
53+
require.NotNil(t, jsch, "Did not return a compiled schema")
54+
}
55+
56+
func Test_ObjectSchema(t *testing.T) {
57+
valOptions := config.NewValidationOptions()
58+
jsch, err := NewCompiledSchema("test", []byte(objectSchema), valOptions)
59+
60+
require.Nil(t, err, "Failed to compile Schema: %v", err)
61+
require.NotNil(t, jsch, "Did not return a compiled schema")
62+
}

0 commit comments

Comments
 (0)