Skip to content

Commit

Permalink
IsRequired function is added. (#12)
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
cinar committed Jun 15, 2023
1 parent d83a8ed commit b5eec29
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
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
}
```

0 comments on commit b5eec29

Please sign in to comment.