| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package binding |
| 17 |
|
| 18 |
import ( |
| 19 |
"mime/multipart" |
| 20 |
"net/http" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
type ( |
| 27 |
|
| 28 |
Post struct { |
| 29 |
Title string `form:"title" json:"title" binding:"Required"` |
| 30 |
Content string `form:"content" json:"content"` |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
Person struct { |
| 35 |
Name string `form:"name" json:"name" binding:"Required"` |
| 36 |
Email string `form:"email" json:"email"` |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
BlogPost struct { |
| 45 |
Post |
| 46 |
Id int `binding:"Required"` |
| 47 |
Ignored string `form:"-" json:"-"` |
| 48 |
Ratings []int `form:"rating" json:"ratings"` |
| 49 |
Author Person `json:"author"` |
| 50 |
Coauthor *Person `json:"coauthor"` |
| 51 |
HeaderImage *multipart.FileHeader |
| 52 |
Pictures []*multipart.FileHeader `form:"picture"` |
| 53 |
unexported string `form:"unexported"` |
| 54 |
} |
| 55 |
|
| 56 |
EmbedPerson struct { |
| 57 |
*Person |
| 58 |
} |
| 59 |
|
| 60 |
SadForm struct { |
| 61 |
AlphaDash string `form:"AlphaDash" binding:"AlphaDash"` |
| 62 |
AlphaDashDot string `form:"AlphaDashDot" binding:"AlphaDashDot"` |
| 63 |
Size string `form:"Size" binding:"Size(1)"` |
| 64 |
SizeSlice []string `form:"SizeSlice" binding:"Size(1)"` |
| 65 |
MinSize string `form:"MinSize" binding:"MinSize(5)"` |
| 66 |
MinSizeSlice []string `form:"MinSizeSlice" binding:"MinSize(5)"` |
| 67 |
MaxSize string `form:"MaxSize" binding:"MaxSize(1)"` |
| 68 |
MaxSizeSlice []string `form:"MaxSizeSlice" binding:"MaxSize(1)"` |
| 69 |
Range int `form:"Range" binding:"Range(1,2)"` |
| 70 |
RangeInvalid int `form:"RangeInvalid" binding:"Range(1)"` |
| 71 |
Email string `binding:"Email"` |
| 72 |
URL string `form:"Url" binding:"Url"` |
| 73 |
URLEmpty string `form:"UrlEmpty" binding:"Url"` |
| 74 |
In string `form:"In" binding:"Default(0);In(1,2,3)"` |
| 75 |
InInvalid string `form:"InInvalid" binding:"In(1,2,3)"` |
| 76 |
NotIn string `form:"NotIn" binding:"NotIn(1,2,3)"` |
| 77 |
Include string `form:"Include" binding:"Include(a)"` |
| 78 |
Exclude string `form:"Exclude" binding:"Exclude(a)"` |
| 79 |
Empty string |
| 80 |
} |
| 81 |
|
| 82 |
Group struct { |
| 83 |
Name string `json:"name" binding:"Required"` |
| 84 |
People []Person `json:"people" binding:"MinSize(1)"` |
| 85 |
} |
| 86 |
|
| 87 |
PointerForm struct { |
| 88 |
ID int `form:"Id" binding:"Id"` |
| 89 |
IDPointer *int `form:"IdPointer" binding:"IdPointer"` |
| 90 |
URL string `form:"Url" binding:"Url"` |
| 91 |
URLPointer *string `form:"UrlPointer" binding:"Url"` |
| 92 |
AlphaDash string `form:"AlphaDash" binding:"AlphaDash"` |
| 93 |
AlphaDashPointer *string `form:"AlphaDashPointer" binding:"AlphaDash"` |
| 94 |
} |
| 95 |
|
| 96 |
CustomErrorHandle struct { |
| 97 |
Rule `binding:"CustomRule"` |
| 98 |
} |
| 99 |
|
| 100 |
TrimSpaceForm struct { |
| 101 |
Title string `form:"title" preprocess:"TrimSpace"` |
| 102 |
Description string `form:"description"` |
| 103 |
Slug string `form:"slug" preprocess:"TrimSpace" binding:"Required;AlphaDash"` |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
handlerFunc func(req *http.Request, obj any) Errors |
| 108 |
) |
| 109 |
|
| 110 |
func (p Post) Validate(_ *http.Request, errs Errors) Errors { |
| 111 |
if len(p.Title) < 10 { |
| 112 |
errs = append(errs, Error{ |
| 113 |
FieldNames: []string{"title"}, |
| 114 |
Classification: "LengthError", |
| 115 |
Message: "Life is too short", |
| 116 |
}) |
| 117 |
} |
| 118 |
return errs |
| 119 |
} |
| 120 |
|
| 121 |
const ( |
| 122 |
testRoute = "/test" |
| 123 |
formContentType = "application/x-www-form-urlencoded" |
| 124 |
) |