-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add more validation to service struct #1110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,10 @@ func ValidateService(service *Service) error { | |
} | ||
} | ||
|
||
if err := isServiceKeysUnique(service); err != nil { | ||
errs = append(errs, err) | ||
} | ||
|
||
// validate configuration image | ||
if service.Configuration.Image != "" { | ||
errs = append(errs, errors.New("configuration.image is not allowed")) | ||
|
@@ -82,6 +86,59 @@ func ValidateService(service *Service) error { | |
return errs.ErrorOrNil() | ||
} | ||
|
||
// isServiceKeysUnique checks uniqueness of service deps/tasks/events/params keys. | ||
func isServiceKeysUnique(service *Service) error { | ||
var errs xerrors.Errors | ||
exist := make(map[string]bool) | ||
for _, dep := range service.Dependencies { | ||
if exist[dep.Key] { | ||
errs = append(errs, fmt.Errorf("dependencies[%s] already exist", dep.Key)) | ||
} | ||
exist[dep.Key] = true | ||
} | ||
|
||
exist = make(map[string]bool) | ||
for _, task := range service.Tasks { | ||
if exist[task.Key] { | ||
errs = append(errs, fmt.Errorf("tasks[%s] already exist", task.Key)) | ||
} | ||
exist[task.Key] = true | ||
|
||
existparam := make(map[string]bool) | ||
for _, param := range task.Inputs { | ||
if existparam[param.Key] { | ||
errs = append(errs, fmt.Errorf("tasks[%s].inputs[%s] already exist", task.Key, param.Key)) | ||
} | ||
existparam[param.Key] = true | ||
} | ||
|
||
existparam = make(map[string]bool) | ||
for _, param := range task.Outputs { | ||
if existparam[param.Key] { | ||
errs = append(errs, fmt.Errorf("tasks[%s].outputs[%s] already exist", task.Key, param.Key)) | ||
} | ||
existparam[param.Key] = true | ||
} | ||
} | ||
|
||
exist = make(map[string]bool) | ||
for _, event := range service.Events { | ||
if exist[event.Key] { | ||
errs = append(errs, fmt.Errorf("events[%s] already exist", event.Key)) | ||
} | ||
exist[event.Key] = true | ||
|
||
existparam := make(map[string]bool) | ||
for _, param := range event.Data { | ||
if existparam[param.Key] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. params.Object keys are not tested (task inputs, task outputs and event)
otherwise a function that validate keys of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise a new custom validation like |
||
errs = append(errs, fmt.Errorf("events[%s].data[%s] already exist", event.Key, param.Key)) | ||
} | ||
existparam[param.Key] = true | ||
} | ||
} | ||
return errs.ErrorOrNil() | ||
} | ||
|
||
func newValidator() (*validator.Validate, ut.Translator) { | ||
en := en.New() | ||
uni := ut.New(en, en) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this one is unique?
does it actually works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it won't be used here, but shouldn't exist