|
| 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