• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang schema.Validator类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/rs/rest-layer/schema.Validator的典型用法代码示例。如果您正苦于以下问题:Golang Validator类的具体用法?Golang Validator怎么用?Golang Validator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Validator类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: SetSort

// SetSort parses and validate a sort parameter and set it as lookup's Sort
func (l *Lookup) SetSort(sort string, validator schema.Validator) error {
	sorts := []string{}
	for _, f := range strings.Split(sort, ",") {
		f = strings.Trim(f, " ")
		if f == "" {
			return errors.New("empty soft field")
		}
		// If the field start with - (to indicate descended sort), shift it before
		// validator lookup
		i := 0
		if f[0] == '-' {
			i = 1
		}
		// Make sure the field exists
		field := validator.GetField(f[i:])
		if field == nil {
			return fmt.Errorf("invalid sort field: %s", f[i:])
		}
		if !field.Sortable {
			return fmt.Errorf("field is not sortable: %s", f[i:])
		}
		sorts = append(sorts, f)
	}
	l.sort = sorts
	return nil
}
开发者ID:apuigsech,项目名称:rest-layer,代码行数:27,代码来源:lookup.go


示例2: checkReferences

// checkReferences checks that fields with the Reference validator reference an existing object
func (r *requestHandler) checkReferences(payload map[string]interface{}, s schema.Validator) *Error {
	for name, value := range payload {
		field := s.GetField(name)
		if field == nil {
			continue
		}
		// Check reference if validator is of type Reference
		if field.Validator != nil {
			if ref, ok := field.Validator.(*schema.Reference); ok {
				resource := r.h.getResource(ref.Path)
				if resource == nil {
					return &Error{500, fmt.Sprintf("Invalid resource reference for field `%s': %s", name, ref.Path), nil}
				}
				lookup := NewLookup()
				lookup.Fields["id"] = value
				list, _ := resource.handler.Find(lookup, 1, 1)
				if len(list.Items) == 0 {
					return &Error{404, fmt.Sprintf("Resource reference not found for field `%s'", name), nil}
				}
			}
		}
		// Check sub-schema if any
		if field.Schema != nil && value != nil {
			if subPayload, ok := value.(map[string]interface{}); ok {
				if err := r.checkReferences(subPayload, field.Schema); err != nil {
					return err
				}
			}
		}
	}
	return nil
}
开发者ID:yetone,项目名称:rest-layer,代码行数:33,代码来源:request.go


示例3: applySelector

func applySelector(s []Field, v schema.Validator, p map[string]interface{}, resolver ReferenceResolver) (map[string]interface{}, error) {
	res := map[string]interface{}{}
	for _, f := range s {
		if val, found := p[f.Name]; found {
			name := f.Name
			// Handle aliasing
			if f.Alias != "" {
				name = f.Alias
			}
			// Handle selector params
			if len(f.Params) > 0 {
				def := v.GetField(f.Name)
				if def == nil || def.Params == nil {
					return nil, fmt.Errorf("%s: params not allowed", f.Name)
				}
				var err error
				val, err = def.Params.Handler(val, f.Params)
				if err != nil {
					return nil, fmt.Errorf("%s: %s", f.Name, err.Error())
				}
			}
			// Handle sub field selection (if field has a value)
			if len(f.Fields) > 0 && val != nil {
				def := v.GetField(f.Name)
				if def != nil && def.Schema != nil {
					subval, ok := val.(map[string]interface{})
					if !ok {
						return nil, fmt.Errorf("%s: invalid value: not a dict", f.Name)
					}
					var err error
					res[name], err = applySelector(f.Fields, def.Schema, subval, resolver)
					if err != nil {
						return nil, fmt.Errorf("%s.%s", f.Name, err.Error())
					}
				} else if ref, ok := def.Validator.(*schema.Reference); ok {
					// Sub-field on a reference (sub-request)
					subres, subval, err := resolver(ref.Path, val)
					if err != nil {
						return nil, fmt.Errorf("%s: error fetching sub-field: %s", f.Name, err.Error())
					}
					res[name], err = applySelector(f.Fields, subres.validator, subval, resolver)
				} else {
					return nil, fmt.Errorf("%s: field as no children", f.Name)
				}
			} else {
				res[name] = val
			}
		}
	}
	return res, nil
}
开发者ID:apuigsech,项目名称:rest-layer,代码行数:51,代码来源:selector.go


示例4: validateSelector

func validateSelector(s []Field, v schema.Validator) error {
	for _, f := range s {
		def := v.GetField(f.Name)
		if def == nil {
			return fmt.Errorf("%s: unknown field", f.Name)
		}
		if len(f.Fields) > 0 {
			if def.Schema != nil {
				// Sub-field on a dict (sub-schema)
				if err := validateSelector(f.Fields, def.Schema); err != nil {
					return fmt.Errorf("%s.%s", f.Name, err.Error())
				}
			} else if _, ok := def.Validator.(*schema.Reference); ok {
				// Sub-field on a reference (sub-request)
			} else {
				return fmt.Errorf("%s: field as no children", f.Name)
			}
		}
		// TODO: support connections
		if len(f.Params) > 0 {
			if def.Params == nil {
				return fmt.Errorf("%s: params not allowed", f.Name)
			}
			for param, value := range f.Params {
				val, found := def.Params.Validators[param]
				if !found {
					return fmt.Errorf("%s: unsupported param name: %s", f.Name, param)
				}
				value, err := val.Validate(value)
				if err != nil {
					return fmt.Errorf("%s: invalid param `%s' value: %s", f.Name, param, err.Error())
				}
				f.Params[param] = value
			}
		}
	}
	return nil
}
开发者ID:apuigsech,项目名称:rest-layer,代码行数:38,代码来源:selector.go


