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

Doesn't emit validation code for optional fields #65

Open
jpeirson opened this issue Feb 19, 2019 · 1 comment
Open

Doesn't emit validation code for optional fields #65

jpeirson opened this issue Feb 19, 2019 · 1 comment

Comments

@jpeirson
Copy link
Contributor

Take for example this RDL fragment:

type SimpleName String (pattern="[a-zA-Z_][a-zA-Z_0-9]*");
type StringTest Struct {
     SimpleName name;
     SimpleName opt (optional);
}

Running rdl generate go-model for this RDL produces the validation func as:

func (self *StringTest) Validate() error {
	if self.Name == "" {
		return fmt.Errorf("StringTest.name is missing but is a required field")
	} else {
		val := rdl.Validate(TestSchema(), "SimpleName", self.Name)
		if !val.Valid {
			return fmt.Errorf("StringTest.name does not contain a valid SimpleName (%v)", val.Error)
		}
	}
	return nil
}

Although opt is an optional field, it is contrained by a regex pattern that should be validated inside Validate(). I'd expect the validation func to instead be:

func (self *StringTest) Validate() error {
	if self.Name == "" {
		return fmt.Errorf("StringTest.name is missing but is a required field")
	} else {
		val := rdl.Validate(TestSchema(), "SimpleName", self.Name)
		if !val.Valid {
			return fmt.Errorf("StringTest.name does not contain a valid SimpleName (%v)", val.Error)
		}
	}

	// --- additional expected code ---
	if self.Opt != "" {
		val := rdl.Validate(TestSchema(), "SimpleName", self.Opt)
		if !val.Valid {
			return fmt.Errorf("StringTest.opt does not contain a valid SimpleName (%v)", val.Error)
		}
	}
	// ---------------------
	
	return nil
}
jpeirson pushed a commit to jpeirson/ardielle-go that referenced this issue Feb 19, 2019
@jpeirson
Copy link
Contributor Author

jpeirson commented Feb 19, 2019

Created PR #66 that I think fixes this issue without regressions.

boynton pushed a commit that referenced this issue Feb 20, 2019
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

1 participant