Skip to content

dungntm58/checkit

Repository files navigation

Checkit

Inspiration

This project is heavily inspired by Checkit, a validation library originally written in JavaScript. You can explore the original library here:
https://github.com/tgriesser/checkit

While working on a blockchain platform project, I became highly interested in Golang. My primary responsibility was building a RESTful module to communicate with the core blockchain, and I initially wrote this library to streamline request body validation.

It allows you to seamlessly validate complete Go structs as well as primitive types, with full support for custom messages, labels, and validation rules.

Installation

To install the package and add it as a dependency, open your Terminal and run:

go get [github.com/dungntm58/checkit](https://github.com/dungntm58/checkit)

Example

Validate complex structure object

r, err := Validator(map[string]Validating{
  "a": Between(0, 2),
  "b": MaxLength(2),
  "c": ExactLength(3),
}).ValidateSync(struct {
  a int
  b *string
  c []interface{}
}{
  a: 1,
  b: &str,
  c: []interface{}{0, 1, 2},
})
fmt.Println(r) // true

Validate single value

r, err := Integer().Validate(1)
fmt.Println(r) // true

Available Validators

Validation Name Description
Accepted The value must be yes, on, or 1. This is useful for validating "Terms of Service" acceptance.
Alpha The value must be entirely alphabetic characters.
AlphaDash The value may have alpha-numeric characters, as well as dashes and underscores.
AlphaNumeric The value must be entirely alpha-numeric characters.
AlphaUnderscore The value must be entirely alpha-numeric, with underscores but not dashes.
Array The value must be a valid array object.
Base64 The value must be a base64 encoded value.
Between:min:max The value must have a size between the given min and max.
Boolean The value must be a javascript boolean.
Contains:value The value must be a string or an array and contain the value.
Date The value must be a valid date object.
Email The field must be a valid formatted e-mail address.
Empty The value under validation must be empty; either an empty string, an empty, array, empty object, or a falsy value.
ExactLength:value The field must have the exact length of "val".
ExistsNonNil The value under validation must be exist or not nil.
Finite The value under validation must be a finite number.
Function The value under validation must be a function.
GreaterThan:value The value under validation must be "greater than" the given value.
GreaterThanEqualTo:value The value under validation must be "greater than" or "equal to" the given value.
Integer The value must have an integer value.
Ipv4 The value must be formatted as an IPv4 address.
Ipv6 The value must be formatted as an IPv6 address.
LessThan:value The value must be "less than" the specified value.
LessThanEqualTo:value The value must be "less than" or "equal to" the specified value.
Luhn The given value must pass a basic luhn (credit card) check regular expression.
Max:value The value must be less than a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
MaxLength:value The value must have a length property which is less than or equal to the specified value. Note, this may be used with both arrays and strings.
Min:value The value must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
MinLength:value The value must have a length property which is greater than or equal to the specified value. Note, this may be used with both arrays and strings.
NaN The value must be NaN.
Natural The value must be a natural number (a number greater than or equal to 0).
NaturalNonZero The value must be a natural number, greater than or equal to 1.
Object The value must be anything except functions, pointers.
PlainObject The value must be a map.
Regex The value must be a Go RegExp object.
String The value must be a string type.
URL The value must be formatted as an URL.
UUID Passes for a validly formatted UUID.

License

This project is licensed under the MIT License.

About

Check Go structs, Go practice for fun

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages