Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Removed config struct changed to NewRule
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Jan 24, 2021
1 parent f1a7cc1 commit ba37b1b
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 1,056 deletions.
33 changes: 15 additions & 18 deletions README-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ $ go get github.com/teacat/tavern
## 範例

```go
err := tavern.Validate([]tavern.Rule{
{
Value: "你好,世界!",
Validators: []tavern.Validator{
tavern.WithRequired(),
tavern.WithMinLength(5),
},
},
})
err := tavern.Validate(
NewRule("你好,世界!",
tavern.WithRequired(),
tavern.WithMinLength(5),
),
NewRule(198964,
tavern.WithMin(100),
),
)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -52,15 +52,12 @@ type Validator func(ctx context.Context, value interface{}) (context.Context, er
事實上這個函式也是一個驗證器,但會在傳入的驗證器發生錯誤時回傳你自訂的錯誤訊息。

```go
err := tavern.Validate([]tavern.Rule{
{
Value: "",
Validators: []tavern.Validator{
tavern.WithCustomError(tavern.WithRequired(), errors.New("你在公三小")),
tavern.WithMinLength(5),
},
},
})
err := tavern.Validate(
NewRule("",
tavern.WithCustomError(tavern.WithRequired(), errors.New("你在公三小")),
tavern.WithMinLength(5),
),
)
if err != nil {
panic(err) // 輸出:你在公三小
}
Expand Down
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ $ go get github.com/teacat/tavern
## Example

```go
err := tavern.Validate([]tavern.Rule{
{
Value: "Hello, world!",
Validators: []tavern.Validator{
tavern.WithRequired(),
tavern.WithMinLength(5),
},
},
})
err := tavern.Validate(
NewRule("Hello, world!",
tavern.WithRequired(),
tavern.WithMinLength(5),
),
NewRule(198964,
tavern.WithMin(100),
),
)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -52,15 +52,12 @@ By default, Tavern returns built-in errors such as `ErrRequired`, `ErrLength` mi
It's also a validator but returns your own custom error when the passed-in validator failed.

```go
err := tavern.Validate([]tavern.Rule{
{
Value: "",
Validators: []tavern.Validator{
tavern.WithCustomError(tavern.WithRequired(), errors.New("nani the fuck")),
tavern.WithMinLength(5),
},
},
})
err := tavern.Validate(
NewRule("",
tavern.WithCustomError(tavern.WithRequired(), errors.New("nani the fuck")),
tavern.WithMinLength(5),
),
)
if err != nil {
panic(err) // output: nani the fuck
}
Expand Down
20 changes: 14 additions & 6 deletions tavern.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@ import "context"
// Rule has the validators to validate the value. The name is optional, a named rule is useful if you wanted to know which value is invalid.
type Rule struct {
// Name of the value, optional.
Name string
name string
// Value is the content will be validated by the validators.
Value interface{}
value interface{}
// Validators to validate the value.
Validators []Validator
validators []Validator
}

// Validator is used to validate the value, it accepts a context to pass between the validators.
type Validator func(ctx context.Context, value interface{}) (context.Context, error)

// Validate validates all the rules that passed in.
func Validate(rules []Rule) (err error) {
func Validate(rules ...Rule) (err error) {
for _, v := range rules {
ctx := context.Background()
for _, j := range v.Validators {
ctx, err = j(ctx, v.Value)
for _, j := range v.validators {
ctx, err = j(ctx, v.value)
if err != nil {
return err
}
}
}
return nil
}

// NewRule creates a new rule for the value and it will be validated by the validators. The returned `Rule` should be passed to `Validate`.
func NewRule(value interface{}, validators ...Validator) Rule {
return Rule{
value: value,
validators: validators,
}
}

0 comments on commit ba37b1b

Please sign in to comment.