Refactoring validation to belong to model

This commit is contained in:
2026-03-19 00:44:37 +00:00
parent a33cedf09e
commit de6c9d6ae5
6 changed files with 224 additions and 57 deletions

View File

@@ -0,0 +1,42 @@
package models
import (
"fmt"
"strings"
)
type ErrEmptyNoteContent struct{}
type ErrEmptyNoteTitle struct{}
type ErrNoteIncomplete struct {
why []error
}
type ErrNoteTitleOverflow struct {
length int
}
func (e *ErrEmptyNoteContent) Error() string {
return "content cannot be empty"
}
func (e *ErrEmptyNoteTitle) Error() string {
return "title cannot be empty"
}
func (e *ErrNoteIncomplete) Error() string {
if len(e.why) == 0 {
panic("ok so if we use this we need to know why it is incomplete")
}
s := make([]string, len(e.why))
for i, err := range e.why {
s[i] = err.Error()
}
return "note incomplete: " + strings.Join(s, " & ")
}
func (e *ErrNoteIncomplete) Unwrap() []error {
return e.why
}
func (e *ErrNoteTitleOverflow) Error() string {
return fmt.Sprintf("title max length %d, %d provided", NoteTitleMaxLength, e.length)
}