-
Notifications
You must be signed in to change notification settings - Fork 1
/
fields.go
41 lines (34 loc) · 1.2 KB
/
fields.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
package wsHelpers
import (
"fmt"
"github.com/pixlise/core/v4/core/errorwithstatus"
)
const IdFieldMaxLength = 16
const Auth0UserIdFieldMaxLength = 32
const DescriptionFieldMaxLength = 512
const SourceCodeMaxLength = 1024 * 1024 * 5 // Trying to be very generous here, but maybe this is not enough?
const TagListMaxLength = 100
func CheckStringField(field *string, fieldName string, minLength int, maxLength int) error {
if field != nil {
if len(*field) < minLength {
return errorwithstatus.MakeBadRequestError(fmt.Errorf(`%v is too short`, fieldName))
}
if len(*field) > maxLength {
return errorwithstatus.MakeBadRequestError(fmt.Errorf(`%v is too long`, fieldName))
}
}
return nil
}
func CheckFieldLength[T any](field []T, fieldName string, minLength int, maxLength int) error {
if field != nil {
if len(field) < minLength {
return errorwithstatus.MakeBadRequestError(fmt.Errorf(`%v is too short`, fieldName))
}
if len(field) > maxLength {
return errorwithstatus.MakeBadRequestError(fmt.Errorf(`%v is too long`, fieldName))
}
} else if minLength > 0 {
return errorwithstatus.MakeBadRequestError(fmt.Errorf(`%v must contain at least %v items`, fieldName, minLength))
}
return nil
}