├── main.go ├── README.md └── rex.go /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type validate struct { 4 | i string 5 | r bool 6 | e string 7 | } 8 | 9 | func validator(u string) validate { 10 | var n validate 11 | n.i = u 12 | n.r = true 13 | n.e = "" 14 | return n 15 | } 16 | 17 | func main() { 18 | newInput := validator("golangV$alidation") 19 | println(newInput.Required().isSpecialCharacter().check()) 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Image of Yaktocat](https://www.devteam.space/wp-content/uploads/2017/03/gopher_head-min.png 2 | ) 3 | 4 | # Text and Input Validation in Golang 5 | 6 | I've created this helping script for my APIs to deal with user inputs. Just clone the repo and use it in your project. 7 | 8 | ## Usage: 9 | 10 | 1. Define the validator 11 | 12 | ` 13 | userInput := validator("blah!blah!blah!") 14 | ` 15 | 16 | 2. Apply the methods 17 | 18 | `userInput.Required().oneLowerCase().check()` `// will return true or false` 19 | 20 | 21 | 22 | ## Methods 23 | 24 | use `.check()` after all methods to return the `boolean` value, otherwise the method will return `type struct` 25 | 26 | | Methods | Descrption | 27 | | ------------------ | :----------------------------------------------------------- | 28 | | Required() | returns `false` if input is empty | 29 | | minLength(int) | returns `false` if input is less than provided minimum value | 30 | | maxLength(int) | returns `false` if input is greater than provided maximum value | 31 | | isEmail | return `false` if input is not an email address | 32 | | oneLowerCase | return `false` if input value doesn't have at least one lowercase character | 33 | | allLowerCase | return `false`if input value doesn't have all lowercase characters | 34 | | oneUpperCase | return `false` if input value doesn't have at least one uppercase character | 35 | | allUpperCase | return `false` if input value doesn't have all uppercase characters | 36 | | oneNumber | return `false` if input value doesn't have at least one number character | 37 | | isSpecialCharacter | return `false` if input value doesn't have at least one special character | 38 | 39 | ## Using Multiple Methods: 40 | 41 | I've design the methods in a way that multiple methods can be used in a line. 42 | 43 | For example to check for a strong password you can use. 44 | 45 | `userInput.minLength(8).oneLowerCase().oneUpperCase().oneNumber().isSpecialCharacter().check()` 46 | 47 | The above code will check for the applied methods and will return boolean `false` if any method doesn't validate the password. 48 | -------------------------------------------------------------------------------- /rex.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "regexp" 5 | "strconv" 6 | ) 7 | 8 | func (s *validate) Required() *validate { 9 | if s.r == true { 10 | if s.i == "" || len(s.i) <= 0 { 11 | s.r = false 12 | s.e = "Is Required" 13 | } 14 | } 15 | return s 16 | } 17 | 18 | func (s *validate) minLength(length int) *validate { 19 | if s.r == true { 20 | if len(s.i) < length { 21 | s.r = false 22 | s.e = "Minimum length " + strconv.Itoa(length) + " characters allowed" 23 | } 24 | } 25 | return s 26 | } 27 | 28 | func (s *validate) maxLength(length int) *validate { 29 | if s.r == true { 30 | if len(s.i) > length { 31 | s.r = false 32 | s.e = "Maximum length " + strconv.Itoa(length) + " characters allowed" 33 | } 34 | } 35 | return s 36 | } 37 | 38 | func (s *validate) isEmail() *validate { 39 | if s.r == true { 40 | re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") 41 | var match = re.MatchString(s.i) 42 | if !match { 43 | s.r = false 44 | s.e = "invalid email" 45 | } 46 | } 47 | return s 48 | } 49 | 50 | func (s *validate) oneLowerCase() *validate { 51 | if s.r == true { 52 | re := regexp.MustCompile("[a-z]+") 53 | var match = re.MatchString(s.i) 54 | if !match { 55 | s.r = false 56 | s.e = "does not contain atleast lowercase letter" 57 | } 58 | } 59 | return s 60 | } 61 | 62 | func (s *validate) allLowerCase() *validate { 63 | if s.r == true { 64 | re := regexp.MustCompile("^[a-z]+$") 65 | var match = re.MatchString(s.i) 66 | if !match { 67 | s.r = false 68 | s.e = "does not contain lowercase letter" 69 | } 70 | } 71 | return s 72 | } 73 | 74 | func (s *validate) oneUpperCase() *validate { 75 | if s.r == true { 76 | re := regexp.MustCompile("[A-Z]+") 77 | var match = re.MatchString(s.i) 78 | if !match { 79 | s.r = false 80 | s.e = "does not contain atleast one uppercase letter" 81 | } 82 | } 83 | return s 84 | } 85 | 86 | func (s *validate) allUpperCase() *validate { 87 | if s.r == true { 88 | re := regexp.MustCompile("^[A-Z]+$") 89 | var match = re.MatchString(s.i) 90 | if !match { 91 | s.r = false 92 | s.e = "all letters are not uppercase" 93 | } 94 | } 95 | return s 96 | } 97 | 98 | func (s *validate) oneNumber() *validate { 99 | if s.r == true { 100 | re := regexp.MustCompile("[0-9]+") 101 | var match = re.MatchString(s.i) 102 | if !match { 103 | s.r = false 104 | s.e = "does not contain atleast one numeric character" 105 | } 106 | } 107 | return s 108 | } 109 | 110 | func (s *validate) isSpecialCharacter() *validate { 111 | if s.r == true { 112 | re := regexp.MustCompile("\\`|\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\+|\\=|\\[|\\{|\\]|\\}|\\||\\|\\'|\\<|\\,|\\.|\\>|\\?|\\/|\"|\\;|\\:|\\s") 113 | var match = re.MatchString(s.i) 114 | if !match { 115 | s.r = false 116 | s.e = "does not contain atleast one special character" 117 | } 118 | } 119 | return s 120 | } 121 | 122 | func (s *validate) check() bool { 123 | return s.r 124 | } 125 | --------------------------------------------------------------------------------