65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package entry
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
ErrEntryNotFound = errors.New("entry not found")
|
|
ErrInvalidEntry = errors.New("invalid entry")
|
|
ErrUniqueViolation = errors.New("a unique value is already used")
|
|
ErrInvalidSort = errors.New("invalid sort")
|
|
ErrInvalidPage = errors.New("invalid page")
|
|
)
|
|
|
|
const (
|
|
MaxPage = 1000000
|
|
MaxPageSize = 100
|
|
)
|
|
|
|
func ValidatePage(page int, size int) error {
|
|
if page > MaxPage || size > MaxPageSize {
|
|
return ErrInvalidPage
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const (
|
|
RuleRequired = "required"
|
|
RuleUnknownField = "unknown_field"
|
|
RuleUnique = "unique"
|
|
)
|
|
|
|
type Entry struct {
|
|
ID uint32 `json:"id"`
|
|
ContentTypeID uint32 `json:"content_type_id"`
|
|
Data map[string]any `json:"data"`
|
|
CreatedBy uint32 `json:"created_by"`
|
|
UpdatedBy uint32 `json:"updated_by"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type FieldError struct {
|
|
Key string
|
|
Rule string
|
|
Cause error
|
|
}
|
|
|
|
func (e FieldError) Error() string {
|
|
return e.Key + ": " + e.Rule
|
|
}
|
|
|
|
func (e FieldError) Unwrap() error {
|
|
return e.Cause
|
|
}
|
|
|
|
func invalidField(key string, rule string) error {
|
|
return FieldError{Key: key, Rule: rule, Cause: ErrInvalidEntry}
|
|
}
|
|
|
|
func UniqueViolation(key string) error {
|
|
return FieldError{Key: key, Rule: RuleUnique, Cause: ErrUniqueViolation}
|
|
}
|