How to use NewValidator method of validation Package

Best Gauge code snippet using validation.NewValidator

gvalid_validator.go

Source:gvalid_validator.go Github

copy

Full Screen

1// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.2//3// This Source Code Form is subject to the terms of the MIT License.4// If a copy of the MIT was not distributed with this file,5// You can obtain one at https://github.com/gogf/gf.6package gvalid7import (8 "context"9 "errors"10 "reflect"11 "github.com/gogf/gf/v2/i18n/gi18n"12 "github.com/gogf/gf/v2/internal/utils"13 "github.com/gogf/gf/v2/util/gconv"14)15// Validator is the validation manager for chaining operations.16type Validator struct {17 i18nManager *gi18n.Manager // I18n manager for error message translation.18 data interface{} // Validation data, which can be a map, struct or a certain value to be validated.19 assoc interface{} // Associated data, which is usually a map, for union validation.20 rules interface{} // Custom validation data.21 messages interface{} // Custom validation error messages, which can be string or type of CustomMsg.22 ruleFuncMap map[string]RuleFunc // ruleFuncMap stores custom rule functions for current Validator.23 useDataInsteadOfObjectAttributes bool // Using `data` as its validation source instead of attribute values from `Object`.24 bail bool // Stop validation after the first validation error.25 caseInsensitive bool // Case-Insensitive configuration for those rules that need value comparison.26}27// New creates and returns a new Validator.28func New() *Validator {29 return &Validator{30 i18nManager: gi18n.Instance(), // Use default i18n manager.31 ruleFuncMap: make(map[string]RuleFunc), // Custom rule function storing map.32 }33}34// Run starts validating the given data with rules and messages.35func (v *Validator) Run(ctx context.Context) Error {36 if v.data == nil {37 return newValidationErrorByStr(38 internalParamsErrRuleName,39 errors.New(`no data passed for validation`),40 )41 }42 originValueAndKind := utils.OriginValueAndKind(v.data)43 switch originValueAndKind.OriginKind {44 case reflect.Map:45 isMapValidation := false46 if v.rules == nil {47 isMapValidation = true48 } else if utils.IsMap(v.rules) || utils.IsSlice(v.rules) {49 isMapValidation = true50 }51 if isMapValidation {52 return v.doCheckMap(ctx, v.data)53 }54 case reflect.Struct:55 isStructValidation := false56 if v.rules == nil {57 isStructValidation = true58 } else if utils.IsMap(v.rules) || utils.IsSlice(v.rules) {59 isStructValidation = true60 }61 if isStructValidation {62 return v.doCheckStruct(ctx, v.data)63 }64 }65 return v.doCheckValue(ctx, doCheckValueInput{66 Name: "",67 Value: v.data,68 Rule: gconv.String(v.rules),69 Messages: v.messages,70 DataRaw: v.assoc,71 DataMap: gconv.Map(v.assoc),72 })73}74// Clone creates and returns a new Validator which is a shallow copy of current one.75func (v *Validator) Clone() *Validator {76 newValidator := New()77 *newValidator = *v78 return newValidator79}80// I18n sets the i18n manager for the validator.81func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator {82 if i18nManager == nil {83 return v84 }85 newValidator := v.Clone()86 newValidator.i18nManager = i18nManager87 return newValidator88}89// Bail sets the mark for stopping validation after the first validation error.90func (v *Validator) Bail() *Validator {91 newValidator := v.Clone()92 newValidator.bail = true93 return newValidator94}95// Ci sets the mark for Case-Insensitive for those rules that need value comparison.96func (v *Validator) Ci() *Validator {97 newValidator := v.Clone()98 newValidator.caseInsensitive = true99 return newValidator100}101// Data is a chaining operation function, which sets validation data for current operation.102func (v *Validator) Data(data interface{}) *Validator {103 if data == nil {104 return v105 }106 newValidator := v.Clone()107 newValidator.data = data108 return newValidator109}110// Assoc is a chaining operation function, which sets associated validation data for current operation.111// The optional parameter `assoc` is usually type of map, which specifies the parameter map used in union validation.112// Calling this function with `assoc` also sets `useDataInsteadOfObjectAttributes` true113func (v *Validator) Assoc(assoc interface{}) *Validator {114 if assoc == nil {115 return v116 }117 newValidator := v.Clone()118 newValidator.assoc = assoc119 newValidator.useDataInsteadOfObjectAttributes = true120 return newValidator121}122// Rules is a chaining operation function, which sets custom validation rules for current operation.123func (v *Validator) Rules(rules interface{}) *Validator {124 if rules == nil {125 return v126 }127 newValidator := v.Clone()128 newValidator.rules = rules129 return newValidator130}131// Messages is a chaining operation function, which sets custom error messages for current operation.132// The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result133// if `rules` is type of []string.134func (v *Validator) Messages(messages interface{}) *Validator {135 if messages == nil {136 return v137 }138 newValidator := v.Clone()139 newValidator.messages = messages140 return newValidator141}142// RuleFunc registers one custom rule function to current Validator.143func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator {144 newValidator := v.Clone()145 newValidator.ruleFuncMap[rule] = f146 return newValidator147}148// RuleFuncMap registers multiple custom rule functions to current Validator.149func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator {150 if m == nil {151 return v152 }153 newValidator := v.Clone()154 for k, v := range m {155 newValidator.ruleFuncMap[k] = v156 }157 return newValidator158}159// getRuleFunc retrieves and returns the custom rule function for specified rule.160func (v *Validator) getRuleFunc(rule string) RuleFunc {161 ruleFunc := v.ruleFuncMap[rule]162 if ruleFunc == nil {163 ruleFunc = customRuleFuncMap[rule]164 }165 return ruleFunc166}...

