This repository has been archived by the owner on May 11, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #64: prepare validation to move it to subpackage of domain
- Loading branch information
Showing
10 changed files
with
122 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package domain | ||
|
||
import "fmt" | ||
|
||
// AccumulatedError represents an error related to invalid input values. | ||
type AccumulatedError interface { | ||
error | ||
|
||
// InputWithErrors returns map of form inputs and their errors. | ||
InputWithErrors() map[Input][]error | ||
} | ||
|
||
type validationError struct { | ||
single bool | ||
position int | ||
value string | ||
message string | ||
} | ||
|
||
func (err validationError) Error() string { | ||
if err.single { | ||
if err.value != "" { | ||
return fmt.Sprintf("value %q at position %d is invalid: %s", err.value, err.position, err.message) | ||
} | ||
return fmt.Sprintf("value at position %d is invalid: %s", err.position, err.message) | ||
} | ||
return err.message | ||
} | ||
|
||
type dataValidationError struct { | ||
dataValidationResult | ||
} | ||
|
||
func (dataValidationError) Error() string { | ||
return "validation error" | ||
} | ||
|
||
func (err dataValidationError) InputWithErrors() map[Input][]error { | ||
m := make(map[Input][]error, len(err.results)) | ||
for _, r := range err.results { | ||
m[r.input] = r.errors | ||
} | ||
return m | ||
} | ||
|
||
type dataValidationResult struct { | ||
results []inputValidationResult | ||
} | ||
|
||
// AsError converts the result into error if it contains at least one input validation error. | ||
func (r dataValidationResult) AsError() AccumulatedError { | ||
for _, sub := range r.results { | ||
if sub.HasError() { | ||
return dataValidationError{r} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type inputValidationResult struct { | ||
input Input | ||
errors []error | ||
} | ||
|
||
// HasError returns true if the result contains at least one, not nil error. | ||
func (r inputValidationResult) HasError() bool { | ||
for _, err := range r.errors { | ||
if err != nil { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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
Empty file.
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
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