From 43dbba36e1d759a79d6513f1ff3e714cadee9516 Mon Sep 17 00:00:00 2001 From: Elliot Courant Date: Fri, 17 Jan 2025 14:03:41 -0600 Subject: [PATCH 1/5] chore(ci): Adding github actions pipeline --- .github/workflows/main.yaml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/main.yaml diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..5ead544 --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,30 @@ +name: Test + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + name: Test + strategy: + matrix: + os: [ubuntu-latest,macos-latest,windows-latest] + go: ['oldstable', 'stable'] + runs-on: ${{ matrix.os }} + 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=count -coverprofile=coverage.out From cc2da63019717ef0f381ba1adb8d3ff312966a09 Mon Sep 17 00:00:00 2001 From: Elliot Courant Date: Fri, 17 Jan 2025 14:06:07 -0600 Subject: [PATCH 2/5] chore: Fixing go fmt --- date.go | 7 ++++--- map.go | 9 +++++---- struct.go | 20 ++++++++++---------- validation.go | 30 +++++++++++++++--------------- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/date.go b/date.go index 31da0e3..f6d4bb2 100644 --- a/date.go +++ b/date.go @@ -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. diff --git a/map.go b/map.go index 106b6d8..b3a87bd 100644 --- a/map.go +++ b/map.go @@ -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 { diff --git a/struct.go b/struct.go index d63619d..08b63ba 100644 --- a/struct.go +++ b/struct.go @@ -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 { diff --git a/validation.go b/validation.go index ec7a161..0495f32 100644 --- a/validation.go +++ b/validation.go @@ -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 { @@ -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 { From d1946e1c9676943a0b4e6ccf74665d638d57ecb6 Mon Sep 17 00:00:00 2001 From: Elliot Courant Date: Fri, 17 Jan 2025 14:08:05 -0600 Subject: [PATCH 3/5] chore: Tweaking coverage --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 5ead544..9d2452e 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -27,4 +27,4 @@ jobs: run: | test -z "`gofmt -l -d .`" test -z "`go run golang.org/x/lint/golint@latest ./...`" - go test -v --race -covermode=count -coverprofile=coverage.out + go test -v --race -covermode=atomic -coverprofile=coverage.out From 84c95537fde757399597f820f55fd95d628d28ac Mon Sep 17 00:00:00 2001 From: Elliot Courant Date: Fri, 17 Jan 2025 14:09:47 -0600 Subject: [PATCH 4/5] chore: Don't test on windows or mac --- .github/workflows/main.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 9d2452e..f090fd8 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -11,9 +11,8 @@ jobs: name: Test strategy: matrix: - os: [ubuntu-latest,macos-latest,windows-latest] go: ['oldstable', 'stable'] - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Set up Golang uses: actions/setup-go@v5 From a2d8837eb2a84b2de648ab6712d8d1d6e581064d Mon Sep 17 00:00:00 2001 From: Elliot Courant Date: Fri, 17 Jan 2025 14:11:02 -0600 Subject: [PATCH 5/5] chore: Tweas --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index f090fd8..be6d795 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -1,4 +1,4 @@ -name: Test +name: CI on: push: