Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 65 additions & 65 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,6 @@ import (
"strconv"
)

/**
Base Constraints for all Data Types
*/

func required(field field, param string) error {
switch field.typ.Kind() {
case reflect.String:
c, err := convertBool(param)
if err != nil {
return err
}
if c == true {
in, _ := field.value.Interface().(string)
if in == "" {
return ErrRequired
}
}
case reflect.Bool:
case reflect.Int:
case reflect.Float32:
case reflect.Uint:
}
return nil
}

func nillable(field field, param string) error {
return nil
}

func def(field field, param string) error {
return nil
}

/**
Numerical Type Constraints
*/
Expand All @@ -62,16 +29,20 @@ func exclusiveMax(field field, param string) error {

func multipleOf(field field, param string) error {
// TODO : works only for int as of now
valid := true
in, _ := field.value.Interface().(int)
c, err := convertInt(param, 0)
cInt := int(c)
if err != nil {
return err
}
valid = in%cInt == 0
if !valid {
return ErrMultipleOf
switch field.typ.Kind() {
case reflect.Int:
in, _ := field.value.Interface().(int)
c, err := convertInt(param, 0)
cInt := int(c)
if err != nil {
return err
}
valid := in%cInt == 0
if !valid {
return ErrMultipleOf
}
default:
return ErrInvalidValidationForField
}
return nil
}
Expand All @@ -80,39 +51,68 @@ func multipleOf(field field, param string) error {
String Type Constraints
*/

func notnull(field field, param string) error {
switch field.typ.Kind() {
case reflect.String:
c, err := convertBool(param)
if err != nil {
return err
}
if c == true {
in, _ := field.value.Interface().(string)
if in == "" {
return ErrNoNull
}
}
default:
return ErrInvalidValidationForField
}
return nil
}

func minLength(field field, param string) error {
valid := true
lc, _ := strconv.Atoi(param)
lv := len(fmt.Sprint(field.value))
valid = lv > lc
if !valid {
return ErrMinLength
switch field.typ.Kind() {
case reflect.String:
lc, _ := strconv.Atoi(param)
lv := len(fmt.Sprint(field.value))
valid := lv > lc
if !valid {
return ErrMinLength
}
default:
return ErrInvalidValidationForField
}
return nil
}

func maxLength(field field, param string) error {
valid := true
lc, _ := strconv.Atoi(param)
lv := len(fmt.Sprint(field.value))
valid = lv < lc
if !valid {
return ErrMaxLength
switch field.typ.Kind() {
case reflect.String:
lc, _ := strconv.Atoi(param)
lv := len(fmt.Sprint(field.value))
valid := lv < lc
if !valid {
return ErrMaxLength
}
default:
return ErrInvalidValidationForField
}
return nil
}

func pattern(field field, param string) error {
in, _ := field.value.Interface().(string)
if field.value.Kind() != reflect.String {
return ErrNotSupported
}
re, err := regexp.Compile(param)
if err != nil {
return ErrBadConstraint
}
if !re.MatchString(in) {
return ErrPattern
switch field.typ.Kind() {
case reflect.String:
in, _ := field.value.Interface().(string)
re, err := regexp.Compile(param)
if err != nil {
return ErrBadConstraint
}
if !re.MatchString(in) {
return ErrPattern
}
default:
return ErrInvalidValidationForField
}
return nil
}
Expand Down
8 changes: 2 additions & 6 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ func (e ErrMsg) Error() string {
}

var (
ErrRequired = ErrMsg{errors.New("required validation failed")}
ErrNoNull = ErrMsg{errors.New("notnull validation failed")}

ErrNillable = ErrMsg{errors.New("nillable validation failed")}

ErrDefault = ErrMsg{errors.New("default validation failed")}
ErrInvalidValidationForField = ErrMsg{errors.New("invalid validation applied to the field")}

ErrMin = ErrMsg{errors.New("min value validation failed")}

Expand All @@ -37,7 +35,5 @@ var (

ErrNotSupported = ErrMsg{errors.New("unsupported constraint on type")}

ErrMandatoryFields = ErrMsg{errors.New("mandatory fields not present")}

ErrEnums = ErrMsg{errors.New("enum validation failed")}
)
4 changes: 4 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func checkMin(val reflect.Value, typ reflect.Type, param string, isExclusive boo
} else {
valid = in > cFloat
}
default:
return ErrInvalidValidationForField
}
if !valid {
if isExclusive {
Expand Down Expand Up @@ -357,6 +359,8 @@ func checkMax(val reflect.Value, typ reflect.Type, param string, isExclusive boo
} else {
valid = in < cFloat
}
default:
return ErrInvalidValidationForField
}
if !valid {
if isExclusive {
Expand Down
30 changes: 2 additions & 28 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (

var logger = l3.Get()

var mapMandatory = map[string]string{
"required": "required",
"nillable": "nillable",
}

type StructValidatorFunc func(field field, param string) error

type tStruct struct {
Expand Down Expand Up @@ -48,13 +43,6 @@ func NewStructValidator() *StructValidator {
return &StructValidator{
validationFunc: map[string]StructValidatorFunc{
// Base Constraints
// boolean value
// mandatory field
"required": required,
// boolean value
// mandatory field
"nillable": nillable,
"default": def,
// Numeric Constraints
// <, > only
"min": min,
Expand All @@ -64,6 +52,8 @@ func NewStructValidator() *StructValidator {
"exclusiveMax": exclusiveMax,
"multipleOf": multipleOf,
// String Constraints
// boolean value
"notnull": notnull,
"min-length": minLength,
"max-length": maxLength,
// regex pattern support
Expand Down Expand Up @@ -98,9 +88,6 @@ func (sv *StructValidator) validateFields() error {
logger.InfoF("skipping validation check for field: %s", field.name)
continue
}
if err := checkForMandatory(field.constraints); err != nil {
return err
}
for _, val := range field.constraints {
if err := val.fnc(field, val.value); err != nil {
return err
Expand All @@ -110,19 +97,6 @@ func (sv *StructValidator) validateFields() error {
return nil
}

func checkForMandatory(constraint []tStruct) error {
count := 0
for _, t := range constraint {
if _, ok := mapMandatory[t.name]; ok {
count++
}
}
if count != len(mapMandatory) {
return ErrMandatoryFields
}
return nil
}

// parseTag returns the map of constraints
func (sv *StructValidator) parseTag(tag string) ([]tStruct, error) {
tl := splitUnescapedComma(tag)
Expand Down
Loading