Skip to content

Commit

Permalink
Implement error in order to fullfill error interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
masv3971 authored and kaptinlin committed Nov 7, 2024
1 parent 3cf1dd1 commit a0bfaf0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
32 changes: 18 additions & 14 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ package jsonschema
import "github.com/kaptinlin/go-i18n"

type EvaluationError struct {
keyword string `json:"-"`
code string `json:"-"`
message string `json:"-"`
params map[string]interface{} `json:"-"`
Keyword string `json:"keyword"`
Code string `json:"code"`
Message string `json:"message"`
Params map[string]interface{} `json:"params"`
}

func NewEvaluationError(keyword string, code string, message string, params ...map[string]interface{}) *EvaluationError {
if len(params) > 0 {
return &EvaluationError{
keyword: keyword,
code: code,
message: message,
params: params[0],
Keyword: keyword,
Code: code,
Message: message,
Params: params[0],
}
} else {
return &EvaluationError{
keyword: keyword,
code: code,
message: message,
Keyword: keyword,
Code: code,
Message: message,
}
}
}

func (e *EvaluationError) Error() string {
return replace(e.message, e.params)
return replace(e.Message, e.Params)
}

func (e *EvaluationError) Localize(localizer *i18n.Localizer) string {
if localizer != nil {
return localizer.Get(e.code, i18n.Vars(e.params))
return localizer.Get(e.Code, i18n.Vars(e.Params))
} else {
return e.Error()
}
Expand Down Expand Up @@ -80,6 +80,10 @@ func (e *EvaluationResult) SetEvaluationPath(evaluationPath string) *EvaluationR
return e
}

func (e *EvaluationResult) Error() string {
return "evaluation failed"
}

func (e *EvaluationResult) SetSchemaLocation(location string) *EvaluationResult {
e.SchemaLocation = location

Expand Down Expand Up @@ -111,7 +115,7 @@ func (e *EvaluationResult) AddError(err *EvaluationError) *EvaluationResult {
e.Valid = false
}

e.Errors[err.keyword] = err
e.Errors[err.Keyword] = err
return e
}

Expand Down
24 changes: 12 additions & 12 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ func TestToList(t *testing.T) {
},
Errors: map[string]*EvaluationError{
"required": {
keyword: "required",
code: "missing_required_property",
message: "Required property {property} is missing",
params: map[string]interface{}{
Keyword: "required",
Code: "missing_required_property",
Message: "Required property {property} is missing",
Params: map[string]interface{}{
"property": "fieldName1",
},
},
"minLength": {
keyword: "minLength",
code: "string_too_short",
message: "Value should be at least {min_length} characters",
params: map[string]interface{}{
Keyword: "minLength",
Code: "string_too_short",
Message: "Value should be at least {min_length} characters",
Params: map[string]interface{}{
"minLength": 5,
},
},
Expand All @@ -145,10 +145,10 @@ func TestToList(t *testing.T) {
EvaluationPath: "/property",
Errors: map[string]*EvaluationError{
"format": {
keyword: "format",
code: "format_mismatch",
message: "Value does not match format {format}",
params: map[string]interface{}{
Keyword: "format",
Code: "format_mismatch",
Message: "Value does not match format {format}",
Params: map[string]interface{}{
"format": "email",
},
},
Expand Down

0 comments on commit a0bfaf0

Please sign in to comment.