feat: user domain -- errors
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package errors
|
||||
|
||||
var (
|
||||
ErrPhoneInvalid = NewAppError(400, "Invalid phone number", ErrorTypeValidation, nil)
|
||||
ErrNationalIDInvalid = NewAppError(400, "Invalid national ID", ErrorTypeValidation, nil)
|
||||
)
|
||||
@@ -0,0 +1,51 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type ErrorType string
|
||||
|
||||
const (
|
||||
ErrorTypeValidation ErrorType = "VALIDATION_ERROR"
|
||||
|
||||
ErrorTypeAuth ErrorType = "AUTH_ERROR"
|
||||
)
|
||||
|
||||
type AppError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Type ErrorType `json:"type"`
|
||||
Cause error `json:"_"`
|
||||
}
|
||||
|
||||
func NewAppError(code int, msg string, typ ErrorType, cause error) *AppError {
|
||||
return &AppError{
|
||||
Code: code,
|
||||
Message: msg,
|
||||
Type: typ,
|
||||
Cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (e *AppError) Unwrap() error {
|
||||
return e.Cause
|
||||
}
|
||||
func (e *AppError) Is(target error) bool {
|
||||
if t, ok := target.(*AppError); ok {
|
||||
return e.Type == t.Type
|
||||
}
|
||||
return errors.Is(e.Cause, target)
|
||||
}
|
||||
|
||||
func (e *AppError) As(target interface{}) bool {
|
||||
if t, ok := target.(**AppError); ok {
|
||||
*t = e
|
||||
return true
|
||||
}
|
||||
return errors.As(e.Cause, target)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package national
|
||||
|
||||
import (
|
||||
"backend/pkg/errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func IsValid(value string) (bool, error) {
|
||||
valueCount := len(value)
|
||||
if valueCount < 8 {
|
||||
return false, errors.ErrNationalIDInvalid
|
||||
}
|
||||
|
||||
if valueCount >= 8 && valueCount < 10 {
|
||||
value = strings.Repeat("0", 10-valueCount) + value
|
||||
}
|
||||
|
||||
var valueSlices []uint8
|
||||
for _, s := range value {
|
||||
i, err := strconv.ParseInt(string(s), 10, 8)
|
||||
if err != nil {
|
||||
return false, errors.ErrNationalIDInvalid
|
||||
}
|
||||
valueSlices = append(valueSlices, uint8(i))
|
||||
}
|
||||
|
||||
s := calculateNationalIDNumbers(&valueSlices)
|
||||
|
||||
s %= 11
|
||||
l := valueSlices[len(value)-1]
|
||||
if (s < 2 && s != int(l)) || (s >= 2 && int(l) != 11-s) {
|
||||
return false, errors.ErrNationalIDInvalid
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func calculateNationalIDNumbers(valueSlices *[]uint8) (sum int) {
|
||||
var i uint8
|
||||
for i = 10; i >= 2; i-- {
|
||||
sum += int(i * (*valueSlices)[10-i])
|
||||
}
|
||||
return sum
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package phone
|
||||
|
||||
import (
|
||||
"backend/pkg/errors"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func IsValid(value string) (bool, error) {
|
||||
re := regexp.MustCompile(`^(?:0|\+98|0098)(\d{10})$`)
|
||||
result := re.FindStringSubmatch(value)
|
||||
if len(result) > 0 {
|
||||
return true, nil
|
||||
}
|
||||
return false, errors.ErrPhoneInvalid
|
||||
}
|
||||
Reference in New Issue
Block a user