为了账号安全,请及时绑定邮箱和手机立即绑定

Golang invopop jsonschema if/then/else 用法

Golang invopop jsonschema if/then/else 用法

Go
不负相思意 2023-02-21 12:56:18
我正在使用库 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"`}但似乎不起作用(如果你想玩的话,我做了一个围棋游乐场)。提前致谢 !资源:条件的 json-schema 规范:https ://json-schema.org/understanding-json-schema/reference/conditionals.html
查看完整描述

1 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

在 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,

  }

}

我没有直接测试过这个,但我相信你明白了。您需要手动弄清楚Ref您的TransformerConfig是什么。


更新:现在有一个新的PR #52,一旦发布,应该会更容易做到!



查看完整回答
反对 回复 2023-02-21
  • 1 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信