-
Notifications
You must be signed in to change notification settings - Fork 43
/
validate.go
42 lines (34 loc) · 1.09 KB
/
validate.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
package api
import (
"bytes"
"io/ioutil"
"github.com/baetyl/baetyl-go/v2/json"
"github.com/baetyl/baetyl-cloud/v2/common"
)
// ValidateResourceForCreating validate when resource create
func (api *API) ValidateResourceForCreating(c *common.Context) (interface{}, error) {
resource := struct {
Name string `json:"name,omitempty"`
}{}
buf, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
return nil, err
}
c.Request.Body = ioutil.NopCloser(bytes.NewReader(buf[:]))
err = json.Unmarshal(buf, &resource)
if err != nil {
return nil, common.Error(common.ErrRequestParamInvalid, common.Field("error", err.Error()))
}
if !common.ValidNonBaetyl(resource.Name) {
return nil, common.Error(common.ErrInvalidName, common.Field("nonBaetyl", "Name"))
}
return nil, nil
}
// ValidateResourceForDeleting validate when resource delete
func (api *API) ValidateResourceForDeleting(c *common.Context) (interface{}, error) {
name := c.GetNameFromParam()
if !common.ValidNonBaetyl(name) {
return nil, common.Error(common.ErrInvalidName, common.Field("nonBaetyl", "Name"))
}
return nil, nil
}