func validateFieldLength(value *string, fldPath *field.Path, min, max int, fieldName string) field.ErrorList { allErrs := field.ErrorList{} if v := models.StringValue(value); !checkFieldLength(v, min, max) { allErrs = append(allErrs, field.Invalid(fldPath.Child(fieldName), v, fmt.Sprintf("%s: %s must be between %d and %d characters", fldPath.String(), fieldName, min, max))) } return allErrs }
func validateRequiredFields(new interface{}, fldPath *field.Path, fields []string) field.ErrorList { allErrs := field.ErrorList{} news := structs.New(new) for _, f := range fields { if news.Field(f).IsZero() { allErrs = append(allErrs, field.Required(fldPath.Child(f), fmt.Sprintf("%s updates must supply %s", fldPath.String(), news.Field(f).Name()))) } } return allErrs }
func validateInvalidFields(new interface{}, fldPath *field.Path, fields []string) field.ErrorList { allErrs := field.ErrorList{} news := structs.New(new) for _, f := range fields { // ommitting immutable fields is OK if news.Field(f).IsZero() { continue } allErrs = append(allErrs, field.Forbidden(fldPath.Child(f), fmt.Sprintf("%s updates must not supply %s", fldPath.String(), news.Field(f).Name()))) } return allErrs }
func validateImmutibleFields(new, old interface{}, fldPath *field.Path, fields []string) field.ErrorList { allErrs := field.ErrorList{} olds := structs.New(old) news := structs.New(new) for _, f := range fields { // ommitting immutable fields is OK if news.Field(f).IsZero() { continue } if !reflect.DeepEqual(news.Field(f).Value(), olds.Field(f).Value()) { allErrs = append(allErrs, field.Forbidden(fldPath.Child(f), fmt.Sprintf("%s updates must not change %s", fldPath.String(), news.Field(f).Name()))) } } return allErrs }