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

Less panics, more validation errors for invalid flow defs #1195

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ var _ flows.Flow = (*flow)(nil)
type flowEnvelope struct {
migrations.Header13

Language i18n.Language `json:"language" validate:"required,language"`
Type flows.FlowType `json:"type" validate:"required,flow_type"`
Language i18n.Language `json:"language" validate:"required,language"`
Type flows.FlowType `json:"type" validate:"required,flow_type"`
Revision int `json:"revision"`
ExpireAfterMinutes int `json:"expire_after_minutes"`
Localization localization `json:"localization"`
Nodes []*node `json:"nodes"`
Nodes []*node `json:"nodes" validate:"dive,required"`
UI json.RawMessage `json:"_ui,omitempty"`
}

Expand Down
12 changes: 12 additions & 0 deletions flows/definition/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func TestBrokenFlows(t *testing.T) {
path string
err string
}{
{
"null_node.json",
"field 'nodes[1]' is required",
},
{
"null_exit.json",
"unable to read node: field 'exits[1]' is required",
},
{
"null_action.json",
"unable to read action: field 'type' is required",
},
{
"exitless_node.json",
"unable to read node: field 'exits' must have a minimum of 1 items",
Expand Down
5 changes: 2 additions & 3 deletions flows/definition/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/nyaruka/goflow/flows/inspect"
"github.com/nyaruka/goflow/flows/routers"
"github.com/nyaruka/goflow/utils"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -143,9 +142,9 @@ func (n *node) EnumerateLocalizables(include func(uuids.UUID, string, []string,

type nodeEnvelope struct {
UUID flows.NodeUUID `json:"uuid" validate:"required,uuid4"`
Actions []json.RawMessage `json:"actions,omitempty"`
Actions []json.RawMessage `json:"actions,omitempty" validate:"dive,required"`
Router json.RawMessage `json:"router,omitempty"`
Exits []*exit `json:"exits" validate:"required,min=1"`
Exits []*exit `json:"exits" validate:"required,min=1,dive,required"`
}

// UnmarshalJSON unmarshals a flow node from the given JSON
Expand Down
31 changes: 31 additions & 0 deletions flows/definition/testdata/broken_flows/null_action.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"flows": [
{
"uuid": "76f0a02f-3b75-4b86-9064-e9195e1b3a02",
"name": "Test Flow",
"spec_version": "13.2.0",
"language": "eng",
"type": "messaging",
"nodes": [
{
"uuid": "a58be63b-907d-4a1a-856b-0bb5579d7507",
"actions": [
{
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"type": "send_msg",
"text": "Hi there @contact.name"
},
null
],
"exits": [
{
"uuid": "37d8813f-1402-4ad2-9cc2-e9054a96525b",
"name": "Default",
"destination_uuid": null
}
]
}
]
}
]
}
24 changes: 24 additions & 0 deletions flows/definition/testdata/broken_flows/null_exit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"flows": [
{
"uuid": "76f0a02f-3b75-4b86-9064-e9195e1b3a02",
"name": "Test Flow",
"spec_version": "13.2.0",
"language": "eng",
"type": "messaging",
"nodes": [
{
"uuid": "a58be63b-907d-4a1a-856b-0bb5579d7507",
"exits": [
{
"uuid": "37d8813f-1402-4ad2-9cc2-e9054a96525b",
"name": "Default",
"destination_uuid": null
},
null
]
}
]
}
]
}
24 changes: 24 additions & 0 deletions flows/definition/testdata/broken_flows/null_node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"flows": [
{
"uuid": "76f0a02f-3b75-4b86-9064-e9195e1b3a02",
"name": "Test Flow",
"spec_version": "13.2.0",
"language": "eng",
"type": "messaging",
"nodes": [
{
"uuid": "a58be63b-907d-4a1a-856b-0bb5579d7507",
"exits": [
{
"uuid": "37d8813f-1402-4ad2-9cc2-e9054a96525b",
"name": "Default",
"destination_uuid": null
}
]
},
null
]
}
]
}