No description
Find a file
Onur Cinar ec327958ca
IP checkers are added. (#41)
* IPv4 checker is added. Fixes #23

* IPv6 checker is added. Fixes #24

* IP checker is added. Fixes #22

* IPv4 and IPv6 tests.
2023-06-16 20:59:57 -07:00
.devcontainer Devcontainer config. (#2) 2023-06-13 20:31:52 -07:00
.github/workflows Code coverage. (#39) 2023-06-16 20:12:10 -07:00
doc IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
scripts Code coverage. (#39) 2023-06-16 20:12:10 -07:00
.gitignore Initial commit 2023-06-13 20:27:57 -07:00
alphanumeric.go Alphanumeric checker is added. (#38) 2023-06-16 19:23:34 -07:00
alphanumeric_test.go Alphanumeric checker is added. (#38) 2023-06-16 19:23:34 -07:00
ascii.go Alphanumeric checker is added. (#38) 2023-06-16 19:23:34 -07:00
ascii_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
checker.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
checker_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
digits.go Alphanumeric checker is added. (#38) 2023-06-16 19:23:34 -07:00
digits_test.go Digits checker is added. (#37) 2023-06-16 19:08:40 -07:00
go.mod Go module. (#1) 2023-06-13 20:30:33 -07:00
ip.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
ip_test.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
ipv4.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
ipv4_test.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
ipv6.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
ipv6_test.go IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
LICENSE Initial commit 2023-06-13 20:27:57 -07:00
max.go Min and Max added. (#35) 2023-06-16 15:17:39 -07:00
max_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
maxlength.go Max Length checker added. (#33) 2023-06-15 18:51:16 -07:00
maxlength_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
min.go Min and Max added. (#35) 2023-06-16 15:17:39 -07:00
min_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
minlenght.go Min length checker added. (#32) 2023-06-15 18:29:45 -07:00
minlength_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
README.md IP checkers are added. (#41) 2023-06-16 20:59:57 -07:00
required.go Min length checker added. (#32) 2023-06-15 18:29:45 -07:00
required_test.go Same checker is added. (#30) 2023-06-15 16:15:32 -07:00
same.go Min length checker added. (#32) 2023-06-15 18:29:45 -07:00
same_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
test_helper.go Code coverage. (#39) 2023-06-16 20:12:10 -07:00
test_helper_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00

Checker

Checker is a Go library that helps you validate user input. It can be used to validate user input stored in a struct, or to validate individual pieces of input.

There are many validation libraries available, but I prefer to build my own tools and avoid pulling in unnecessary dependencies. That's why I created Checker, a simple validation library with no dependencies. It's easy to use and gets the job done.

Usage

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

go get github.com/cinar/checker

Next, you will need to import the library into your source file. You can do this by following the example below:

import (
    "github.com/cinar/checker"
)

Validating User Input Stored in a Struct

Checker can be used in two ways. The first way is to validate user input stored in a struct. To do this, you can list the checkers through the struct tag for each field. Here is an example:

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

person := &Person{}

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

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:

var name

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

Normalizers and Checkers

Checkers are used to check for problems in user input, while normalizers are used to transform user input into a preferred format. For example, a normalizer could be used to trim spaces from the beginning and end of a string, or to convert a string to title case.

I am not entirely happy with the decision to combine checkers and normalizers into a single library, but using them together can be useful. Normalizers and checkers can be mixed in any order when defining the validation steps for user data. For example, the trim normalizer can be used in conjunction with the required checker to first trim the user input and then check if the user provided the required information. Here is an example:

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

Checkers Provided

This package currently provides the following checkers:

  • alphanumeric checks if the given string consists of only alphanumeric characters.
  • ascii checks if the given string consists of only ASCII characters.
  • digits checks if the given string consists of only digit characters.
  • ip checks if the given value is an IP address.
  • ipv4 checks if the given value is an IPv4 address.
  • ipv6 checks if the given value is an IPv6 address.
  • max checks if the given value is less than the given maximum.
  • max-length checks if the length of the given value is less than the given maximum length.
  • min checks if the given value is greather than the given minimum.
  • min-length checks if the length of the given value is greather than the given minimum length.
  • required checks if the required value is provided.
  • same checks if the given value is equal to the value of the field with the given name.