Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complie error: ut.MyUserType.Validate undefined #161

Closed
KobayashiTakaki opened this issue Feb 21, 2022 · 1 comment
Closed

Complie error: ut.MyUserType.Validate undefined #161

KobayashiTakaki opened this issue Feb 21, 2022 · 1 comment

Comments

@KobayashiTakaki
Copy link

Hi,

There is a situation that the generated codes result compile error.
The error goes like: ut.MyUserType.Validate undefined.

Here is my design code.

package design

import (
	. "github.com/shogo82148/goa-v1/design"
	. "github.com/shogo82148/goa-v1/design/apidsl"
)

var Author = Type("Author", func() {
	Attribute("name", String)
	Attribute("country", String)
	Required("name")
})

var Book = Type("Book", func() {
	Attribute("title", String)
	Attribute("author", Author)
})

The Author type has string "name" attribute and string "country" attribute.
The Required validation is set to the Author's "name" attribute.

The Book type has string "title" attribute and refers Author type as "author" attribute.

When I run codegen with this code, I get that compile error.

After investigation, I found two problems.

  1. User type validation should not execute Validate of other types when the other type has only Required validation on primitive type attributes.
  2. Validate method for public type should not be generated when the type has only Required validation on primitive type attributes.

Details are following.

Problem 1: Improper execution of other types' Validate

When the type has only Required validation on primitive type attributes, the type will not have validation code (in design of goa, as mentioned in Validator's recurseAttribute method).

So, the type referring such type (which has only Required validation for primitives) as an attribute should not call Validate method on that type.

However, the code generation procedure produces codes which call Validate on the other types regardless it's validation (having Validate method or not).

I think this problem should be fixed by changing here

if hasValidations {

to this.

		if hasValidations && catt.Validation != nil && !catt.Validation.HasRequiredOnly() {

This if statement is for decision whether generate codes calling Validate on the attribute or not.
This should consider situations that the referring type does not have Validate method.

Problem 2: Unnecessary generation of Validate

By changing my design code as like this (two String type attributes for Required),

var Author = Type("Author", func() {
	Attribute("name", String)
	Attribute("country", String)
	Required("name", "country")
})

codegen generates empty Validate method (just return) for this type.

// Validate validates the Author type instance.
func (ut *Author) Validate() (err error) {

	return
}

I think this is a bug of the code generation, because required String type will be primitive string, and validations for Required of primitive types should not be generated.

This problem can be fixed by changing here

to this.

			if i > 0 && val != "" {

This if statement is for adding line breaks between required validation codes.
Current condition (just if i > 0) will append "\n" even if the required attribute is primitive type (which will not produce validation code).
I think adding line breaks in the for loop should be done when the current code is not empty.

Codes

Here is my codes for testing codegen behavior.
https://github.com/KobayashiTakaki/goa-test

Thanks😌

shogo82148 added a commit that referenced this issue Feb 21, 2022
@shogo82148
Copy link
Owner

fixed by #160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants