├── LICENSE ├── README.md ├── example └── main.go ├── go.mod └── regexlite.go /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Boluwatife Olaifa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Regex Lite🔥 [![Twitter Follow](https://img.shields.io/twitter/follow/iamgrandbusta?style=social)](https://twitter.com/iamgrandbusta) 2 | 3 | ## 📖Description 4 | 5 | `regexlite` is a Golang utility library designed to simplify the creation and validation of regular expressions. 6 | 7 | ## ✨Features 8 | 9 | - [x] Chainable Functions 10 | - [x] Easy construction of complex regex patterns 11 | - [x] Built-in validators for common use-cases like email, URL, etc. 12 | - [x] Customizable and extendable 13 | 14 | ## Installation 15 | 16 | ```bash 17 | go get github.com/Grandbusta/regexlite 18 | ``` 19 | 20 | ## 🛠️Usage 21 | 22 | Here's a quick example to get you started: 23 | 24 | ```go 25 | package main 26 | 27 | import ( 28 | "fmt" 29 | 30 | "github.com/Grandbusta/regexlite" 31 | ) 32 | 33 | func main() { 34 | isValidPassword, err := regexlite.Value("Thisisapass&").Min(8).Max(30).HasUpperCase().HasSpecialCharacter().Validate() 35 | fmt.Println(isValidPassword, err) // returns true or false based on validation. err returns nil or error based on validation 36 | } 37 | ``` 38 | 39 | ## 📮Available Methods 40 | 41 | - `HasText()`: Ensures the string contains text (a-z, A-Z). 42 | - `HasNumbers()`: Ensures the string contains numbers. 43 | - `HasSpecialCharacter()`: Ensures the string contains special characters. 44 | - `Min(length: int)`: Sets the minimum length of the string. 45 | - `Max(length: int)`: Sets the maximum length of the string. 46 | - `Validate()`: Executes the validation and returns a boolean and error result. 47 | - `HasUpperCase()`: Ensures string contains at least one uppercase character. 48 | - `HasLowerCase()`: Ensures string contains at least one lowercase character. 49 | - `IsEmail()`: Ensures string is a valid email. 50 | - `IsUrl()`: Ensures string is a valid url. 51 | - `Contains(substring string)`: Ensures string contains a substring. 52 | 53 | ## ➕Contributing 54 | 55 | If you have a suggestion that would make this project better, please fork the repo and create a pull request. 56 | Don't forget to give the project a star! Thanks again! 57 | 58 | ## 🤓 Author(s) 59 | 60 | **Olaifa Boluwatife Jonathan** [![Twitter Follow](https://img.shields.io/twitter/follow/iamgrandbusta?style=social)](https://twitter.com/iamgrandbusta) 61 | 62 | ## 🔖 License 63 | 64 | Distributed under the MIT License. See `LICENSE` for more information. 65 | -------------------------------------------------------------------------------- /example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/Grandbusta/regexlite" 7 | ) 8 | 9 | func main() { 10 | isValidPassword, err := regexlite.Value("Thisisapass&").Min(8).Max(30).HasUpperCase().HasSpecialCharacter().Validate() 11 | fmt.Println(isValidPassword, err) // returns true or false based on validation. err returns nil or error based on validation 12 | } 13 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Grandbusta/regexlite 2 | 3 | go 1.21.3 4 | -------------------------------------------------------------------------------- /regexlite.go: -------------------------------------------------------------------------------- 1 | package regexlite 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "log" 7 | "regexp" 8 | ) 9 | 10 | type regexPart struct { 11 | errorText string 12 | regex string 13 | } 14 | 15 | type lengthData struct { 16 | length int 17 | errorText string 18 | } 19 | 20 | type data struct { 21 | value string 22 | regexparts []regexPart 23 | minLengthData lengthData 24 | maxLengthData lengthData 25 | } 26 | 27 | func getRegexPart(regex string, errorText string) regexPart { 28 | return regexPart{errorText: errorText, regex: regex} 29 | } 30 | 31 | func Value(value string) *data { 32 | return &data{value: value} 33 | } 34 | 35 | func (d *data) HasText() *data { 36 | d.regexparts = append( 37 | d.regexparts, 38 | getRegexPart(".*[A-Za-z]+.*", "text not present in value"), 39 | ) 40 | return d 41 | } 42 | 43 | func (d *data) HasNumbers() *data { 44 | d.regexparts = append( 45 | d.regexparts, 46 | getRegexPart(".*[0-9].*", "number not present in value"), 47 | ) 48 | return d 49 | } 50 | 51 | func (d *data) HasSpecialCharacter() *data { 52 | d.regexparts = append( 53 | d.regexparts, 54 | getRegexPart(".*[!@#$%^&*].*", "special character not present in value"), 55 | ) 56 | return d 57 | } 58 | func (d *data) HasUpperCase() *data { 59 | d.regexparts = append(d.regexparts, getRegexPart(".*[A-Z]", "uppercase not present in value")) 60 | return d 61 | } 62 | func (d *data) HasLowerCase() *data { 63 | d.regexparts = append( 64 | d.regexparts, 65 | getRegexPart(".*[a-z].*", "lower case not present in value"), 66 | ) 67 | return d 68 | } 69 | 70 | func (d *data) IsEmail() *data { 71 | d.regexparts = append( 72 | d.regexparts, 73 | getRegexPart("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", "invalid email"), 74 | ) 75 | return d 76 | } 77 | 78 | func (d *data) IsUrl() *data { 79 | d.regexparts = append( 80 | d.regexparts, 81 | getRegexPart("^(https?|ftp):\\/\\/[^\\s/$.?#].[^\\s]*$", "invalid Url"), 82 | ) 83 | return d 84 | } 85 | 86 | func (d *data) Contains(substring string) *data { 87 | d.regexparts = append( 88 | d.regexparts, 89 | getRegexPart(fmt.Sprintf(".*%v", substring), fmt.Sprintf("substring '%v' not present in value", substring)), 90 | ) 91 | return d 92 | } 93 | 94 | func (d *data) Min(length int) *data { 95 | d.minLengthData = lengthData{ 96 | length: length, 97 | errorText: fmt.Sprintf("value should have a minimum of %v characters", length), 98 | } 99 | return d 100 | } 101 | 102 | func (d *data) Max(length int) *data { 103 | d.maxLengthData = lengthData{ 104 | length: length, 105 | errorText: fmt.Sprintf("value should have a maximum of %v characters", length), 106 | } 107 | return d 108 | } 109 | 110 | func (d *data) Validate() (valid bool, err error) { 111 | if d.minLengthData.length != 0 { 112 | if len(d.value) < d.minLengthData.length { 113 | return false, errors.New(d.minLengthData.errorText) 114 | } 115 | } 116 | if d.maxLengthData.length != 0 { 117 | if len(d.value) > d.maxLengthData.length { 118 | return false, errors.New(d.maxLengthData.errorText) 119 | } 120 | } 121 | for _, regexpart := range d.regexparts { 122 | r, err := regexp.Compile(regexpart.regex) 123 | if err != nil { 124 | log.Fatal(err) 125 | } 126 | if !r.MatchString(d.value) { 127 | return false, errors.New(regexpart.errorText) 128 | } 129 | } 130 | return true, nil 131 | } 132 | --------------------------------------------------------------------------------