2017-01-24 15 views
1

2つのプロパティのセットは常にオプションですが、別の(常に必須の)ブール値の値が本当。oneOf(v4またはv5)を使用するJSONスキーマの重複を削除

常にオプションですが、常に許可されるわけではないプロパティは、名前がmax_recurrencesrecurrence_argumentsです。依存する値がtrueのブール値プロパティの名前は、recurringです。

私は以下のスキームを思いついたと思いますが、それはoneOf配列の各項目の他のすべてのプロパティを複製しています。私はこの重複を避ける方法を探しています。

{ 
    "id": "plan_schedule", 
    "type": "object", 
    "oneOf": [ 
    { 
     "properties": { 
     "start_date": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "end_date": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "trigger": { 
      "$ref": "re_non_empty_string" 
     }, 
     "arguments": { 
      "type": "object", 
      "minProperties": 1 
     }, 
     "recurring": { 
      "type": "boolean", 
      "enum": [true], 
     }, 
     "max_recurrences": { 
      "type": "integer", 
      "minimum": 1 
     }, 
     "recurrence_arguments": { 
      "type": "object", 
      "minProperties": 1 
     } 
     } 
    }, 
    { 
     "properties": { 
     "start_date": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "end_date": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "trigger": { 
      "$ref": "re_non_empty_string" 
     }, 
     "arguments": { 
      "type": "object", 
      "minProperties": 1 
     }, 
     "recurring": { 
      "type": "boolean", 
      "enum": [false], 
     }, 
     } 
    } 
    ], 
    "additionalProperties": false, 
    "required": ["start_date", "trigger", "recurring"] 
} 

誰でもお手伝いできますか?私はv4を使いたいと思っていますが、役に立つならばv5を使うことができます。さらに明確にする

、私はプロパティのみを一覧表示する必要があることを望んだ:スキーマ全体でstart_dateend_datetriggerarguments 1時間。

答えて

1

JSON Schemaの草案-04:

{ 
    "type": "object", 
    "properties": { 
    "recurring": { 
     "type": "boolean" 
    } 
    // all other properties 
    } 
    "additionalProperties": false, 
    "required": ["start_date", "trigger", "recurring"] 
    "anyOf": [ 
    { 
     "properties": { "recurring": { "enum": [true] } } 
    }, 
    { 
     "properties": { "recurring": { "enum": [false] } }, 
     "not": { 
     "anyOf": [ 
      { "required": ["max_recurrences"] }, 
      { "required": ["recurrence_arguments"] } 
     } 
     } 
    } 
    ] 
} 

あなたは(V5はどこにも使用されていない概念ですので、私はそうと仮定)AJVを使用している場合は、カスタムキーワードを使用して、上記簡素化することができます「その後、/他/ IF」ドラフト07のために提案され、何らかのサポートがある "禁止された" - それはajv-keywordsで定義されています。 「anyOfは」に置き換えることができます:

"if": { "properties": { "recurring": { "enum": [false] } } }, 
"then": { "prohibited": ["max_recurrences", "recurrence_arguments"] } 

EDIT:

実際には、任意のカスタムキーワードを指定せずに「依存関係」キーワードでさらに簡単に行うことができます。代わりに「anyOf」の:

"dependencies": { 
    "max_recurrences": { "$ref": "#recurring" }, 
    "recurrence_arguments": { "$ref": "#recurring" } 
}, 
"definitions": { 
    "recurring": { 
    "id": "#recurring", 
    "properties": { 
     "recurring": { "enum": [true] } 
    } 
    } 
} 
+1

このスキーマは** recurring' 'の値に基づいて' recurrence_arguments'と 'max_recurrences' **が要求されるべきかどうかを決定されたように見えます。それは実際に私が探しているものではありません。フィールドは決して必要ではありませんが、 'recurring'が' true'でなければ許されません。 – LukeP

+0

どのオプションがありますか? – esp

+0

あなたの編集は実際には私が望むように見えますが、 'recurring'が真の場合に必要なフィールドを作るようなトップ2の外観になります。私は間違っていますか? – LukeP

関連する問題