drmgg4411
2019-02-01 13:07
浏览 62
已采纳

如何为一个端点创建多种验证方法?

I want to make a validation api in order to validate a set of json requests regarding specific set of rules. To do that I want to use just one endpoint and call functions that correspond to the specific json struct. I know that there is no method overloading in go so I am kind of stumped.

...

type requestBodyA struct {
    SomeField   string `json:"someField"`
    SomeOtherField  string `json:"someOtherField"`
}

type requestBodyB struct {
    SomeDifferentField   string `json:"someDifferentField"`
    SomeOtherDifferentField  string `json:"someOtherDifferentField"`
}



type ValidationService interface {
    ValidateRequest(ctx context.Context, s string) (err error)
}

type basicValidationService struct{}

...

So in order to validate lots of different json requests, is it better to create structs for each and every json request? Or should I create these dynamically? How can I know what kind of request is sent if I only have one endpoint?

图片转代码服务由CSDN问答提供 功能建议

我想制作一个验证api来验证一组与特定规则有关的json请求。 为此,我只想使用一个端点并调用与特定json结构相对应的函数。 我知道go中没有方法重载,所以我有点受挫。

  ... 
 
type requestBodyA struct {
 SomeField string`json:“ someField  “`
 SomeOtherField字符串`json:” someOtherField“`
} 
 
type requestBodyB struct {
 SomeDifferentField字符串`json:” someDifferentField“`
 SomeOtherDifferentField字符串json:” someOtherDifferentField“`
} 
  
 
 
type ValidationService接口{
 ValidateRequest(ctx context.Context,s字符串)(错误错误)
} 
 
type basicValidationService struct {} 
 
 ... 
  <  / pre> 
 
 

因此,为了验证许多不同的json请求,为每个json请求创建结构更好吗? 还是应该动态创建这些? 如果我只有一个端点,我怎么知道发送什么样的请求?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题