Skip to content

Commit

Permalink
Fix issue with union validations (#3164)
Browse files Browse the repository at this point in the history
Refactor validation code to remove duplication of logic.
  • Loading branch information
raphael authored Oct 11, 2022
1 parent 771ee77 commit 019ddb5
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 133 deletions.
4 changes: 2 additions & 2 deletions codegen/service/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ func buildValidations(projected *expr.AttributeExpr, scope *codegen.NameScope) [
})
ctx = projectedTypeContext("", !expr.IsPrimitive(projected.Type), scope)
}
data["Validate"] = codegen.ValidationCode(&expr.AttributeExpr{Type: o, Validation: rt.Validation}, ctx, true, false, "result")
data["Validate"] = codegen.ValidationCode(&expr.AttributeExpr{Type: o, Validation: rt.Validation}, rt, ctx, true, false, "result")
data["Fields"] = fields
}

Expand All @@ -1687,7 +1687,7 @@ func buildValidations(projected *expr.AttributeExpr, scope *codegen.NameScope) [
Name: name,
Description: fmt.Sprintf("%s runs the validations defined on %s.", name, tname),
Ref: scope.GoTypeRef(projected),
Validate: codegen.ValidationCode(ut.Attribute(), ctx, true, expr.IsAlias(ut), "result"),
Validate: codegen.ValidationCode(ut.Attribute(), ut, ctx, true, expr.IsAlias(ut), "result"),
})
}
return validations
Expand Down
68 changes: 60 additions & 8 deletions codegen/testdata/validation_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,29 +235,29 @@ const (

AliasTypeValidationCode = `func Validate() (err error) {
if target.RequiredAlias != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("target", string(*target.RequiredAlias), "^[A-z].*[a-z]$"))
err = goa.MergeErrors(err, goa.ValidatePattern("target.required_alias", string(*target.RequiredAlias), "^[A-z].*[a-z]$"))
}
if target.RequiredAlias != nil {
if utf8.RuneCountInString(string(*target.RequiredAlias)) < 1 {
err = goa.MergeErrors(err, goa.InvalidLengthError("target", string(*target.RequiredAlias), utf8.RuneCountInString(string(*target.RequiredAlias)), 1, true))
err = goa.MergeErrors(err, goa.InvalidLengthError("target.required_alias", string(*target.RequiredAlias), utf8.RuneCountInString(string(*target.RequiredAlias)), 1, true))
}
}
if target.RequiredAlias != nil {
if utf8.RuneCountInString(string(*target.RequiredAlias)) > 10 {
err = goa.MergeErrors(err, goa.InvalidLengthError("target", string(*target.RequiredAlias), utf8.RuneCountInString(string(*target.RequiredAlias)), 10, false))
err = goa.MergeErrors(err, goa.InvalidLengthError("target.required_alias", string(*target.RequiredAlias), utf8.RuneCountInString(string(*target.RequiredAlias)), 10, false))
}
}
if target.Alias != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("target", string(*target.Alias), "^[A-z].*[a-z]$"))
err = goa.MergeErrors(err, goa.ValidatePattern("target.alias", string(*target.Alias), "^[A-z].*[a-z]$"))
}
if target.Alias != nil {
if utf8.RuneCountInString(string(*target.Alias)) < 1 {
err = goa.MergeErrors(err, goa.InvalidLengthError("target", string(*target.Alias), utf8.RuneCountInString(string(*target.Alias)), 1, true))
err = goa.MergeErrors(err, goa.InvalidLengthError("target.alias", string(*target.Alias), utf8.RuneCountInString(string(*target.Alias)), 1, true))
}
}
if target.Alias != nil {
if utf8.RuneCountInString(string(*target.Alias)) > 10 {
err = goa.MergeErrors(err, goa.InvalidLengthError("target", string(*target.Alias), utf8.RuneCountInString(string(*target.Alias)), 10, false))
err = goa.MergeErrors(err, goa.InvalidLengthError("target.alias", string(*target.Alias), utf8.RuneCountInString(string(*target.Alias)), 10, false))
}
}
}
Expand Down Expand Up @@ -448,6 +448,56 @@ const (
}
}
}
`
UnionValidationCode = `func Validate() (err error) {
if target.RequiredUnion == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("required_union", "target"))
}
switch v := target.RequiredUnion.(type) {
case *Union_Int:
if v.Int != nil {
if err2 := ValidateInteger(v.Int); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
case *Union_Float:
if v.Float != nil {
if err2 := ValidateFloat(v.Float); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
case *Union_String:
if v.String != nil {
if err2 := ValidateString(v.String); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
switch v := target.Union.(type) {
case *Union_Int:
if v.Int != nil {
if err2 := ValidateInteger(v.Int); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
case *Union_Float:
if v.Float != nil {
if err2 := ValidateFloat(v.Float); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
case *Union_String:
if v.String != nil {
if err2 := ValidateString(v.String); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
}
`

ResultTypePointerValidationCode = `func Validate() (err error) {
Expand All @@ -471,8 +521,10 @@ const (
`

TypeWithCollectionPointerValidationCode = `func Validate() (err error) {
if err2 := ValidateResultCollection(target.Collection); err2 != nil {
err = goa.MergeErrors(err, err2)
if target.Collection != nil {
if err2 := ValidateResultCollection(target.Collection); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
`
Expand Down
14 changes: 14 additions & 0 deletions codegen/testdata/validation_types_dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ var ValidationTypesDSL = func() {
Required("required_map")
})

_ = Type("Union", func() {
OneOf("required_union", func() {
Attribute("int", IntegerT)
Attribute("float", FloatT)
Attribute("string", StringT)
})
OneOf("union", func() {
Attribute("int", IntegerT)
Attribute("float", FloatT)
Attribute("string", StringT)
})
Required("required_union")
})

Result = ResultType("application/vnd.goa.result", func() {
TypeName("Result")
Attributes(func() {
Expand Down
Loading

0 comments on commit 019ddb5

Please sign in to comment.