Skip to content

Commit

Permalink
Merge errors for then/else + fix error description
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosh Sadiq committed Jan 26, 2018
1 parent 6664329 commit 4c91039
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
20 changes: 10 additions & 10 deletions errors.go
Expand Up @@ -146,13 +146,13 @@ type (
ResultErrorFields
}

// NumberIfThenError. ErrorDetails: -
NumberIfThenError struct {
// ConditionThenError. ErrorDetails: -
ConditionThenError struct {
ResultErrorFields
}

// NumberIfElseError. ErrorDetails: -
NumberIfElseError struct {
// ConditionElseError. ErrorDetails: -
ConditionElseError struct {
ResultErrorFields
}
)
Expand Down Expand Up @@ -240,12 +240,12 @@ func newError(err ResultError, context *jsonContext, value interface{}, locale l
case *NumberLTError:
t = "number_lt"
d = locale.NumberLT()
case *NumberIfThenError:
t = "number_if_then"
d = locale.NumberLT()
case *NumberIfElseError:
t = "number_if_else"
d = locale.NumberLT()
case *ConditionThenError:
t = "condition_then"
d = locale.ConditionThen()
case *ConditionElseError:
t = "condition_else"
d = locale.ConditionElse()
}

err.SetType(t)
Expand Down
8 changes: 4 additions & 4 deletions locales.go
Expand Up @@ -76,8 +76,8 @@ type (
HttpBadStatus() string
ParseError() string

NumberIfThen() string
NumberIfElse() string
ConditionThen() string
ConditionElse() string

// ErrorFormat
ErrorFormat() string
Expand Down Expand Up @@ -275,11 +275,11 @@ func (l DefaultLocale) ParseError() string {
}

//If/Else
func (l DefaultLocale) NumberIfThen() string {
func (l DefaultLocale) ConditionThen() string {
return `Must validate "then" as "if" was valid`
}

func (l DefaultLocale) NumberIfElse() string {
func (l DefaultLocale) ConditionElse() string {
return `Must validate "else" as "if" was not valid`
}

Expand Down
8 changes: 4 additions & 4 deletions schema_test.go
Expand Up @@ -220,15 +220,15 @@ func TestJsonSchemaTestSuite(t *testing.T) {
{"phase": "if then else", "test": "ignore else without if, valid when valid against lone else", "schema": "ifthenelse/schema_2.json", "data": "ifthenelse/data_20.json", "valid": "true"},
{"phase": "if then else", "test": "ignore else without if, valid when invailid against lone else", "schema": "ifthenelse/schema_2.json", "data": "ifthenelse/data_21.json", "valid": "true"},
{"phase": "if then else", "test": "if and then without else, valid through then", "schema": "ifthenelse/schema_3.json", "data": "ifthenelse/data_30.json", "valid": "true"},
{"phase": "if then else", "test": "if and then without else, invalid through then", "schema": "ifthenelse/schema_3.json", "data": "ifthenelse/data_31.json", "valid": "false", "errors": "number_if_then"},
{"phase": "if then else", "test": "if and then without else, invalid through then", "schema": "ifthenelse/schema_3.json", "data": "ifthenelse/data_31.json", "valid": "false", "errors": "condition_then"},
{"phase": "if then else", "test": "if and then without else, valid when if test fails", "schema": "ifthenelse/schema_3.json", "data": "ifthenelse/data_32.json", "valid": "true"},
{"phase": "if then else", "test": "if and else without then, valid when if test passes", "schema": "ifthenelse/schema_4.json", "data": "ifthenelse/data_40.json", "valid": "true"},
{"phase": "if then else", "test": "if and else without then, valid through else", "schema": "ifthenelse/schema_4.json", "data": "ifthenelse/data_41.json", "valid": "true"},
{"phase": "if then else", "test": "if and else without then, invalid through else", "schema": "ifthenelse/schema_4.json", "data": "ifthenelse/data_42.json", "valid": "false", "errors": "number_if_else"},
{"phase": "if then else", "test": "if and else without then, invalid through else", "schema": "ifthenelse/schema_4.json", "data": "ifthenelse/data_42.json", "valid": "false", "errors": "condition_else"},
{"phase": "if then else", "test": "validate against correct branch, then vs else, valid through then", "schema": "ifthenelse/schema_5.json", "data": "ifthenelse/data_50.json", "valid": "true"},
{"phase": "if then else", "test": "validate against correct branch, then vs else, invalid through then", "schema": "ifthenelse/schema_5.json", "data": "ifthenelse/data_51.json", "valid": "false", "errors": "number_if_then"},
{"phase": "if then else", "test": "validate against correct branch, then vs else, invalid through then", "schema": "ifthenelse/schema_5.json", "data": "ifthenelse/data_51.json", "valid": "false", "errors": "condition_then"},
{"phase": "if then else", "test": "validate against correct branch, then vs else, valid through else", "schema": "ifthenelse/schema_5.json", "data": "ifthenelse/data_52.json", "valid": "true"},
{"phase": "if then else", "test": "validate against correct branch, then vs else, invalid through else", "schema": "ifthenelse/schema_5.json", "data": "ifthenelse/data_53.json", "valid": "false", "errors": "number_if_else"},
{"phase": "if then else", "test": "validate against correct branch, then vs else, invalid through else", "schema": "ifthenelse/schema_5.json", "data": "ifthenelse/data_53.json", "valid": "false", "errors": "condition_else"},
{"phase": "if then else", "test": "non-interference across combined schemas, then vs else, valid, but woud have been invalid through then", "schema": "ifthenelse/schema_6.json", "data": "ifthenelse/data_60.json", "valid": "true"},
{"phase": "if then else", "test": "non-interference across combined schemas, then vs else, valid, but would have been invalid through else", "schema": "ifthenelse/schema_6.json", "data": "ifthenelse/data_61.json", "valid": "true"},
{"phase": "minProperties validation", "test": "longer is valid", "schema": "minProperties/schema_0.json", "data": "minProperties/data_00.json", "valid": "true"},
Expand Down
10 changes: 6 additions & 4 deletions validation.go
Expand Up @@ -379,13 +379,15 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte
if currentSubSchema._then != nil && validationResultIf.Valid() {
validationResultThen := currentSubSchema._then.subValidateWithContext(currentNode, context)
if !validationResultThen.Valid() {
result.addError(new(NumberIfThenError), context, currentNode, ErrorDetails{})
result.addError(new(ConditionThenError), context, currentNode, ErrorDetails{})
result.mergeErrors(validationResultThen)
}
}
if currentSubSchema._else != nil && !validationResultIf.Valid() {
validationResultThen := currentSubSchema._else.subValidateWithContext(currentNode, context)
if !validationResultThen.Valid() {
result.addError(new(NumberIfElseError), context, currentNode, ErrorDetails{})
validationResultElse := currentSubSchema._else.subValidateWithContext(currentNode, context)
if !validationResultElse.Valid() {
result.addError(new(ConditionElseError), context, currentNode, ErrorDetails{})
result.mergeErrors(validationResultElse)
}
}
}
Expand Down

0 comments on commit 4c91039

Please sign in to comment.