Skip to content

Commit

Permalink
Consolidate validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Dec 15, 2023
1 parent a95824c commit 0c84ddb
Showing 1 changed file with 12 additions and 35 deletions.
47 changes: 12 additions & 35 deletions lambda/create/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
)

var countryCodeRe = regexp.MustCompile("^[A-Z]{2}$")

func Validate(lpa shared.LpaInit) []shared.FieldError {
activeAttorneyCount, replacementAttorneyCount := countAttorneys(lpa.Attorneys)

Expand Down Expand Up @@ -77,14 +79,6 @@ func flatten(fieldErrors ...[]shared.FieldError) []shared.FieldError {
return errors
}

func validateIf(ok bool, e []shared.FieldError) []shared.FieldError {
if ok {
return e
}

return nil
}

func validateIfElse(ok bool, eIf []shared.FieldError, eElse []shared.FieldError) []shared.FieldError {
if ok {
return eIf
Expand All @@ -93,20 +87,16 @@ func validateIfElse(ok bool, eIf []shared.FieldError, eElse []shared.FieldError)
return eElse
}

func required(source string, value string) []shared.FieldError {
if value == "" {
return []shared.FieldError{{Source: source, Detail: "field is required"}}
}
func validateIf(ok bool, e []shared.FieldError) []shared.FieldError {
return validateIfElse(ok, e, nil)
}

return nil
func required(source string, value string) []shared.FieldError {
return validateIf(value == "", []shared.FieldError{{Source: source, Detail: "field is required"}})
}

func empty(source string, value string) []shared.FieldError {
if value != "" {
return []shared.FieldError{{Source: source, Detail: "field must not be provided"}}
}

return nil
return validateIf(value != "", []shared.FieldError{{Source: source, Detail: "field must not be provided"}})
}

func validateDate(source string, date shared.Date) []shared.FieldError {
Expand All @@ -122,25 +112,16 @@ func validateDate(source string, date shared.Date) []shared.FieldError {
}

func validateTime(source string, t time.Time) []shared.FieldError {
if t.IsZero() {
return []shared.FieldError{{Source: source, Detail: "field is required"}}
}

return nil
return validateIf(t.IsZero(), []shared.FieldError{{Source: source, Detail: "field is required"}})
}

func validateAddress(prefix string, address shared.Address) []shared.FieldError {
errors := flatten(
return flatten(
required(fmt.Sprintf("%s/line1", prefix), address.Line1),
required(fmt.Sprintf("%s/town", prefix), address.Town),
required(fmt.Sprintf("%s/country", prefix), address.Country),
validateIf(!countryCodeRe.MatchString(address.Country), []shared.FieldError{{Source: fmt.Sprintf("%s/country", prefix), Detail: "must be a valid ISO-3166-1 country code"}}),
)

if ok, _ := regexp.MatchString("^[A-Z]{2}$", address.Country); !ok {
errors = append(errors, shared.FieldError{Source: fmt.Sprintf("%s/country", prefix), Detail: "must be a valid ISO-3166-1 country code"})
}

return errors
}

type isValid interface {
Expand All @@ -161,11 +142,7 @@ func validateIsValid[V isValid](source string, v V) []shared.FieldError {
}

func validateUnset(source string, v interface{ Unset() bool }) []shared.FieldError {
if !v.Unset() {
return []shared.FieldError{{Source: source, Detail: "field must not be provided"}}
}

return nil
return validateIf(!v.Unset(), []shared.FieldError{{Source: source, Detail: "field must not be provided"}})
}

func validateAttorneys(prefix string, attorneys []shared.Attorney) []shared.FieldError {
Expand Down

0 comments on commit 0c84ddb

Please sign in to comment.