parent
2894efaf46
commit
708eed5817
6 changed files with 62 additions and 14 deletions
|
@ -42,11 +42,15 @@ func (c *CharacteristicBase) validate() types.ValidationError {
|
||||||
cv := make(types.ValidationError, 0)
|
cv := make(types.ValidationError, 0)
|
||||||
|
|
||||||
if c.CharacteristicName == "" {
|
if c.CharacteristicName == "" {
|
||||||
cv["Name"] = []string{helpers.MustProvideAValue}
|
cv = append(cv, types.NewValidationError(
|
||||||
|
"characteristicName",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.CharacteristicTypeID == 0 {
|
if c.CharacteristicTypeID == 0 {
|
||||||
cv["Characteristic Type"] = []string{helpers.MustProvideAValue}
|
cv = append(cv, types.NewValidationError(
|
||||||
|
"characteristicType",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cv) > 0 {
|
if len(cv) > 0 {
|
||||||
|
|
|
@ -43,11 +43,21 @@ func (m *MeasurementBase) validate() types.ValidationError {
|
||||||
mv := make(types.ValidationError, 0)
|
mv := make(types.ValidationError, 0)
|
||||||
|
|
||||||
if m.StrainID == 0 {
|
if m.StrainID == 0 {
|
||||||
mv["Strain"] = []string{helpers.MustProvideAValue}
|
mv = append(mv, types.NewValidationError(
|
||||||
|
"strain",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.CharacteristicID == 0 {
|
if m.CharacteristicID == 0 {
|
||||||
mv["Characteristic"] = []string{helpers.MustProvideAValue}
|
mv = append(mv, types.NewValidationError(
|
||||||
|
"characteristic",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.TextMeasurementTypeID.Valid == false && m.TxtValue.Valid == false && m.NumValue.Valid == false {
|
||||||
|
mv = append(mv, types.NewValidationError(
|
||||||
|
"value",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(mv) > 0 {
|
if len(mv) > 0 {
|
||||||
|
@ -119,7 +129,9 @@ func (m *Measurement) UnmarshalJSON(b []byte) error {
|
||||||
id, err := GetTextMeasurementTypeID(v)
|
id, err := GetTextMeasurementTypeID(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
measurement.TxtValue = types.NullString{sql.NullString{String: v, Valid: true}}
|
if v != "" {
|
||||||
|
measurement.TxtValue = types.NullString{sql.NullString{String: v, Valid: true}}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,11 +43,15 @@ func (s *SpeciesBase) validate() types.ValidationError {
|
||||||
sv := make(types.ValidationError, 0)
|
sv := make(types.ValidationError, 0)
|
||||||
|
|
||||||
if s.GenusID == 0 {
|
if s.GenusID == 0 {
|
||||||
sv["Genus"] = []string{helpers.MustProvideAValue}
|
sv = append(sv, types.NewValidationError(
|
||||||
|
"genus",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.SpeciesName == "" {
|
if s.SpeciesName == "" {
|
||||||
sv["Species"] = []string{helpers.MustProvideAValue}
|
sv = append(sv, types.NewValidationError(
|
||||||
|
"speciesName",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(sv) > 0 {
|
if len(sv) > 0 {
|
||||||
|
|
|
@ -43,11 +43,15 @@ func (s *StrainBase) validate() types.ValidationError {
|
||||||
sv := make(types.ValidationError, 0)
|
sv := make(types.ValidationError, 0)
|
||||||
|
|
||||||
if s.SpeciesID == 0 {
|
if s.SpeciesID == 0 {
|
||||||
sv["Species"] = []string{helpers.MustProvideAValue}
|
sv = append(sv, types.NewValidationError(
|
||||||
|
"species",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.StrainName == "" {
|
if s.StrainName == "" {
|
||||||
sv["Name"] = []string{helpers.MustProvideAValue}
|
sv = append(sv, types.NewValidationError(
|
||||||
|
"strainName",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(sv) > 0 {
|
if len(sv) > 0 {
|
||||||
|
|
|
@ -43,20 +43,28 @@ func (u *UserBase) validate() types.ValidationError {
|
||||||
uv := make(types.ValidationError, 0)
|
uv := make(types.ValidationError, 0)
|
||||||
|
|
||||||
if u.Name == "" {
|
if u.Name == "" {
|
||||||
uv["Name"] = []string{helpers.MustProvideAValue}
|
uv = append(uv, types.NewValidationError(
|
||||||
|
"name",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.Email == "" {
|
if u.Email == "" {
|
||||||
uv["Email"] = []string{helpers.MustProvideAValue}
|
uv = append(uv, types.NewValidationError(
|
||||||
|
"email",
|
||||||
|
helpers.MustProvideAValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
regex, _ := regexp.Compile(`(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})`)
|
regex, _ := regexp.Compile(`(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})`)
|
||||||
if u.Email != "" && !regex.MatchString(u.Email) {
|
if u.Email != "" && !regex.MatchString(u.Email) {
|
||||||
uv["Email"] = []string{"Must provide a valid email address"}
|
uv = append(uv, types.NewValidationError(
|
||||||
|
"email",
|
||||||
|
"Must provide a valid email address"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(u.Password) < 8 {
|
if len(u.Password) < 8 {
|
||||||
uv["Password"] = []string{"Password must be at least 8 characters"}
|
uv = append(uv, types.NewValidationError(
|
||||||
|
"password",
|
||||||
|
"Password must be at least 8 characters"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(uv) > 0 {
|
if len(uv) > 0 {
|
||||||
|
|
|
@ -2,7 +2,23 @@ package types
|
||||||
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
type ValidationError map[string][]string
|
type Source struct {
|
||||||
|
Pointer string `json:"pointer"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ErrorDetail struct {
|
||||||
|
Source `json:"source"`
|
||||||
|
Detail string `json:"detail"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewValidationError(attr, message string) ErrorDetail {
|
||||||
|
return ErrorDetail{
|
||||||
|
Source: Source{Pointer: "data/attributes/" + attr},
|
||||||
|
Detail: message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ValidationError []ErrorDetail
|
||||||
|
|
||||||
func (v ValidationError) Error() string {
|
func (v ValidationError) Error() string {
|
||||||
errs, err := json.Marshal(struct {
|
errs, err := json.Marshal(struct {
|
||||||
|
|
Reference in a new issue