forked from qor/validations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidations.go
More file actions
30 lines (24 loc) · 775 Bytes
/
validations.go
File metadata and controls
30 lines (24 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package validations
import (
"fmt"
"github.com/PatronGG/gorm"
)
// NewError generate a new error for a model's field
func NewError(resource interface{}, column, err string) error {
return &Error{Resource: resource, Column: column, Message: err}
}
// Error is a validation error struct that hold model, column and error message
type Error struct {
Resource interface{}
Column string
Message string
}
// Label is a label including model type, primary key and column name
func (err Error) Label() string {
scope := gorm.Scope{Value: err.Resource}
return fmt.Sprintf("%v_%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue(), err.Column)
}
// Error show error message
func (err Error) Error() string {
return fmt.Sprintf("%v", err.Message)
}