devicemngmt/validate.go

79 lines
1.6 KiB
Go
Raw Normal View History

2021-11-05 08:40:07 +00:00
package devicemngmt
import (
"errors"
"github.com/Selly-Modules/logger"
2021-11-22 08:14:36 +00:00
"github.com/Selly-Modules/mongodb"
2021-11-05 08:40:07 +00:00
)
2021-11-05 09:57:33 +00:00
func (co CreateOptions) validate() error {
2021-11-05 08:40:07 +00:00
// DeviceID
if co.DeviceID == "" {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - Create: no deviceID data", logger.LogData{
2021-11-05 08:40:07 +00:00
"payload": co,
})
return errors.New("no deviceID data")
}
// IP
if co.IP == "" {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - Create: no ip data", logger.LogData{
2021-11-05 08:40:07 +00:00
"payload": co,
})
return errors.New("no ip data")
}
// UserID
if co.UserID == "" {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - Create: no userID data", logger.LogData{
2021-11-05 08:40:07 +00:00
"payload": co,
})
return errors.New("no userID data")
}
2021-11-22 08:14:36 +00:00
if _, isValid := mongodb.NewIDFromString(co.UserID); !isValid {
return errors.New("invalid userID data")
}
2021-11-05 08:40:07 +00:00
// AuthToken
if co.AuthToken == "" {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - Create: no authToken data", logger.LogData{
2021-11-05 08:40:07 +00:00
"payload": co,
})
return errors.New("no authToken data")
}
return nil
}
2021-11-08 01:52:10 +00:00
func (uo UpdateOptions) validate() error {
2021-11-26 10:37:15 +00:00
// UserID
if uo.UserID == "" {
logger.Error("devicemngmt - Update: no userID data", logger.LogData{
"payload": uo,
})
return errors.New("no userID data")
}
if _, isValid := mongodb.NewIDFromString(uo.UserID); !isValid {
return errors.New("invalid userID data")
}
2021-11-08 01:52:10 +00:00
// IP
if uo.IP == "" {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - Update: no ip data", logger.LogData{
2021-11-08 01:52:10 +00:00
"payload": uo,
})
return errors.New("no ip data")
}
// AuthToken
if uo.AuthToken == "" {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - Update: no authToken data", logger.LogData{
2021-11-08 01:52:10 +00:00
"payload": uo,
})
return errors.New("no authToken data")
}
return nil
}