-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable 'Format' and 'Content' assertions during schema validation #116
Open
JemDay
wants to merge
8
commits into
pb33f:main
Choose a base branch
from
JemDay:jem-param
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea021d4
Enhance validation configuration options to include assertions for 'f…
JemDay b839f3c
- Propogate errors from helper functions
JemDay 6fc5851
- Address Linting issues.
JemDay 2484c6a
- Increase test coverage.
JemDay 8e7e11a
- Address review comments
JemDay a4107b9
- Address review comments
JemDay a9f8881
- Address review comments
JemDay 4b65d02
- Fixed corrupted tests (more haste, less speed)
JemDay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package helpers | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/santhosh-tekuri/jsonschema/v6" | ||
|
||
"github.com/pb33f/libopenapi-validator/config" | ||
) | ||
|
||
// ConfigureCompiler configures a JSON Schema compiler with the desired behavior. | ||
func ConfigureCompiler(c *jsonschema.Compiler, o *config.ValidationOptions) { | ||
// Sanity | ||
if o == nil { | ||
return | ||
} | ||
JemDay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// nil is the default so this is OK. | ||
c.UseRegexpEngine(o.RegexEngine) | ||
|
||
// Enable Format assertions if required. | ||
if o.FormatAssertions { | ||
c.AssertFormat() | ||
} | ||
|
||
// Content Assertions | ||
if o.ContentAssertions { | ||
c.AssertContent() | ||
} | ||
} | ||
|
||
// NewCompilerWithOptions mints a new JSON schema compiler with custom configuration. | ||
func NewCompilerWithOptions(o *config.ValidationOptions) *jsonschema.Compiler { | ||
// Build it | ||
c := jsonschema.NewCompiler() | ||
|
||
// Configure it | ||
ConfigureCompiler(c, o) | ||
|
||
// Return it | ||
return c | ||
} | ||
|
||
// NewCompiledSchema establishes a programmatic representation of a JSON Schema document that is used for validation. | ||
func NewCompiledSchema(name string, jsonSchema []byte, o *config.ValidationOptions) (*jsonschema.Schema, error) { | ||
// Fake-Up a resource name for the schema | ||
resourceName := fmt.Sprintf("%s.json", name) | ||
|
||
// Establish a compiler with the desired configuration | ||
compiler := NewCompilerWithOptions(o) | ||
compiler.UseLoader(NewCompilerLoader()) | ||
|
||
// Decode the JSON Schema into a JSON blob. | ||
decodedSchema, err := jsonschema.UnmarshalJSON(bytes.NewReader(jsonSchema)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal JSON schema: %w", err) | ||
} | ||
|
||
// Give our schema to the compiler. | ||
err = compiler.AddResource(resourceName, decodedSchema) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to add resource to schema compiler: %w", err) | ||
} | ||
|
||
// Try to compile it. | ||
jsch, err := compiler.Compile(resourceName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to compile JSON schema: %w", err) | ||
} | ||
|
||
// Done. | ||
return jsch, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/pb33f/libopenapi-validator/config" | ||
) | ||
|
||
// A few simple JSON Schemas | ||
const stringSchema = `{ | ||
"type": "string", | ||
"format": "date", | ||
"minLength": 10 | ||
}` | ||
|
||
const objectSchema = `{ | ||
"type": "object", | ||
"title" : "Fish", | ||
"properties" : { | ||
"name" : { | ||
"type": "string", | ||
"description": "The given name of the fish" | ||
}, | ||
"species" : { | ||
"type" : "string", | ||
"enum" : [ "OTHER", "GUPPY", "PIKE", "BASS" ] | ||
} | ||
} | ||
}` | ||
|
||
func Test_SchemaWithNilOptions(t *testing.T) { | ||
jsch, err := NewCompiledSchema("test", []byte(stringSchema), nil) | ||
|
||
require.NoError(t, err, "Failed to compile Schema") | ||
require.NotNil(t, jsch, "Did not return a compiled schema") | ||
} | ||
|
||
func Test_SchemaWithDefaultOptions(t *testing.T) { | ||
valOptions := config.NewValidationOptions() | ||
jsch, err := NewCompiledSchema("test", []byte(stringSchema), valOptions) | ||
|
||
require.NoError(t, err, "Failed to compile Schema") | ||
require.NotNil(t, jsch, "Did not return a compiled schema") | ||
} | ||
|
||
func Test_SchemaWithOptions(t *testing.T) { | ||
valOptions := config.NewValidationOptions(config.WithFormatAssertions(), config.WithContentAssertions()) | ||
|
||
jsch, err := NewCompiledSchema("test", []byte(stringSchema), valOptions) | ||
|
||
require.NoError(t, err, "Failed to compile Schema") | ||
require.NotNil(t, jsch, "Did not return a compiled schema") | ||
} | ||
|
||
func Test_ObjectSchema(t *testing.T) { | ||
valOptions := config.NewValidationOptions() | ||
jsch, err := NewCompiledSchema("test", []byte(objectSchema), valOptions) | ||
|
||
require.NoError(t, err, "Failed to compile Schema") | ||
require.NotNil(t, jsch, "Did not return a compiled schema") | ||
} | ||
|
||
func Test_ValidJSONSchemaWithInvalidContent(t *testing.T) { | ||
// An example of a dubious JSON Schema | ||
const badSchema = `{ | ||
"type": "you-dont-know-me", | ||
"format": "date", | ||
"minLength": 10 | ||
}` | ||
|
||
jsch, err := NewCompiledSchema("test", []byte(badSchema), nil) | ||
|
||
assert.NotNil(t, err, "Expected an error to be thrown") | ||
JemDay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Nil(t, jsch, "invalid schema compiled!") | ||
} | ||
|
||
func Test_MalformedSONSchema(t *testing.T) { | ||
// An example of a JSON schema with malformed JSON | ||
const badSchema = `{ | ||
"type": "you-dont-know-me", | ||
"format": "date" | ||
"minLength": 10 | ||
}` | ||
|
||
jsch, err := NewCompiledSchema("test", []byte(badSchema), nil) | ||
|
||
assert.NotNil(t, err, "Expected an error to be thrown") | ||
JemDay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Nil(t, jsch, "invalid schema compiled!") | ||
} | ||
|
||
func Test_ValidJSONSchemaWithIncompleteContent(t *testing.T) { | ||
// An example of a dJSON schema with references to non-existent stuff | ||
const incompleteSchema = `{ | ||
"type": "object", | ||
"title" : "unresolvable", | ||
"properties" : { | ||
"name" : { | ||
"type": "string", | ||
}, | ||
"species" : { | ||
"$ref": "#/$defs/speciesEnum" | ||
} | ||
} | ||
}` | ||
|
||
jsch, err := NewCompiledSchema("test", []byte(incompleteSchema), nil) | ||
|
||
assert.NotNil(t, err, "Expected an error to be thrown") | ||
JemDay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Nil(t, jsch, "invalid schema compiled!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this
The sanity is in the nil check. The comment is misleading otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opted (no pun intended) to remove the comment but leave the logic as-is ... seemed more logical to me.