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

Set rule from dynamic values #12

Open
guiferpa opened this issue Aug 14, 2020 · 0 comments
Open

Set rule from dynamic values #12

guiferpa opened this issue Aug 14, 2020 · 0 comments
Labels
enhancement New feature or request hacktoberfest
Milestone

Comments

@guiferpa
Copy link
Owner

Nowadays there's only support to static configuration. For example, configuring some rule to Enum validation it's necessary set this Enum value statically.

I had on problem using defined const values in my source code in validation rule, follow the code example below:

...

type Status string

const (
    StatusCreated Status = "__CREATED__"
    StatusPending Status = "__PENDING__"
    StatusDoing   Status = "__DOING__"
    StatusDone    Status = "__DONE__"
)

...

type Task struct {
    Name   string
    Status Status `json:"status" validate:"enum=__CREATED__,__PENDING__,__DOING__,__DONE__"`
}

...

validator := gody.NewValidator()
validator.AddRules(rule.Enum)

task := Task{}
validator.Validate(task)

With the previous example we have a problem of duplicity and my suggestion for the static config problem is likely this source code below:

type Status string

const (
    StatusCreated Status = "__CREATED__"
    StatusPending Status = "__PENDING__"
    StatusDoing   Status = "__DOING__"
    StatusDone    Status = "__DONE__"
)

...

type Task struct {
    Name   string
    Status Status `json:"status" validate:"enum={status}"`
}

...

validator := gody.NewValidator()
validator.AddRuleParameters(...[]rule.Parameter{
    {
        Name: "status", 
        Value: fmt.Sprintf("%s,%s,%s,%s", StatusCreated, StatusPending, StatusDoing, StatusDone),
    },
})
validator.AddRules(rule.Enum)

task := Task{}
validator.Validate(task)
@guiferpa guiferpa added the enhancement New feature or request label Aug 14, 2020
@guiferpa guiferpa modified the milestones: v2, v3 Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest
Projects
None yet
Development

No branches or pull requests

1 participant