Golang中使用if/then/else的invopop jsonschema用法

huangapple go评论89阅读模式
英文:

Golang invopop jsonschema usage of if/then/else

问题

我正在使用invopop/jsonschema库根据go struct标签生成我们的json-schema。但是我在如何使用if/then/else属性方面遇到了困难。

我之前是这样做的:

type Boulou struct {
	Name              string                   `json:"name" jsonschema:"required,minLength=1,description=unique name"`
	Transformers      []TransformerConfig `json:"transformers" jsonschema:"title=transformers,if=properties.kind.const=convert_swim,then=required[0]=convert_swim_config"`
}

但似乎不起作用(如果你想尝试一下,我在go playground上创建了一个示例)。

提前感谢!

资源:

  • 条件的json-schema规范:https://json-schema.org/understanding-json-schema/reference/conditionals.html
英文:

I'm using the library invopop/jsonschema for generating our json-schema based on go struct tags. But I struggle on how to use the if/then/else attributes.

I was doing something like this

type Boulou struct {
	Name              string                   `json:"name" jsonschema:"required,minLength=1,description=unique name"`
	Transformers      []TransformerConfig `json:"transformers" jsonschema:"title=transformers,if=properties.kind.const=convert_swim,then=required[0]=convert_swim_config"`
}

But does not seems to work (i made a go playground if you would like to play with).

Thanks in advance !

Resources:

答案1

得分: 2

这些复杂的用例在invopop/jsonschema中使用Go标签的支持并不好。对于任何超出常规用例的情况,我建议实现JSONSchema()方法,这样你就可以手动定义对象。按照你的示例:

type Boulou struct {
    Name              string              `json:"name"`
    Transformers      []TransformerConfig `json:"transformers"`
}

func (Boulou) JSONSchema() *jsonschema.Schema {
  props := orderedmap.New()
  props.Set("name", &jsonschema.Schema{
    Type:  "string",
    Title: "Name",
  })
  props.Set("transformers", &jsonschema.Schema{
    Type: "array",
    Title: "Transformers",
    Items: &jsonschema.Schema{
      Ref:  ".....",
      If:   "properties.kind.const=convert_swim",
      Then: "required[0]=convert_swim_config",
    },
  })
  return &jsonschema.Schema{
    Type:       "object",
    Title:      "Boulou",
    Properties: props,
  }
}

我没有直接测试过这个代码,但我相信你能理解。你需要手动找出TransformerConfigRef是什么。

更新:现在有一个新的PR #52,一旦发布,应该会更容易实现这个功能!

英文:

These complex use-cases aren't supported all that well using Go tags in invopop/jsonschema. Anything that breaks out of regular use-cases I'd recommend implementing the JSONSchema() method so you can define the object manually. Following your example:

type Boulou struct {
    Name              string              `json:"name"`
    Transformers      []TransformerConfig `json:"transformers"`
}

func (Boulou) JSONSchema() *jsonschema.Schema {
  props = orderedmap.New()
  props.Set("name", &jsonschema.Schema{
    Type: "string",
    Title: "Name",
  })
  props.Set("transformers", &jsonschema.Schema{
    Type: "array",
    Title: "Transformers",
    Items: &jsonschema.Schema{
      Ref:  ".....",
      If:   "properties.kind.const=convert_swim",
      Then: "required[0]=convert_swim_config",
    },
  })
  return &jsonschema.Schema{
    Type:       "object",
    Title:      "Boulou",
    Properties: props,
  }
}

I haven't tested this directly, but I'm sure you get the idea. You'll need to figure out what the Ref for your TransformerConfig is manually.

Update: there is now a new PR #52 which once launched, should make this much easier to do!

huangapple
  • 本文由 发表于 2022年9月28日 17:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/73879302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定