-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from carolynvs/jsonschema-lock
Make validating a bundle thread-safe
- Loading branch information
Showing
5 changed files
with
37 additions
and
21 deletions.
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
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 |
---|---|---|
@@ -1,45 +1,54 @@ | ||
package definition | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/qri-io/jsonpointer" | ||
"github.com/qri-io/jsonschema" | ||
) | ||
|
||
// ContentEncoding represents a "custom" Schema property | ||
type ContentEncoding string | ||
|
||
// NewContentEncoding allocates a new ContentEncoding validator | ||
func NewContentEncoding() jsonschema.Validator { | ||
func NewContentEncoding() jsonschema.Keyword { | ||
return new(ContentEncoding) | ||
} | ||
|
||
// Validate implements the Validator interface for ContentEncoding | ||
// which, as of writing, isn't included by default in the jsonschema library we consume | ||
func (c ContentEncoding) Validate(propPath string, data interface{}, errs *[]jsonschema.ValError) { | ||
func (c ContentEncoding) Validate(propPath string, data interface{}, errs *[]jsonschema.KeyError) {} | ||
|
||
func (c ContentEncoding) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) { | ||
if obj, ok := data.(string); ok { | ||
switch c { | ||
case "base64": | ||
_, err := base64.StdEncoding.DecodeString(obj) | ||
if err != nil { | ||
jsonschema.AddError(errs, propPath, data, fmt.Sprintf("invalid %s value: %s", c, obj)) | ||
currentState.AddError(data, fmt.Sprintf("invalid %s value: %s", c, obj)) | ||
} | ||
// Add validation support for other encodings as needed | ||
// See https://json-schema.org/latest/json-schema-validation.html#rfc.section.8.3 | ||
default: | ||
jsonschema.AddError(errs, propPath, data, fmt.Sprintf("unsupported or invalid contentEncoding type of %s", c)) | ||
currentState.AddError(data, fmt.Sprintf("unsupported or invalid contentEncoding type of %s", c)) | ||
} | ||
} | ||
} | ||
|
||
func (c ContentEncoding) Register(uri string, registry *jsonschema.SchemaRegistry) {} | ||
|
||
func (c ContentEncoding) Resolve(pointer jsonpointer.Pointer, uri string) *jsonschema.Schema { | ||
return nil | ||
} | ||
|
||
// NewRootSchema returns a jsonschema.RootSchema with any needed custom | ||
// jsonschema.Validators pre-registered | ||
func NewRootSchema() *jsonschema.RootSchema { | ||
func NewRootSchema() *jsonschema.Schema { | ||
// Register custom validators here | ||
// Note: as of writing, jsonschema doesn't have a stock validator for instances of type `contentEncoding` | ||
// There may be others missing in the library that exist in http://json-schema.org/draft-07/schema# | ||
// and thus, we'd need to create/register them here (if not included upstream) | ||
jsonschema.RegisterValidator("contentEncoding", NewContentEncoding) | ||
return new(jsonschema.RootSchema) | ||
jsonschema.RegisterKeyword("contentEncoding", NewContentEncoding) | ||
jsonschema.LoadDraft2019_09() | ||
return &jsonschema.Schema{} | ||
} |
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