Skip to content
Snippets Groups Projects
Unverified Commit 00a6ebee authored by Ivan Porto Carrero's avatar Ivan Porto Carrero Committed by GitHub
Browse files

Merge pull request #26 from youyuanwu/feature/ReadOnly-Error

Add ReadOnly Error
parents 7f224678 91d424a6
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ const (
typeFailWithData = "%s in %s must be of type %s: %q"
typeFailWithError = "%s in %s must be of type %s, because: %s"
requiredFail = "%s in %s is required"
readOnlyFail = "%s in %s is readOnly"
tooLongMessage = "%s in %s should be at most %d chars long"
tooShortMessage = "%s in %s should be at least %d chars long"
patternFail = "%s in %s should match '%s'"
......@@ -41,6 +42,7 @@ const (
typeFailWithDataNoIn = "%s must be of type %s: %q"
typeFailWithErrorNoIn = "%s must be of type %s, because: %s"
requiredFailNoIn = "%s is required"
readOnlyFailNoIn = "%s is readOnly"
tooLongMessageNoIn = "%s should be at most %d chars long"
tooShortMessageNoIn = "%s should be at least %d chars long"
patternFailNoIn = "%s should match '%s'"
......@@ -91,6 +93,7 @@ const (
UnallowedPropertyCode
FailedAllPatternPropsCode
MultipleOfMustBePositiveCode
ReadOnlyFailCode
)
// CompositeError is an error that groups several errors together
......@@ -501,6 +504,23 @@ func Required(name, in string, value interface{}) *Validation {
}
}
// ReadOnly error for when a value is present in request
func ReadOnly(name, in string, value interface{}) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(readOnlyFailNoIn, name)
} else {
msg = fmt.Sprintf(readOnlyFail, name, in)
}
return &Validation{
code: ReadOnlyFailCode,
Name: name,
In: in,
Value: value,
message: msg,
}
}
// TooLong error for when a string is too long
func TooLong(name, in string, max int64, value interface{}) *Validation {
var msg string
......
......@@ -273,6 +273,18 @@ func TestSchemaErrors(t *testing.T) {
assert.Equal(t, "something is required", err.Error())
assert.Equal(t, nil, err.Value)
err = ReadOnly("something", "query", nil)
assert.Error(t, err)
assert.EqualValues(t, ReadOnlyFailCode, err.Code())
assert.Equal(t, "something in query is readOnly", err.Error())
assert.Equal(t, nil, err.Value)
err = ReadOnly("something", "", nil)
assert.Error(t, err)
assert.EqualValues(t, ReadOnlyFailCode, err.Code())
assert.Equal(t, "something is readOnly", err.Error())
assert.Equal(t, nil, err.Value)
err = TooLong("something", "query", 5, "abcdef")
assert.Error(t, err)
assert.EqualValues(t, TooLongFailCode, err.Code())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment