2017-08-22 10 views
1

billing_addressが必要に応じてリストされていなくても、react-jsonschema-formバリデーターは本質的にshipping_addressbilling_addressの両方を必要とします。これは、addressタイプでは3つのプロパティがすべて必要なためです。 billing_addressをオプションにするにはどうすればよいですか? react-jsonschema-formは、プロパティのすべてが記入されていない場合は、billing_addressを提出しないでください。react-jsonschema-formプレイグラウンドへのlinkがあります。react-jsonschema-formの必須フィールドを持つオプションオブジェクト

{ 
    "definitions": { 
    "address": { 
     "type": "object", 
     "properties": { 
     "street_address": { 
      "type": "string" 
     }, 
     "city": { 
      "type": "string" 
     }, 
     "state": { 
      "type": "string" 
     } 
     }, 
     "required": [ 
     "street_address", 
     "city", 
     "state" 
     ] 
    } 
    }, 
    "type": "object", 
    "properties": { 
    "billing_address": { 
     "title": "Billing address", 
     "$ref": "#/definitions/address" 
    }, 
    "shipping_address": { 
     "title": "Shipping address", 
     "$ref": "#/definitions/address" 
    } 
    }, 
    "required": [ 
    "shipping_address" 
    ] 
} 
+0

、JSONデータに請求先住所を提供していないと、正常に動作します – Pedro

答えて

0

あなたは、請求先住所、条件付きで表示され、必要なを作るためにdynamicスキーマの依存関係を使用することができます。これはオプションのオブジェクトを持つ場合と同じではありませんが、ユーザーエクスペリエンスが多少異なる場合は十分です。 react-jsonschema-formの遊び場にはlinkがあります。私の意見では、ライブ検証は無効になっています(ページの右上にチェックボックスがあります)。

あなたが例えばこのhttp://www.jsonschemavalidator.net/をしようとした場合、そのバリデータに問題があるように思わ
{ 
    "definitions": { 
    "address": { 
     "type": "object", 
     "properties": { 
     "street_address": { 
      "type": "string" 
     }, 
     "city": { 
      "type": "string" 
     }, 
     "state": { 
      "type": "string" 
     } 
     }, 
     "required": [ 
     "street_address", 
     "city", 
     "state" 
     ] 
    } 
    }, 
    "type": "object", 
    "properties": { 
    "different_addresses": { 
     "title": "My billing address is different than my shipping address.", 
     "type": "boolean", 
     "default": false 
    }, 
    "shipping_address": { 
     "title": "Shipping address", 
     "$ref": "#/definitions/address" 
    } 
    }, 
    "required": [ 
    "shipping_address" 
    ], 
    "dependencies": { 
    "different_addresses": { 
     "oneOf": [ 
     { 
      "properties": { 
      "different_addresses": { 
       "enum": [ 
       false 
       ] 
      } 
      } 
     }, 
     { 
      "properties": { 
      "different_addresses": { 
       "enum": [ 
       true 
       ] 
      }, 
      "billing_address": { 
       "title": "Billing address", 
       "$ref": "#/definitions/address" 
      } 
      }, 
      "required": [ 
      "billing_address" 
      ] 
     } 
     ] 
    } 
    } 
} 
関連する問題