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

Improve usage documentation #14

Open
SharkFourSix opened this issue Oct 1, 2024 · 1 comment
Open

Improve usage documentation #14

SharkFourSix opened this issue Oct 1, 2024 · 1 comment

Comments

@SharkFourSix
Copy link

The example on the repo page only depicts usage of a single rule.

  1. How do you use multiple rules on a single field?
  2. Do we have to manually add the included rules all the time when we instantiate the validator?
@guiferpa
Copy link
Owner

Hey @SharkFourSix , sorry for delay.

🌵 Currently this project really doesn't have a detailed documentation, let's join effort for it, be my guest to help the project.

  1. How do you use multiple rules on a single field?
    There's a folder named examples with some examples about implementations. For specifically multiple rules I hope this example help you

  2. Do we have to manually add the included rules all the time when we instantiate the validator?
    Unfortunately yes, gody was built thinking about many packages for different configurations then in this way you can have many packages with different instances with your each config validator.

package foo

import (
	"github.com/guiferpa/gody/v2"
	"github.com/guiferpa/gody/v2/rule"
)

var (
	Validator    *gody.Validator
	DefaultRules []gody.Rule
)

func init() {
	DefaultRules = []gody.Rule{rule.NotEmpty}
	Validator = gody.NewValidator()
	Validator.AddRules(DefaultRules...)
}
package bar

import (
	"github.com/guiferpa/gody-sample/foo"

	"github.com/guiferpa/gody/v2"
	"github.com/guiferpa/gody/v2/rule"
)

var (
	Validator    *gody.Validator
	DefaultRules []gody.Rule
)

func init() {
	DefaultRules = []gody.Rule{rule.Min}
	Validator = gody.NewValidator()
	Validator.AddRules((append(DefaultRules, foo.DefaultRules...))...)
}
package main

import (
	"fmt"

	"github.com/guiferpa/gody-sample/bar"
	"github.com/guiferpa/gody-sample/foo"
)

type FooBar struct {
	Attribute string `validate:"not_empty"`
}

func main() {
	foobar := FooBar{}
	if _, err := foo.Validator.Validate(foobar); err != nil {
		fmt.Println(err) // field attribute cannot be empty
	}
	if _, err := bar.Validator.Validate(foobar); err != nil {
		fmt.Println(err) // field attribute cannot be empty
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants