Refactoring validation to belong to model
This commit is contained in:
@@ -19,3 +19,22 @@ type Note struct {
|
||||
// Content is the actual text content of the note.
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func (n *Note) Validate() error {
|
||||
errs := []error{}
|
||||
if n.Title == "" {
|
||||
errs = append(errs, &ErrEmptyNoteTitle{})
|
||||
}
|
||||
if n.Content == "" {
|
||||
errs = append(errs, &ErrEmptyNoteContent{})
|
||||
}
|
||||
if len(errs) != 0 {
|
||||
return &ErrNoteIncomplete{why: errs}
|
||||
}
|
||||
if titleLen := len(n.Title); titleLen > NoteTitleMaxLength {
|
||||
return &ErrNoteTitleOverflow{
|
||||
length: titleLen,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user