| 
									
										
										
										
											2019-12-03 21:21:57 +05:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	// stdlib | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"log" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// other | 
					
						
							|  |  |  | 	"go.dev.pztrn.name/valiwork" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	stringValidatorName = "string_validate_things" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	log.Println("Starting validation example...") | 
					
						
							|  |  |  | 	log.Println("WARN: to see additional valiwork output define 'VALIWORK_DEBUG' environment variable and set it to 'true'!") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//stringToValidate := " I am pretty b@d $tring" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 21:28:26 +05:00
										 |  |  | 	_ = valiwork.RegisterValidator(stringValidatorName, stringValidator) | 
					
						
							| 
									
										
										
										
											2019-12-03 21:21:57 +05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func stringValidator(thing interface{}, optional ...interface{}) []interface{} { | 
					
						
							|  |  |  | 	var errs []interface{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	stringToValidate, ok := thing.(string) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		errs = append(errs, errors.New("passed value is not a string")) | 
					
						
							|  |  |  | 		return errs | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Are string begins with spaces? | 
					
						
							|  |  |  | 	if strings.HasPrefix(stringToValidate, " ") { | 
					
						
							|  |  |  | 		errs = append(errs, errors.New("string begins with space")) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Does string contains any special characters? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return errs | 
					
						
							|  |  |  | } |