亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號