https://github.com/patrickkabwe/go-validator
Go Validator is a simple package for validating email addresses, URLs, IP addresses, and empty fields in Go. It also provides a way to validate struct fields using struct tags.
https://github.com/patrickkabwe/go-validator
go go-validation go-validator golang validation
Last synced: over 1 year ago
JSON representation
Go Validator is a simple package for validating email addresses, URLs, IP addresses, and empty fields in Go. It also provides a way to validate struct fields using struct tags.
- Host: GitHub
- URL: https://github.com/patrickkabwe/go-validator
- Owner: patrickkabwe
- License: mit
- Created: 2023-05-21T09:36:47.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-12T14:18:58.000Z (about 2 years ago)
- Last Synced: 2025-02-15T17:40:44.130Z (over 1 year ago)
- Topics: go, go-validation, go-validator, golang, validation
- Language: Go
- Homepage:
- Size: 45.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Go Validator
Go Validator is a simple package for validating structs, email addresses, URLs, IP addresses, and empty fields in Go. It also provides a way to validate struct fields using struct tags.
## Installation
To use this package, you need to have Go installed and set up. Then you can run the following command to add the package to your project:
```go
go get github.com/patrickkabwe/go-validator
```
## Features
- 📧 `Email` validation
- 🌐 `URL` validation
- 🌐 `IP` address validation
- 📝 `Empty` field validation
- 📦 `Struct` validation using struct tags
- 📊 `Map, Slice, Embedded Struct` validation (Coming Soon)
## Usage
Import the validator package into your Go code:
```go
import "github.com/patrickkabwe/go-validator"
```
## Creating a Validator
To create a new instance of the Validator interface, use the New() function:
```go
v := validator.New()
```
### Validating Email Addresses
To check if a string is a valid email address, use the IsEmail() method:
```go
ok, err := v.IsEmail(email)
if err != nil {
// handle the error
}
```
### Checking for Empty Fields
To validate if a string is empty, use the IsEmpty() method:
```go
var input = "" // empty string
var input = "John Doe" // non-empty string
ok, err := v.IsEmpty(input)
if err != nil {
// handle the error
}
```
### Validating URLs
To validate if a string is a valid URL, use the IsURL() method:
```go
var input = "https://example.com"
ok, err := v.IsURL(input)
if err != nil {
// handle the error
}
```
### Validating IP Addresses
To validate if a string is a valid IP address, use the IsIP() method:
```go
var input = "127.0.0.1"
ok, err := v.IsIP(input)
if err != nil {
// handle the error
}
```
### Validating Structs
To perform structural validation on a struct, use the ValidateStruct() method. It checks for fields with validate tags and returns a slice of errors:
```go
type User struct {
Name string `validate:"required"`
Email string `validate:"required,email"`
}
input := User{
Name: "John Doe",
Email: "test@gmail.com",
}
errors := v.ValidateStruct(input)
if len(errors) > 0 {
// handle the validation errors
}
```
### Contributing
Contributions to this package are welcome. Feel free to submit issues and pull requests on the GitHub repository.
### License
This package is licensed under the MIT License. See the LICENSE file for more information.