示例5: checkReferences

// checkReferences ensures that fields with the Reference validator reference an existing object
func checkReferences(ctx context.Context, payload map[string]interface{}, s schema.Validator) *Error {
	for name, value := range payload {
		field := s.GetField(name)
		if field == nil {
			continue
		}
		// Check reference if validator is of type Reference
		if field.Validator != nil {
			if ref, ok := field.Validator.(*schema.Reference); ok {
				router, ok := IndexFromContext(ctx)
				if !ok {
					return &Error{500, "Router not available in context", nil}
				}
				rsrc, found := router.GetResource(ref.Path, nil)
				if !found {
					return &Error{500, fmt.Sprintf("Invalid resource reference for field `%s': %s", name, ref.Path), nil}
				}
				_, err := rsrc.Get(ctx, value)
				if err == resource.ErrNotFound {
					return &Error{404, fmt.Sprintf("Resource reference not found for field `%s'", name), nil}
				} else if err != nil {
					return &Error{500, fmt.Sprintf("Error fetching resource reference for field `%s': %v", name, err), nil}
				}
			}
		}
		// Check sub-schema if any
		if field.Schema != nil && value != nil {
			if subPayload, ok := value.(map[string]interface{}); ok {
				if err := checkReferences(ctx, subPayload, field.Schema); err != nil {
					return err
				}
			}
		}
	}
	return nil
}
开发者ID:rs,项目名称:rest-layer,代码行数:37,代码来源:util.go


示例6: checkReferences

// checkReferences checks that fields with the Reference validator reference an existing object
func (r *request) checkReferences(ctx context.Context, payload map[string]interface{}, s schema.Validator) *Error {
	for name, value := range payload {
		field := s.GetField(name)
		if field == nil {
			continue
		}
		// Check reference if validator is of type Reference
		if field.Validator != nil {
			if ref, ok := field.Validator.(*schema.Reference); ok {
				router, ok := IndexFromContext(ctx)
				if !ok {
					return &Error{500, "Router not available in context", nil}
				}
				rsrc, _, found := router.GetResource(ref.Path)
				if !found {
					return &Error{500, fmt.Sprintf("Invalid resource reference for field `%s': %s", name, ref.Path), nil}
				}
				l := resource.NewLookup()
				l.AddQuery(schema.Query{schema.Equal{Field: "id", Value: value}})
				list, _ := rsrc.Find(ctx, l, 1, 1)
				if len(list.Items) == 0 {
					return &Error{404, fmt.Sprintf("Resource reference not found for field `%s'", name), nil}
				}
			}
		}
		// Check sub-schema if any
		if field.Schema != nil && value != nil {
			if subPayload, ok := value.(map[string]interface{}); ok {
				if err := r.checkReferences(ctx, subPayload, field.Schema); err != nil {
					return err
				}
			}
		}
	}
	return nil
}
开发者ID:C8E,项目名称:rest-layer,代码行数:37,代码来源:request.go


示例7: validateFilter

// validateFilter recursively validates the format of a filter query
func validateFilter(f map[string]interface{}, validator schema.Validator, parentKey string) error {
	for key, exp := range f {
		switch key {
		case "$ne", "$gt", "$gte", "$lt", "$lte":
			op := key
			if parentKey == "" {
				return fmt.Errorf("%s can't be at first level", op)
			}
			if _, ok := isNumber(exp); !ok {
				return fmt.Errorf("%s: value for %s must be a number", parentKey, op)
			}
			if field := validator.GetField(parentKey); field != nil {
				if field.Validator != nil {
					switch field.Validator.(type) {
					case schema.Integer, schema.Float:
						// Ok
					default:
						return fmt.Errorf("%s: cannot apply %s operation on a non numerical field", parentKey, op)
					}
				}
			}
		case "$in", "$nin":
			op := key
			if parentKey == "" {
				return fmt.Errorf("%s can't be at first level", op)
			}
			if _, ok := exp.(map[string]interface{}); ok {
				return fmt.Errorf("%s: value for %s can't be a dict", parentKey, op)
			}
		case "$or":
			var subFilters []interface{}
			var ok bool
			if subFilters, ok = exp.([]interface{}); !ok {
				return fmt.Errorf("%s: value for $or must be an array of dicts", parentKey)
			}
			if len(subFilters) < 2 {
				return fmt.Errorf("%s: $or must contain at least to elements", parentKey)
			}
			for _, subFilter := range subFilters {
				if sf, ok := subFilter.(map[string]interface{}); !ok {
					return fmt.Errorf("%s: value for $or must be an array of dicts", parentKey)
				} else if err := validateFilter(sf, validator, ""); err != nil {
					return err
				}
			}
		default:
			// Exact match
			if subFilter, ok := exp.(map[string]interface{}); ok {
				if parentKey != "" {
					return fmt.Errorf("%s: invalid expression", parentKey)
				}
				if err := validateFilter(subFilter, validator, key); err != nil {
					return err
				}
			} else {
				if field := validator.GetField(key); field != nil {
					if field.Validator != nil {
						if _, err := field.Validator.Validate(exp); err != nil {
							return fmt.Errorf("invalid filter expression for field `%s': %s", key, err)
						}
					}
				} else {
					return fmt.Errorf("unknown filter field: %s", key)
				}
			}
		}
	}
	return nil
}
开发者ID:yetone,项目名称:rest-layer,代码行数:70,代码来源:lookup.go



注:本文中的github.com/rs/rest-layer/schema.Validator类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang xhandler.HandlerFuncC函数代码示例发布时间:2022-05-28
下一篇:
Golang resource.Resource类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap