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

Allow categories up to 64 chars, disallow empty categories #1300

Merged
merged 2 commits into from
Jan 6, 2025
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
4 changes: 3 additions & 1 deletion flows/definition/migrations/testdata/migrations/13.6.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
}
},
{
"description": "switch router with no result name and missing category name",
"description": "switch router with no result name",
"original": {
"uuid": "25a2d8b2-ae7c-4fed-964a-506fb8c3f0c0",
"name": "Test Flow",
Expand All @@ -223,6 +223,7 @@
"categories": [
{
"uuid": "37d8813f-1402-4ad2-9cc2-e9054a96525b",
"name": "Blue",
"exit_uuid": "fc2fcd23-7c4a-44bd-a8c6-6c88e6ed09f8"
},
{
Expand Down Expand Up @@ -269,6 +270,7 @@
"categories": [
{
"uuid": "37d8813f-1402-4ad2-9cc2-e9054a96525b",
"name": "Blue",
"exit_uuid": "fc2fcd23-7c4a-44bd-a8c6-6c88e6ed09f8"
},
{
Expand Down
24 changes: 6 additions & 18 deletions flows/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package flows
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
"time"
Expand All @@ -16,24 +15,13 @@ import (
)

func init() {
resultNameRegex := regexp.MustCompile(`^.{1,64}$`)
resultCategoryRegex := regexp.MustCompile(`^.{1,36}$`)

utils.RegisterValidatorTag("result_name",
func(fl validator.FieldLevel) bool {
return resultNameRegex.MatchString(fl.Field().String())
},
func(validator.FieldError) string {
return "is not a valid result name"
},
utils.RegisterValidatorAlias("result_name", "min=1,max=64",
func(validator.FieldError) string { return "is not a valid result name" },
)
utils.RegisterValidatorTag("result_category",
func(fl validator.FieldLevel) bool {
return resultCategoryRegex.MatchString(fl.Field().String())
},
func(validator.FieldError) string {
return "is not a valid result category"
},

// editor enforces max length of 36 for user defined categories but routers like split by group can set the category to a longer value like a group name
utils.RegisterValidatorAlias("result_category", "min=1,max=64",
func(validator.FieldError) string { return "is not a valid result category" },
)
}

Expand Down
2 changes: 1 addition & 1 deletion flows/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestResultNameAndCategoryValidation(t *testing.T) {
ValidName: "Color",
InvalidName: "1234567890123456789012345678901234567890123456789012345678901234567890",
ValidCategory: "Blue",
InvalidCategory: "1234567890123456789012345678901234567",
InvalidCategory: "1234567890123456789012345678901234567890123456789012345678901234567890",
}
err := utils.Validate(obj)
assert.EqualError(t, err, "field 'invalid_name' is not a valid result name, field 'invalid_category' is not a valid result category")
Expand Down
2 changes: 1 addition & 1 deletion flows/routers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ type baseRouterEnvelope struct {
Type string `json:"type" validate:"required"`
Wait json.RawMessage `json:"wait,omitempty"`
ResultName string `json:"result_name,omitempty" validate:"omitempty,result_name"`
Categories []json.RawMessage `json:"categories,omitempty" validate:"required,min=1,dive,result_category"`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong.. and not sure how it's currently working since each category at this point is just json.RawMessage

Categories []json.RawMessage `json:"categories,omitempty" validate:"required,min=1"`
}

// ReadRouter reads a router from the given JSON
Expand Down
2 changes: 1 addition & 1 deletion flows/routers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ flows.Category = (*Category)(nil)

type categoryEnvelope struct {
UUID flows.CategoryUUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name,omitempty" validate:"max=36"`
Name string `json:"name,omitempty" validate:"required,result_category"`
ExitUUID flows.ExitUUID `json:"exit_uuid,omitempty" validate:"required,uuid4"`
}

Expand Down
2 changes: 2 additions & 0 deletions test/testdata/runner/extra.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@
"categories": [
{
"uuid": "2d481ce6-efcf-4898-a825-f76208e32f2a",
"name": "Has Text",
"exit_uuid": "e63af3a0-4c7c-469e-8c5a-01cc38ab872d"
},
{
"uuid": "0680b01f-ba0b-48f4-a688-d2f963130126",
"name": "No Text",
"exit_uuid": "7f156979-1f47-49cd-98b9-e5e6fe0e3baf"
}
],
Expand Down
5 changes: 3 additions & 2 deletions test/testdata/runner/extra.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
"type": "msg_received"
},
{
"category": "",
"category": "Has Text",
"created_on": "2018-07-06T12:30:38.123456789Z",
"name": "Continue",
"step_uuid": "970b8069-50f5-4f6f-8f41-6b2d9f33d623",
Expand Down Expand Up @@ -569,7 +569,7 @@
"type": "msg_received"
},
{
"category": "",
"category": "Has Text",
"created_on": "2018-07-06T12:30:38.123456789Z",
"name": "Continue",
"step_uuid": "970b8069-50f5-4f6f-8f41-6b2d9f33d623",
Expand Down Expand Up @@ -632,6 +632,7 @@
],
"results": {
"continue": {
"category": "Has Text",
"created_on": "2018-07-06T12:30:36.123456789Z",
"input": "Ryan Lewis",
"name": "Continue",
Expand Down
Loading