No description
Find a file
Onur Cinar 7046bec40a
MAC and CIDR checkers are added. (#53)
* CIDR checker is added. Fixes #42

* Update README.md.

* MAC checker is added. Fixes #25

* Remove file.
2023-06-17 00:51:06 -07:00
.devcontainer Devcontainer config. (#2) 2023-06-13 20:31:52 -07:00
.github/workflows Update go.yml. (#51) 2023-06-17 00:15:22 -07:00
doc MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -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 MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -07:00
checker_test.go ASCII checker is added. (#36) 2023-06-16 16:28:01 -07:00
cidr.go MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -07:00
cidr_test.go MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -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
mac.go MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -07:00
mac_test.go MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -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 MAC and CIDR checkers are added. (#53) 2023-06-17 00:51:06 -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

GoDoc License Go CI

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.
  • cidr checker checks if the value is a valid CIDR notation IP address and prefix length.
  • 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.
  • mac checks if the given value is a valid an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer 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.

Custom Checkers

To define a custom checker, you need to create a new function with the following parameters:

func CustomChecker(value, parent reflect.Value) Result {
    return ResultValid
}

type MakeFunc You also need to create a make function that takes the checker configuration and returns a reference to the checker function.

func CustomMaker(params string) CheckFunc {
    return CustomChecker
}

Finally, you need to call the Register function to register your custom checker.

checker.Register("custom-checker", CustomMaker)

Once you have registered your custom checker, you can use it by simply specifying its name.

type User struct {
    Username string `checkers:"custom-checker"`
}

License

This library is free to use, modify, and distribute under the terms of the MIT license. The full license text can be found in the LICENSE file.

The MIT license is a permissive license that allows you to do almost anything with the library, as long as you retain the copyright notice and the license text. This means that you can use the library in commercial products, modify it, and redistribute it without having to ask for permission from the authors.

The LICENSE file is located in the root directory of the library. You can open it in a text editor to read the full license text.