Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsRequired function is added. #12

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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ There are many validation libraries available, but I prefer to build my own tool

To get started, install the Checker library with the following command:

```golang
```bash
go get github.com/cinar/checker
```

Expand Down Expand Up @@ -37,7 +37,7 @@ if !valid {
}
```

### Validating Individual User Data
### Validating Individual User Input

If you do not want to validate user input stored in a struct, you can individually call the checker functions to validate the user input. Here is an example:

Expand Down Expand Up @@ -66,4 +66,4 @@ type Person struct {

This package currently provides the following checkers:

- [required]() checks if the required value is provided.
- [required](docs/checkers/required.md) checks if the required value is provided.
7 changes: 6 additions & 1 deletion check_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import "reflect"
// ResultRequired indicates that the required value is missing.
const ResultRequired Result = "REQUIRED"

// IsRequired checks if the given required value is present.
func IsRequired(v interface{}) Result {
return checkRequired(reflect.ValueOf(v), reflect.ValueOf(nil))
}

// makeRequired makes a checker function for required.
func makeRequired(_ string) CheckFunc {
return checkRequired
}

// checkRequired checks if the required value is provided.
// checkRequired checks if the required value is present.
func checkRequired(value, _ reflect.Value) Result {
if value.IsZero() {
return ResultRequired
Expand Down
8 changes: 8 additions & 0 deletions check_required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"testing"
)

func TestIsRequired(t *testing.T) {
s := "valid"

if IsRequired(s) != ResultValid {
t.Fail()
}
}

func TestCheckRequiredValidString(t *testing.T) {
s := "valid"

Expand Down
27 changes: 27 additions & 0 deletions doc/checkers/required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Required Checker

The ```required``` checker checks for the presence of required input. If the input is not present, the checker will return the ```REQUIRED``` result. Here is an example:

```golang
type Person struct {
Name string `checkers:"required"`
}

person := &Person{}

mistakes, valid := checker.Check(person)
if !valid {
// Send the mistakes back to the user
}
```

If you do not want to validate user input stored in a struct, you can individually call the ```required``` checker function ```IsRequired``` to validate the user input. Here is an example:

```golang
var name

result := checker.IsRequired(name)
if result != ResultValid {
// Send the result back to the user
}
```