No description
Find a file
Onur Cinar 5dd65585ed
Regexp checker is added. Fixes #48 (#67)
* Regexp checker is added. Fixes #48

* Fix the struct tag.
2023-06-18 21:23:37 -07:00
.devcontainer Devcontainer config. (#2) 2023-06-13 20:31:52 -07:00
.github Create pull_request_template.md (#65) 2023-06-18 19:19:57 -07:00
doc Regexp checker is added. Fixes #48 (#67) 2023-06-18 21:23:37 -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 Regexp checker is added. Fixes #48 (#67) 2023-06-18 21:23:37 -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
email.go Regexp checker is added. Fixes #48 (#67) 2023-06-18 21:23:37 -07:00
email_test.go Email checker is added. Fixes #28 (#63) 2023-06-18 19:01:47 -07:00
fqdn.go Email checker is added. Fixes #28 (#63) 2023-06-18 19:01:47 -07:00
fqdn_test.go FQDN checker is added. Fixes #61 (#62) 2023-06-18 14:57:29 -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
lower.go Upper and lower normalizers. Fixes #43 Fixes #44 (#58) 2023-06-18 11:35:36 -07:00
lower_test.go Upper and lower normalizers. Fixes #43 Fixes #44 (#58) 2023-06-18 11:35:36 -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 Regexp checker is added. Fixes #48 (#67) 2023-06-18 21:23:37 -07:00
regexp.go Regexp checker is added. Fixes #48 (#67) 2023-06-18 21:23:37 -07:00
regexp_test.go Regexp checker is added. Fixes #48 (#67) 2023-06-18 21:23:37 -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
title.go Title normalizer is added. Fixes #45 (#59) 2023-06-18 12:21:37 -07:00
title_test.go Update title normalizer test and example. (#60) 2023-06-18 12:25:44 -07:00
trim.go Trim left and right added. (#54) 2023-06-17 10:08:54 -07:00
trim_left.go Trim left and right added. (#54) 2023-06-17 10:08:54 -07:00
trim_left_test.go Trim left and right added. (#54) 2023-06-17 10:08:54 -07:00
trim_right.go Trim left and right added. (#54) 2023-06-17 10:08:54 -07:00
trim_right_test.go Trim left and right added. (#54) 2023-06-17 10:08:54 -07:00
trim_test.go Trim left and right added. (#54) 2023-06-17 10:08:54 -07:00
upper.go Upper and lower normalizers. Fixes #43 Fixes #44 (#58) 2023-06-18 11:35:36 -07:00
upper_test.go Upper and lower normalizers. Fixes #43 Fixes #44 (#58) 2023-06-18 11:35:36 -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.
  • email checks if the given string is an email address.
  • fqdn checks if the given string is a fully qualified domain name.
  • 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.
  • regexp checks if the given string matches the regexp pattern.
  • 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.

Normalizers Provided

This package currently provides the following normalizers. They can be mixed with the checkers when defining the validation steps for user data.

  • lower maps all Unicode letters in the given value to their lower case.
  • upper maps all Unicode letters in the given value to their upper case.
  • title maps the first letter of each word to their upper case.
  • trim removes the whitespaces at the beginning and at the end of the given value.
  • trim-left removes the whitespaces at the beginning of the given value.
  • trim-right removes the whitespaces at the end of the given value.

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"`
}

Code Quality

User input validation is a critical task that must be performed correctly in order to ensure that user data is handled correctly. This is why it is important to have extensive unit testing in place for any user input validation library.

The Checker library has a code coverage threshold of 100%, which means that all of the code in the library has been tested. This ensures that the library is extremely reliable and that it will not fail to validate user input correctly.

The test cases for the library can be found in the _test.go files. These files contain a comprehensive set of tests that cover all of the possible scenarios for user input validation.

If you are planning to make a pull request to this project, please make sure to add enough test cases to ensure that the code coverage remains at 100%. This will help to ensure that the library remains reliable and that user data is handled correctly.

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.