-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return better error messages for invalid JSON schema types in templat…
…es (#661) ## Changes Adds a function to validate json schema types added by the author. The default json unmarshaller does not validate that the parsed type matches the enum defined in `jsonschema.Type` Includes some other improvements to provide better error messages. This PR was prompted by usability difficulties reported by @mingyu89 during mlops stack migration. ## Tests Unit tests
- Loading branch information
1 parent
6e708da
commit 878bb6d
Showing
6 changed files
with
135 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJsonSchemaValidate(t *testing.T) { | ||
var err error | ||
toSchema := func(s string) *Schema { | ||
return &Schema{ | ||
Properties: map[string]*Schema{ | ||
"foo": { | ||
Type: Type(s), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
err = toSchema("string").validate() | ||
assert.NoError(t, err) | ||
|
||
err = toSchema("boolean").validate() | ||
assert.NoError(t, err) | ||
|
||
err = toSchema("number").validate() | ||
assert.NoError(t, err) | ||
|
||
err = toSchema("integer").validate() | ||
assert.NoError(t, err) | ||
|
||
err = toSchema("int").validate() | ||
assert.EqualError(t, err, "type int is not a recognized json schema type. Please use \"integer\" instead") | ||
|
||
err = toSchema("float").validate() | ||
assert.EqualError(t, err, "type float is not a recognized json schema type. Please use \"number\" instead") | ||
|
||
err = toSchema("bool").validate() | ||
assert.EqualError(t, err, "type bool is not a recognized json schema type. Please use \"boolean\" instead") | ||
|
||
err = toSchema("foobar").validate() | ||
assert.EqualError(t, err, "type foobar is not a recognized json schema type") | ||
} |
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