From b5eec292bb2e9c57bc680d68b375b84ee49b9cba Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Thu, 15 Jun 2023 13:57:49 -0700 Subject: [PATCH] IsRequired function is added. (#12) Fixes #11 --- README.md | 6 +++--- check_required.go | 7 ++++++- check_required_test.go | 8 ++++++++ doc/checkers/required.md | 27 +++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 doc/checkers/required.md diff --git a/README.md b/README.md index fe8c255..b89b0f5 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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: @@ -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. diff --git a/check_required.go b/check_required.go index 4d5cac8..e83d53d 100644 --- a/check_required.go +++ b/check_required.go @@ -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 diff --git a/check_required_test.go b/check_required_test.go index 82ae55a..3b689e2 100644 --- a/check_required_test.go +++ b/check_required_test.go @@ -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" diff --git a/doc/checkers/required.md b/doc/checkers/required.md new file mode 100644 index 0000000..bac89d6 --- /dev/null +++ b/doc/checkers/required.md @@ -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 +} +```