海印网
海印网

在 Go Huma 中添加过滤查询参数

admin数码00

据我所知,不幸的是,huma 不支持这样的数组查询过滤器:filters[]=filter1&filters[]=filter2(也不保留括号,例如filter=filter1&filter=filter2)。我遇到了这个 github 问题,它给出了一个用逗号 https://github.com/danielgtaylor/huma/issues/325, 分隔过滤器的示例,所以这就是我们最终所做的:filters=postcode:eq:rm7(ex,created:gt:2024-01-01

记录过滤器

与主体参数不同,主体参数可以简单地指定为结构,然后在文档中对其进行验证和生成,过滤器的文档和验证必须单独完成。

文档可以简单地添加到 huma.param 对象的描述属性下(在操作下):

parameters: []*huma.param{{
            name: "filters",
            in:   "query",
            description: "filter properties by various fields. separate filters by comma.

" +
                "format: field:operator:value

" +
                "supported fields:
" +
                "- postcode (operator: eq)
" +
                "- created (operators: gt, lt, gte, lte)
",
            schema: &huma.schema{
                type: "string",
                items: &huma.schema{
                    type:    "string",
                    pattern: "^[a-za-z_]+:(eq|neq|gt|lt|gte|lte):[a-za-z0-9-:.]+$",
                },
                examples: []any{
                    "postcode:eq:rm7 8ex",
                    "created:gt:2024-01-01",
                },
            },
            required: false,
        }},

登录后复制

在 Go Huma 中添加过滤查询参数-第1张图片-海印网

我们现在可以定义 propertyfilterparams 结构进行验证:

type filterparam struct {
    field    string
    operator string
    value    interface{}
}

type propertyfilterparams struct {
    items []filterparam
}

func (s *propertyfilterparams) unmarshaltext(text []byte) error {
    equalityfields := []string{"postcode"}
    greatersmallerfields := []string{}
    datefields := []string{"created"}

    for _, item := range strings.split(string(text), ",") {
        filterparam, err := parseandvalidatefilteritem(item, equalityfields, greatersmallerfields, datefields)
        if err != nil {
            return err
        }
        s.items = append(s.items, filterparam)
    }

    return nil
}

func (s *propertyfilterparams) schema(registry huma.registry) *huma.schema {
    return &huma.schema{
        type: huma.typestring,
    }
}

func parseandvalidatefilteritem(item string, equalityfields []string, greatersmallerfields []string, datefields []string) (filterparam, error) {
    parts := strings.splitn(item, ":", 3)

    field := parts[0]
    operator := parts[1]
    value := parts[2]

    if contains(equalityfields, field) {
        if operator != "eq" && operator != "neq" {
            return filterparam{}, fmt.errorf("unsupported operator %s for field %s. only 'eq' and 'neq' are supported.", operator, field)
        }
    } else if contains(greatersmallerfields, field) {
        if !validation.isvalidcomparegreatersmalleroperator(operator) {
            return filterparam{}, fmt.errorf("unsupported operator %s for field %s. supported operators: eq, neq, gt, lt, gte, lte.", operator, field)
        }
    } else if contains(datefields, field) {
        if !validation.isvalidcomparegreatersmalleroperator(operator) {
            return filterparam{}, fmt.errorf("unsupported operator %s for field %s. supported operators: eq, neq, gt, lt, gte, lte.", operator, field)
        }
        if !validation.isvaliddate(value) {
            return filterparam{}, fmt.errorf("invalid date format: %s. expected: yyyy-mm-dd", value)
        }
    } else {
        return filterparam{}, fmt.errorf("unsupported filter field: %s", field)
    }

    return filterparam{field: field, operator: operator, value: value}, nil
}

登录后复制

我将 propertyfilterparams 添加到 propertyqueryparams 结构中:

type propertyqueryparams struct {
    paginationparams
    filter propertyfilterparams `query:"filters" doc:"filter properties by various fields"`
    sort   propertysortparams   `query:"sorts" doc:"sort properties by various fields"`
}

登录后复制

这就是将 propertyqueryparams 添加到路由的样子(请注意,操作代码本身,包括过滤器描述,位于 getallpropertyoperation 下 - 我没有粘贴完整的代码,但希望您能理解它的要点) 。如果验证失败,它将抛出 422 响应。我还添加了如何循环遍历通过的过滤器值:

huma.Register(api, getAllPropertyOperation(schema, "get-properties", "/properties", []string{"Properties"}),
        func(ctx context.Context, input *struct {
            models.Headers
            models.PropertyQueryParams
        }) (*models.MultiplePropertyOutput, error) {

            for _, filter := range input.Filter.Items {
                fmt.Println(filter)
            }

            return mockMultiplePropertyResponse(), err
        })
}

登录后复制

我希望这对某人有帮助。如果您找到更好的解决方案,请在评论中告诉我。

以上就是在 Go Huma 中添加过滤查询参数的详细内容,更多请关注其它相关文章!

Tags: 过滤器参数

Sorry, comments are temporarily closed!