-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoc.go
56 lines (51 loc) · 1.57 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Package valix - Go package for validating requests
/*
Check requests in the form of *http.Request, `map[string]interface{}` or `[]interface{}`
Validators can be created from existing structs, for example:
type AddPersonRequest struct {
Name string `json:"name" v8n:"notNull,mandatory,&StringNoControlCharacters{},&StringLength{Minimum: 1, Maximum: 255}"`
Age int `json:"age" v8n:"type:Integer,notNull,mandatory,&PositiveOrZero{}"`
}
var AddPersonRequestValidator = valix.MustCompileValidatorFor(AddPersonRequest{}, nil)
Or validators can be expressed effectively in code, for example:
var personValidator = &valix.Validator{
IgnoreUnknownProperties: false,
Properties: valix.Properties{
"name": {
Type: valix.Type.JsonString,
NotNull: true,
Mandatory: true,
Constraints: valix.Constraints{
&valix.StringLength{Minimum: 1, Maximum: 255},
},
},
"age": {
Type: valix.Type.Int,
NotNull: true,
Mandatory: true,
Constraints: valix.Constraints{
&valix.PositiveOrZero{},
},
},
},
}
Validators can re-use common property validators, for example re-using the `personValidator` above:
var addPersonToGroupValidator = &valix.Validator{
IgnoreUnknownProperties: false,
Properties: valix.Properties{
"person": {
Type: valix.Type.JsonObject,
ObjectValidator: personValidator,
},
"group": {
Type: valix.Type.JsonString,
NotNull: true,
Mandatory: true,
Constraints: valix.Constraints{
&valix.StringLength{Minimum: 1, Maximum: 255},
},
},
},
}
*/
package valix