// ValidateStructField validates value based on the tag in the form maxlength=10
495256
func ValidateStructField(value reflect.Value, tag string) (bool, error) {
	re := regexp.MustCompile(`^maxlength=(\d+)$`)
	parts := re.FindStringSubmatch(tag)

	if len(parts) != 2 {
		return false, fmt.Errorf("invalid tag '%s', must be maxlength=n", tag)
	}

	max, _ := strconv.Atoi(parts[1])

	switch value.Kind() {
	case reflect.String:
		return ValidateString(value.String(), max)
	case reflect.Slice:
		l := value.Slice(0, value.Len()).Len()
		if l > max {
			return false, fmt.Errorf("%d values, max allowed %d", l, max)
		}

		return true, nil

	default:
		return false, fmt.Errorf("cannot check length of %s type", value.Kind().String())
	}
}
457705
func (p *parser) parseStruct(st reflect.Value, path []flagNameComponent) (err error) {
	posStarted := false
	foreachStructField(st, func(f reflect.Value, sf reflect.StructField) (stop bool) {
		if !posStarted && f.Type() == reflect.TypeOf(StartPos{}) {
			posStarted = true
			return false
		}
		if f.Type() == reflect.TypeOf(ExcessArgs{}) {
			p.excess = f.Addr().Interface().(*ExcessArgs)
			return false
		}
		if sf.PkgPath != "" {
			return false
		}
		if p.excess != nil {
			err = ErrFieldsAfterExcessArgs
			return true
		}
		if canMarshal(f) {
			if posStarted {
				err = p.addPos(f, sf, path)
			} else {
				err = p.addFlag(f, sf, path)
				if err != nil {
					err = fmt.Errorf("error adding flag in %s: %s", st.Type(), err)
				}
			}
			return err != nil
		}
		if f.Kind() == reflect.Struct {
			if canMarshal(f.Addr()) {
				err = fmt.Errorf("field %q has type %s, did you mean to use %s?", sf.Name, f.Type(), f.Addr().Type())
				return true
			}
			err = p.parseStruct(f, append(path, structFieldFlagNameComponent(sf)))
			return err != nil
		}
		err = fmt.Errorf("field has bad type: %v", f.Type())
		return true
	})
	return
}
291342
func (m *RateLimit_Action_RequestHeaders) Validate() error {
	if m == nil {
		return nil
	}

	if len(m.GetHeaderName()) < 1 {
		return RateLimit_Action_RequestHeadersValidationError{
			field:  "HeaderName",
			reason: "value length must be at least 1 bytes",
		}
	}

	if len(m.GetDescriptorKey()) < 1 {
		return RateLimit_Action_RequestHeadersValidationError{
			field:  "DescriptorKey",
			reason: "value length must be at least 1 bytes",
		}
	}

	return nil
}
1040455
func validateStruct(errors Errors, obj interface{}) Errors {
	typ := reflect.TypeOf(obj)
	val := reflect.ValueOf(obj)

	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		field := typ.Field(i)

		// Allow ignored fields in the struct
		if field.Tag.Get("form") == "-" || !val.Field(i).CanInterface() {
			continue
		}

		fieldVal := val.Field(i)
		fieldValue := fieldVal.Interface()
		zero := reflect.Zero(field.Type).Interface()

		// Validate nested and embedded structs (if pointer, only do so if not nil)
		if field.Type.Kind() == reflect.Struct ||
			(field.Type.Kind() == reflect.Ptr && !reflect.DeepEqual(zero, fieldValue) &&
				field.Type.Elem().Kind() == reflect.Struct) {
			errors = validateStruct(errors, fieldValue)
		}
		errors = validateField(errors, zero, field, fieldVal, fieldValue)
	}
	return errors
}
820447
func ValidateTag(tag string) ([]string, error) {
	ltag := strings.ToLower(tag)
	if !validTagRe.MatchString(ltag) {
		return nil, fmt.Errorf("invalid tag: %q", tag)
	}
	ltags := strings.Split(ltag, ":")
	return ltags, nil
}
496270
func ValidateNode(node *api.Node) validation.ErrorList {
	allErrs := ValidateObjectMeta(&node.ObjectMeta, false, ValidateNodeName, validation.NewFieldPath("metadata"))

	// Only validate spec. All status fields are optional and can be updated later.

	// external ID is required.
	if len(node.Spec.ExternalID) == 0 {
		allErrs = append(allErrs, validation.NewRequiredError(validation.NewFieldPath("spec", "externalID")))
	}

	// TODO(rjnagal): Ignore PodCIDR till its completely implemented.
	return allErrs
}
779949
func Validate(req *http.Request, userStruct FieldMapper) error {
	err := validate(Errors{}, req, userStruct)
	if len(err) > 0 {
		return err
	}

	return nil
}
961620
func ValidateStructField(target interface{}, field string) (bool, error) {
	val := reflect.ValueOf(target)

	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}

	valueField := val.FieldByName(field)
	typeField, ok := val.Type().FieldByName(field)
	if !ok {
		return false, fmt.Errorf("unknown field %s", field)
	}

	validation := strings.TrimSpace(typeField.Tag.Get("validate"))

	err := validateStructField(valueField, typeField, validation)
	if err != nil {
		return false, err
	}

	return true, nil
}
1356261
func Validate(val interface{}) error {
	switch reflect.TypeOf(val).Kind() {
	case reflect.Slice:
		// validate a slice
		err := validateSlice(val)
		return err
	case reflect.Ptr:
		// try to get the valu from the ptr
		v := reflect.ValueOf(val).Elem()
		if v.CanInterface() {
			return Validate(v.Interface())
		}
	case reflect.Struct:
		// when it is a struct jsut validate
		_, err := govalidator.ValidateStruct(val)
		return err
	}
	return nil
}
1166407
func (s *StructStruct) string(n int) string {
	sname := s.Name()
	pad := strings.Repeat(" ", n)

	var res string
	res += fmt.Sprintf("%s%s {\n", pad, sname)

	var fn int
	for _, field := range s.data {
		var n int
		if emb := field.Embedded(); emb != nil {
			n = len(emb.Name())
		} else {
			n = len(field.Name())
		}
		if n > fn {
			fn = n
		}
	}

	f := fmt.Sprintf("%s%%%ds %%T\n", pad, fn+1)
	for _, field := range s.data {
		if emb := field.Embedded(); emb != nil {
			res += emb.string(n + fn)
			continue
		}
		res += fmt.Sprintf(f, field.Name(), field.value.Interface())
	}

	res += fmt.Sprintf("%s}\n", pad)
	return res
}
229307
func (v *LengthRule) Validate(value interface{}) error {
	value, isNil := Indirect(value)
	if isNil || IsEmpty(value) {
		return nil
	}

	var (
		l   int
		err error
	)
	if s, ok := value.(string); ok && v.rune {
		l = utf8.RuneCountInString(s)
	} else if l, err = LengthOfValue(value); err != nil {
		return err
	}

	if v.min > 0 && l < v.min || v.max > 0 && l > v.max {
		return errors.New(v.message)
	}
	return nil
}
371647
func (o *ElapseParams) validateLength(formats strfmt.Registry) error {

	if err := validate.MinimumInt("length", "path", int64(o.Length), 2, false); err != nil {
		return err
	}

	if err := validate.MaximumInt("length", "path", int64(o.Length), 30, false); err != nil {
		return err
	}

	return nil
}
568837
func ReflectStruct(dest interface{}, src interface{}) (err error) {
	if dest == nil || src == nil {
		return
	}

	return reflectField(
		reflect.ValueOf(dest),
		reflect.ValueOf(src),
	)
}
607046
func getStructField(v interface{}, field string) (fieldVal interface{}, exists bool) {
	vv := reflect.ValueOf(v)
	if !vv.IsValid() {
		return nil, false
	}
	if vv.Kind() == reflect.Ptr {
		vv = vv.Elem()
	}
	fv := vv.FieldByName(field)
	if !fv.IsValid() {
		return nil, false
	}
	return fv.Interface(), true
}
597837
func (dec *Decoder) findStructField() error {
	var anon reflect.Value

	num := dec.curr.NumField()
	for i := 0; i < num; i++ {
		field := dec.curr.Type().Field(i)
		if field.Name == dec.field {
			// check if the field's name is equal
			dec.curr = dec.curr.Field(i)
			return nil
		} else if field.Anonymous {
			// if the field is a anonymous struct, then iterate over its fields
			tmp := dec.curr
			dec.curr = dec.curr.FieldByIndex(field.Index)
			if err := dec.findStructField(); err != nil {
				dec.curr = tmp
				continue
			}
			// field in anonymous struct is found,
			// but first it should found the field in the rest of struct
			// (a field with same name in the current struct should have preference over anonymous struct)
			anon = dec.curr
			dec.curr = tmp
		} else if dec.field == field.Tag.Get(dec.opts.TagName) {
			// is not found yet, then retry by its tag name "form"
			dec.curr = dec.curr.Field(i)
			return nil
		}
	}
	if anon.IsValid() {
		dec.curr = anon
		return nil
	}

	return ErrPath{field: dec.field}
}
327475
func (e *RequiredValidator) Validate(s interface{}) error {
	if e.TagName == "" {
		e.TagName = "required"
	}

	if e.TagValue == "" {
		e.TagValue = "true"
	}

	for _, field := range structs.Fields(s) {
		if err := e.processField("", field); err != nil {
			return err
		}
	}

	return nil
}
442344
func convertFromStruct(p interface{}, tagName string) interface{} {
	result := make(map[string]interface{})
	return convertFromStructDeep(result, tagName, toType(p), toValue(p))
}