Skip to content
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
29 changes: 29 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
name: Test
strategy:
matrix:
go: ['oldstable', 'stable']
runs-on: ubuntu-latest
steps:
- name: Set up Golang
uses: actions/setup-go@v5
with:
go-version: '${{ matrix.go }}'

- name: Checkout code
uses: actions/checkout@v2

- name: Test
run: |
test -z "`gofmt -l -d .`"
test -z "`go run golang.org/x/lint/golint@latest ./...`"
go test -v --race -covermode=atomic -coverprofile=coverage.out
7 changes: 4 additions & 3 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ type DateRule struct {
// Date returns a validation rule that checks if a string value is in a format that can be parsed into a date.
// The format of the date should be specified as the layout parameter which accepts the same value as that for time.Parse.
// For example,
// validation.Date(time.ANSIC)
// validation.Date("02 Jan 06 15:04 MST")
// validation.Date("2006-01-02")
//
// validation.Date(time.ANSIC)
// validation.Date("02 Jan 06 15:04 MST")
// validation.Date("2006-01-02")
//
// By calling Min() and/or Max(), you can let the Date rule to check if a parsed date value is within
// the specified date range.
Expand Down
9 changes: 5 additions & 4 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ type (
// Use Key() to specify map keys that need to be validated. Each Key() call specifies a single key which can
// be associated with multiple rules.
// For example,
// validation.Map(
// validation.Key("Name", validation.Required),
// validation.Key("Value", validation.Required, validation.Length(5, 10)),
// )
//
// validation.Map(
// validation.Key("Name", validation.Required),
// validation.Key("Value", validation.Required, validation.Length(5, 10)),
// )
//
// A nil value is considered valid. Use the Required rule to make sure a map value is present.
func Map(keys ...*KeyRules) MapRule {
Expand Down
20 changes: 10 additions & 10 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ func (e ErrFieldNotFound) Error() string {
// should be specified as a pointer to the field. A field can be associated with multiple rules.
// For example,
//
// value := struct {
// Name string
// Value string
// }{"name", "demo"}
// err := validation.ValidateStruct(&value,
// validation.Field(&a.Name, validation.Required),
// validation.Field(&a.Value, validation.Required, validation.Length(5, 10)),
// )
// fmt.Println(err)
// // Value: the length must be between 5 and 10.
// value := struct {
// Name string
// Value string
// }{"name", "demo"}
// err := validation.ValidateStruct(&value,
// validation.Field(&a.Name, validation.Required),
// validation.Field(&a.Value, validation.Required, validation.Length(5, 10)),
// )
// fmt.Println(err)
// // Value: the length must be between 5 and 10.
//
// An error will be returned if validation fails.
func ValidateStruct(structPtr interface{}, fields ...*FieldRules) error {
Expand Down
30 changes: 15 additions & 15 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ var (
// Validate validates the given value and returns the validation error, if any.
//
// Validate performs validation using the following steps:
// 1. For each rule, call its `Validate()` to validate the value. Return if any error is found.
// 2. If the value being validated implements `Validatable`, call the value's `Validate()`.
// Return with the validation result.
// 3. If the value being validated is a map/slice/array, and the element type implements `Validatable`,
// for each element call the element value's `Validate()`. Return with the validation result.
// 1. For each rule, call its `Validate()` to validate the value. Return if any error is found.
// 2. If the value being validated implements `Validatable`, call the value's `Validate()`.
// Return with the validation result.
// 3. If the value being validated is a map/slice/array, and the element type implements `Validatable`,
// for each element call the element value's `Validate()`. Return with the validation result.
func Validate(value interface{}, rules ...Rule) error {
for _, rule := range rules {
if s, ok := rule.(skipRule); ok && s.skip {
Expand Down Expand Up @@ -103,16 +103,16 @@ func Validate(value interface{}, rules ...Rule) error {
// ValidateWithContext validates the given value with the given context and returns the validation error, if any.
//
// ValidateWithContext performs validation using the following steps:
// 1. For each rule, call its `ValidateWithContext()` to validate the value if the rule implements `RuleWithContext`.
// Otherwise call `Validate()` of the rule. Return if any error is found.
// 2. If the value being validated implements `ValidatableWithContext`, call the value's `ValidateWithContext()`
// and return with the validation result.
// 3. If the value being validated implements `Validatable`, call the value's `Validate()`
// and return with the validation result.
// 4. If the value being validated is a map/slice/array, and the element type implements `ValidatableWithContext`,
// for each element call the element value's `ValidateWithContext()`. Return with the validation result.
// 5. If the value being validated is a map/slice/array, and the element type implements `Validatable`,
// for each element call the element value's `Validate()`. Return with the validation result.
// 1. For each rule, call its `ValidateWithContext()` to validate the value if the rule implements `RuleWithContext`.
// Otherwise call `Validate()` of the rule. Return if any error is found.
// 2. If the value being validated implements `ValidatableWithContext`, call the value's `ValidateWithContext()`
// and return with the validation result.
// 3. If the value being validated implements `Validatable`, call the value's `Validate()`
// and return with the validation result.
// 4. If the value being validated is a map/slice/array, and the element type implements `ValidatableWithContext`,
// for each element call the element value's `ValidateWithContext()`. Return with the validation result.
// 5. If the value being validated is a map/slice/array, and the element type implements `Validatable`,
// for each element call the element value's `Validate()`. Return with the validation result.
func ValidateWithContext(ctx context.Context, value interface{}, rules ...Rule) error {
for _, rule := range rules {
if s, ok := rule.(skipRule); ok && s.skip {
Expand Down
Loading