Full Screen

Full Screen

NewValidator

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 validate := validator.New()4 err := validate.Struct(&User{5 })6 if err != nil {7 fmt.Println(err)8 }9}10import (11func main() {12 validate := validator.New()13 err := validate.Struct(&User{14 })15 if err != nil {16 fmt.Println(err)17 }18}19import (20func main() {21 validate := validator.New()22 err := validate.Struct(&User{23 })24 if err != nil {25 fmt.Println(err)26 }27}28import (29func main() {30 validate := validator.New()31 err := validate.Struct(&User{32 })33 if err != nil {34 fmt.Println(err)35 }36}37import (38func main() {39 validate := validator.New()40 err := validate.Struct(&User{

Full Screen

Full Screen

NewValidator

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter a number: ")4 fmt.Scanln(&num)5 validation.NewValidator(num)6}7import "fmt"8func NewValidator(num int) {9 if num%2 == 0 {10 fmt.Println("Entered number is even")11 } else {12 fmt.Println("Entered number is odd")13 }14}15import "fmt"16func IsPrime(num int) {17 if num == 2 {18 fmt.Println("Entered number is prime")19 } else {20 for i := 2; i < num; i++ {21 if num%i == 0 {22 }23 }

Full Screen

Full Screen

NewValidator

Using AI Code Generation

copy

Full Screen

1func main() {2 fmt.Println("Hello, playground")3 v := validation.NewValidator()4 v.Validate(p)5}6func main() {7 fmt.Println("Hello, playground")8 v := validation.NewValidator()9 v.Validate(p)10}11func main() {12 fmt.Println("Hello, playground")13 v := validation.NewValidator()14 v.Validate(p)15}16func main() {17 fmt.Println("Hello, playground")18 v := validation.NewValidator()19 v.Validate(p)20}21func main() {22 fmt.Println("Hello, playground")23 v := validation.NewValidator()24 v.Validate(p)25}26func main() {27 fmt.Println("Hello, playground")28 v := validation.NewValidator()29 v.Validate(p)30}31func main() {32 fmt.Println("Hello, playground")33 v := validation.NewValidator()34 v.Validate(p)35}36func main() {37 fmt.Println("Hello, playground")38 v := validation.NewValidator()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful