-
Notifications
You must be signed in to change notification settings - Fork 54
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
feat(draft2019-09): upgrading jsonschema to support draft2019-09 #63
Closed
+45,005
−246
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1d52efc
feat(draft2019-09): introduce tests for jsonschema to support draft20…
Arqu 17cbe24
fix(tests): make tests pass for existing use cases
Arqu 52f8d7d
fix(tests): preliminary support for
Arqu 84bdd64
fix(tests): updating to latest revisions of test data
Arqu 4aed327
fix(tests): make date-time format respect upper/lowercase T/Z
Arqu 1b039f6
fix(tests): iri support todo
Arqu d61f978
fix(tests): cleanup of test case definitions
Arqu acd4101
fix(tests): simply ignore extends for draft3
Arqu 4054ec0
fix(tests): traversal test update for draft2019-09
Arqu 417b4e7
store
Arqu ebeb2ba
anchor
Arqu d205a2a
hyper dirty - use ./neoschema to test
Arqu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
module github.com/qri-io/jsonschema | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/qri-io/jsonpointer v0.1.0 | ||
github.com/sergi/go-diff v1.0.0 | ||
github.com/stretchr/testify v1.3.0 // indirect | ||
) | ||
|
||
replace github.com/qri-io/jsonpointer => /Users/arqu/dev/qri/jsonpointer |
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,19 @@ | ||
package keywords | ||
|
||
import ( | ||
// kw "github.com/qri-io/jsonschema/keywords" | ||
) | ||
|
||
func LoadDraft() { | ||
// standard keywords | ||
kw.RegisterKeyword("type", NewVoid) | ||
kw.RegisterKeyword("enum", NewVoid) | ||
kw.RegisterKeyword("const", NewVoid) | ||
|
||
// numeric keywords | ||
kw.RegisterKeyword("multipleOf", NewVoid) | ||
kw.RegisterKeyword("maximum", NewVoid) | ||
kw.RegisterKeyword("exclusiveMaximum", NewVoid) | ||
kw.RegisterKeyword("minimum", NewVoid) | ||
kw.RegisterKeyword("exclusiveMinimum", NewVoid) | ||
} |
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,20 @@ | ||
package keywords | ||
|
||
import ( | ||
"fmt" | ||
js "github.com/qri-io/jsonschema" | ||
) | ||
|
||
type Id struct {} | ||
|
||
func NewId() Keyword { | ||
return &Id{} | ||
} | ||
|
||
func (i *Id) Validate(propPath string, data interface{}, errs *[]KeyError) { | ||
fmt.Println("WARN: Using Id Validator - always True") | ||
} | ||
|
||
func (i *Id) RegisterSubschemas(uri string, registry *js.SchemaRegistry) { | ||
|
||
} |
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,177 @@ | ||
package keywords | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
var notSupported = map[string]bool{ | ||
"$schema": true, | ||
"$id": true, | ||
"$anchor": true, | ||
"$recursiveAnchor": true, | ||
"$defs": true, | ||
"$ref": true, | ||
"$recursiveRef": true, | ||
"title": true, | ||
"description": true, | ||
"default": true, | ||
"examples": true, | ||
"readOnly": true, | ||
"writeOnly": true, | ||
"$comment": true, | ||
|
||
// string keywords | ||
"maxLength": true, | ||
"minLength": true, | ||
"pattern": true, | ||
|
||
// boolean keywords | ||
"allOf": true, | ||
"anyOf": true, | ||
"oneOf": true, | ||
"not": true, | ||
|
||
// array keywords | ||
"items": true, | ||
"additionalItems": true, | ||
"maxItems": true, | ||
"minItems": true, | ||
"uniqueItems": true, | ||
"contains": true, | ||
|
||
// object keywords | ||
"maxProperties": true, | ||
"minProperties": true, | ||
"required": true, | ||
"properties": true, | ||
"patternProperties": true, | ||
"additionalProperties": true, | ||
"dependencies": true, | ||
"propertyNames": true, | ||
|
||
// conditional keywords | ||
"if": true, | ||
"then": true, | ||
"else": true, | ||
|
||
//optional formats | ||
"format": true, | ||
} | ||
|
||
var KeywordRegistry = map[string]KeyMaker{} | ||
|
||
func IsKeyword(prop string) bool { | ||
_, ok := KeywordRegistry[prop] | ||
return ok | ||
} | ||
|
||
func GetKeyword(prop string) Keyword { | ||
if !IsKeyword(prop) { | ||
return NewVoid() | ||
} | ||
return KeywordRegistry[prop]() | ||
} | ||
|
||
func RegisterKeyword(prop string, maker KeyMaker) { | ||
KeywordRegistry[prop] = maker | ||
} | ||
|
||
// MaxValueErrStringLen sets how long a value can be before it's length is truncated | ||
// when printing error strings | ||
// a special value of -1 disables output trimming | ||
var MaxKeywordErrStringLen = 20 | ||
|
||
// Validator is an interface for anything that can validate. | ||
// JSON-Schema keywords are all examples of validators | ||
type Keyword interface { | ||
// Validate checks decoded JSON data and writes | ||
// validation errors (if any) to an outparam slice of ValErrors | ||
// propPath indicates the position of data in the json tree | ||
Validate(propPath string, data interface{}, errs *[]KeyError) | ||
// ValidateFromContext(schCtx *SchemaContext, errs *[]ValError) | ||
|
||
RegisterSubschemas(uri string, registry *SchemaRegistry) | ||
} | ||
|
||
// BaseValidator is a foundation for building a validator | ||
type BaseKeyword struct { | ||
path string | ||
} | ||
|
||
// SetPath sets base validator's path | ||
func (b *BaseKeyword) SetPath(path string) { | ||
b.path = path | ||
} | ||
|
||
// Path gives this validator's path | ||
func (b BaseKeyword) Path() string { | ||
return b.path | ||
} | ||
|
||
// AddError is a convenience method for appending a new error to an existing error slice | ||
func (b BaseKeyword) AddError(errs *[]KeyError, propPath string, data interface{}, msg string) { | ||
*errs = append(*errs, KeyError{ | ||
PropertyPath: propPath, | ||
RulePath: b.Path(), | ||
InvalidValue: data, | ||
Message: msg, | ||
}) | ||
} | ||
|
||
// ValMaker is a function that generates instances of a validator. | ||
// Calls to ValMaker will be passed directly to json.Marshal, | ||
// so the returned value should be a pointer | ||
type KeyMaker func() Keyword | ||
|
||
|
||
|
||
|
||
// ValError represents a single error in an instance of a schema | ||
// The only absolutely-required property is Message. | ||
type KeyError struct { | ||
// PropertyPath is a string path that leads to the | ||
// property that produced the error | ||
PropertyPath string `json:"propertyPath,omitempty"` | ||
// InvalidValue is the value that returned the error | ||
InvalidValue interface{} `json:"invalidValue,omitempty"` | ||
// RulePath is the path to the rule that errored | ||
RulePath string `json:"rulePath,omitempty"` | ||
// Message is a human-readable description of the error | ||
Message string `json:"message"` | ||
} | ||
|
||
// Error implements the error interface for ValError | ||
func (v KeyError) Error() string { | ||
// [propPath]: [value] [message] | ||
if v.PropertyPath != "" && v.InvalidValue != nil { | ||
return fmt.Sprintf("%s: %s %s", v.PropertyPath, InvalidValueString(v.InvalidValue), v.Message) | ||
} else if v.PropertyPath != "" { | ||
return fmt.Sprintf("%s: %s", v.PropertyPath, v.Message) | ||
} | ||
return v.Message | ||
} | ||
|
||
// InvalidValueString returns the errored value as a string | ||
func InvalidValueString(data interface{}) string { | ||
bt, err := json.Marshal(data) | ||
if err != nil { | ||
return "" | ||
} | ||
bt = bytes.Replace(bt, []byte{'\n', '\r'}, []byte{' '}, -1) | ||
if MaxKeywordErrStringLen != -1 && len(bt) > MaxKeywordErrStringLen { | ||
bt = append(bt[:MaxKeywordErrStringLen], []byte("...")...) | ||
} | ||
return string(bt) | ||
} | ||
|
||
// AddError creates and appends a ValError to errs | ||
func AddError(errs *[]KeyError, propPath string, data interface{}, msg string) { | ||
*errs = append(*errs, KeyError{ | ||
PropertyPath: propPath, | ||
InvalidValue: data, | ||
Message: msg, | ||
}) | ||
} | ||
|
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,19 @@ | ||
package keywords | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Template struct {} | ||
|
||
func NewTemplate() Keyword { | ||
return &Template{} | ||
} | ||
|
||
func (t *Template) Validate(propPath string, data interface{}, errs *[]KeyError) { | ||
fmt.Println("WARN: Using Template Validator - always True") | ||
} | ||
|
||
func (t *Template) RegisterSubschemas(uri string, registry *SchemaRegistry) { | ||
|
||
} |
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,19 @@ | ||
package keywords | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Void struct {} | ||
|
||
func NewVoid() Keyword { | ||
return &Void{} | ||
} | ||
|
||
func (vo *Void) Validate(propPath string, data interface{}, errs *[]KeyError) { | ||
fmt.Println("WARN: Using Void Validator - always True") | ||
} | ||
|
||
func (vo *Void) RegisterSubschemas(uri string, registry *SchemaRegistry) { | ||
|
||
} |
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.
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.
let's use go 1.13 for now. Apparently IPFS doesn't work with 1.14