英文:
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:
- json-schema spec for conditions: https://json-schema.org/understanding-json-schema/reference/conditionals.html
答案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,
}
}
我没有直接测试过这个代码,但我相信你能理解。你需要手动找出TransformerConfig
的Ref
是什么。
更新:现在有一个新的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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论