Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dreamph/validation
Golang Validation
https://github.com/dreamph/validation
api golang validation validation-builder
Last synced: about 2 months ago
JSON representation
Golang Validation
- Host: GitHub
- URL: https://github.com/dreamph/validation
- Owner: dreamph
- License: mit
- Created: 2024-02-21T02:42:43.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-02-29T05:24:07.000Z (10 months ago)
- Last Synced: 2024-06-21T14:29:55.310Z (6 months ago)
- Topics: api, golang, validation, validation-builder
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Validation Builder build on top [ozzo-validation](https://github.com/go-ozzo/ozzo-validation)
# Example Usage
```go
package mainimport (
"fmt""github.com/dreamph/validation"
)type Wrapper struct {
Attr1 int `json:"attr1"`
Type int `json:"type"`
FieldOne FieldOne `json:"fieldOne"`
FieldTwo *FieldTwo `json:"fieldTwo"`
FieldThree []FieldThree `json:"fieldThree"`
}type FieldOne struct {
FieldThree string `json:"fieldThree"`
FieldFour string `json:"fieldFour"`
}type FieldTwo struct {
FieldFive int `json:"fieldFive"`
}type FieldThree struct {
Data int `json:"data"`
}func main() {
request := Wrapper{
Attr1: 0,
Type: 3,FieldOne: FieldOne{
FieldThree: "Test",
FieldFour: "",
},FieldTwo: &FieldTwo{
FieldFive: 16,
},FieldThree: []FieldThree{
{Data: -1},
},
}validationBuilder := validation.NewStructValidationBuilder(&request)
validationBuilder.AddRequiredFieldRules(
validation.Field(&request.Type, validation.Required, validation.In(1, 2, 3)),
)validationBuilder.AddFieldRules(
validation.Field(&request.Attr1, validation.Required),
)validationBuilder.AddFieldRules(
validation.StructField[FieldOne](&request.FieldOne, func(value FieldOne) error {
return validation.ValidateStruct(&value,
validation.Field(&value.FieldThree, validation.Required),
validation.Field(&value.FieldFour, validation.Required),
)
}),
validation.StructField[FieldTwo](&request.FieldTwo, func(value FieldTwo) error {
return validation.ValidateStruct(&value,
validation.Field(&value.FieldFive, validation.Required),
)
}),
validation.ArrayField[FieldThree](&request.FieldThree, func(value FieldThree, i int) error {
return validation.ValidateStruct(&value,
validation.Field(&value.Data, validation.Required, validation.Min(0)),
)
}),
)err := validationBuilder.Validate()
if err != nil {
fmt.Println(err)
}
}```