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

Validating optional strings in Validate() func #66

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions gen/gomodel/gomodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,23 +755,32 @@ func (gen *modelGenerator) emitStructValidator(st *rdl.StructTypeDef, flattened
for _, f := range flattened {
fname := capitalize(string(f.Name))
ftype := string(f.Type)
if !f.Optional {
bt := gen.registry.FindBaseType(f.Type)
switch bt {
case rdl.BaseTypeString, rdl.BaseTypeSymbol:
bt := gen.registry.FindBaseType(f.Type)
switch bt {
case rdl.BaseTypeString, rdl.BaseTypeSymbol:
if !f.Optional {
gen.emit(fmt.Sprintf("\tif self.%s == \"\" {\n", fname))
gen.emit(fmt.Sprintf("\t\treturn fmt.Errorf(\"%s.%s is missing but is a required field\")\n", st.Name, f.Name))
if FullValidation {
if bt == rdl.BaseTypeString && fname != "String" {
gen.emit(fmt.Sprintf("\t} else {\n\t\tval := %sValidate(%sSchema(), %q, self.%s)\n\t\tif !val.Valid {\n\t\t\treturn fmt.Errorf(\"%s.%s does not contain a valid %s (%%v)\", val.Error)\n\t\t}\n", rdlPrefix, capitalize(string(gen.schema.Name)), ftype, fname, st.Name, string(f.Name), ftype))
}
if FullValidation {
if bt == rdl.BaseTypeString && fname != "String" {
if !f.Optional {
gen.emit("\t} else {\n")
} else {
gen.emit(fmt.Sprintf("\tif self.%s != \"\" {\n", fname))
}
gen.emit(fmt.Sprintf("\t\tval := %sValidate(%sSchema(), %q, self.%s)\n\t\tif !val.Valid {\n\t\t\treturn fmt.Errorf(\"%s.%s does not contain a valid %s (%%v)\", val.Error)\n\t\t}\n", rdlPrefix, capitalize(string(gen.schema.Name)), ftype, fname, st.Name, string(f.Name), ftype))
}
gen.emit("\t}\n")
case rdl.BaseTypeTimestamp:
}
gen.emit("\t}\n")
case rdl.BaseTypeTimestamp:
if !f.Optional {
gen.emit(fmt.Sprintf("\tif self.%s.IsZero() {\n", fname))
gen.emit(fmt.Sprintf("\t\treturn fmt.Errorf(\"%s: Missing required field: %s\")\n", st.Name, f.Name))
gen.emit("\t}\n")
case rdl.BaseTypeArray, rdl.BaseTypeMap, rdl.BaseTypeStruct, rdl.BaseTypeUUID:
}
case rdl.BaseTypeArray, rdl.BaseTypeMap, rdl.BaseTypeStruct, rdl.BaseTypeUUID:
if !f.Optional {
gen.emit(fmt.Sprintf("\tif self.%s == nil {\n", fname))
gen.emit(fmt.Sprintf("\t\treturn fmt.Errorf(\"%s: Missing required field: %s\")\n", st.Name, f.Name))
gen.emit("\t}\n")
Expand Down