Skip to content

Commit f53fe54

Browse files
author
JubaerHossain
committed
readme file added
1 parent 70392a6 commit f53fe54

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Validation Package
2+
3+
[![GoDoc](https://godoc.org/github.com/JubaerHossain/go-validation/validation?status.svg)](https://godoc.org/github.com/JubaerHossain/go-validation/validation)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/JubaerHossain/go-validation)](https://goreportcard.com/report/github.com/JubaerHossain/go-validation)
5+
[![Build Status](https://travis-ci.org/JubaerHossain/go-validation.svg?branch=master)](https://travis-ci.org/JubaerHossain/go-validation)
6+
[![codecov](https://codecov.io/gh/JubaerHossain/go-validation/branch/master/graph/badge.svg)](https://codecov.io/gh/JubaerHossain/go-validation)
7+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8+
9+
This is a simple Go package for validating structs based on a set of validation rules. It can be used to ensure that incoming data from clients, databases or other sources meets certain criteria before being processed further.
10+
11+
# Installation
12+
13+
To use this package in your Go project, you can install it using the go get command:
14+
15+
```golang
16+
go get github.com/JubaerHossain/validation
17+
```
18+
19+
# Getting Started
20+
21+
Here's an example of how to use this package to validate a User struct:
22+
23+
```golang
24+
package main
25+
26+
import (
27+
"fmt"
28+
29+
"github.com/JubaerHossain/validation"
30+
)
31+
32+
type User struct {
33+
Name string `json:"first_name"`
34+
Password string `json:"password"`
35+
}
36+
37+
func main() {
38+
39+
userInput := User{
40+
Name: "John",
41+
Password: "123456",
42+
}
43+
validationErrors := ValidateUser(userInput)
44+
if validationErrors != nil {
45+
var errorMsgs []string
46+
for _, validationErr := range validationErrors {
47+
errorMsgs = append(errorMsgs, validationErr.Field+" : "+validationErr.Message)
48+
}
49+
fmt.Println(errorMsgs)
50+
}
51+
}
52+
53+
func ValidateUser(user User) []validation.ValidationErrorItem {
54+
55+
rules := []validation.ValidationRule{
56+
{
57+
Field: "name",
58+
Description: "Name",
59+
Validations: []func(interface{}) validation.ValidationErrorItem{
60+
validation.RequiredValidation,
61+
validation.MinLengthValidation(3),
62+
validation.MaxLengthValidation(50),
63+
},
64+
},
65+
{
66+
Field: "password",
67+
Description: "Password",
68+
Validations: []func(interface{}) validation.ValidationErrorItem{
69+
validation.RequiredValidation,
70+
validation.MinLengthValidation(6),
71+
validation.MaxLengthValidation(50),
72+
},
73+
},
74+
}
75+
76+
return validation.Validate(user, rules)
77+
}
78+
```
79+
80+
In this example, we define a User struct with two fields: Name and Password. We then define an array of ValidationRule structs that describe the validation rules for each field. Each ValidationRule has a Field name, a human-readable Description of the field, and an array of validation functions that take a value and return a ValidationErrorItem if the value is invalid.
81+
82+
We then call the Validate function with the User struct and the array of validation rules. This function returns an array of ValidationErrorItems if there are any validation errors, or an empty array if the input is valid.
83+
84+
# Available Validations
85+
86+
Here are the available validation functions that can be used in a ValidationRule:
87+
88+
- **RequiredValidation** - Returns an error if the value is nil or an empty string.
89+
- **MinLengthValidation(min int)** - Returns an error if the string length is less than the minimum value.
90+
- **MaxLengthValidation(max int)** - Returns an error if the string length is greater than the maximum value.
91+
- You can also define your own custom validation functions by implementing the func(interface{}) ValidationErrorItem signature.
92+
93+
# License
94+
95+
This package is licensed under the MIT License. See the LICENSE file for details.

example/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/JubaerHossain/validator"
7+
)
8+
9+
type User struct {
10+
Name string `json:"first_name"`
11+
Password string `json:"password"`
12+
}
13+
14+
func main() {
15+
16+
userInput := User{
17+
Name: "John",
18+
Password: "123456",
19+
}
20+
validationErrors := ValidateUser(userInput)
21+
if validationErrors != nil {
22+
var errorMsgs []string
23+
for _, validationErr := range validationErrors {
24+
errorMsgs = append(errorMsgs, validationErr.Field+" : "+validationErr.Message)
25+
}
26+
fmt.Println(errorMsgs)
27+
}
28+
}
29+
30+
func ValidateUser(user User) []validator.ValidationErrorItem {
31+
32+
rules := []validator.ValidationRule{
33+
{
34+
Field: "name",
35+
Description: "Name",
36+
Validations: []func(interface{}) validator.ValidationErrorItem{
37+
validator.RequiredValidation,
38+
validator.MinLengthValidation(3),
39+
validator.MaxLengthValidation(50),
40+
},
41+
},
42+
{
43+
Field: "password",
44+
Description: "Password",
45+
Validations: []func(interface{}) validator.ValidationErrorItem{
46+
validator.RequiredValidation,
47+
validator.MinLengthValidation(6),
48+
validator.MaxLengthValidation(50),
49+
},
50+
},
51+
}
52+
53+
return validator.Validate(user, rules)
54+
55+
}

0 commit comments

Comments
 (0)