您正在查看 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info

關於 OASValidation 政策
您可以使用 OASValidation (OpenAPI 規格驗證) 政策,根據 OpenAPI 3.0 規格 (JSON 或 YAML) 驗證傳入的要求或回應訊息。請參閱「哪些內容會經過驗證?」一文。
當附加政策的步驟執行時,OASValidation 政策會指定要用於驗證的 OpenAPI 規格名稱。OpenAPI 規格會儲存為資源,並位於 API Proxy 套件中的以下標準位置:apiproxy/resources/oas
。OpenAPI 規範必須包含 .json
、.yml
、.yaml
擴充功能。
使用 UI 或 API,將 OpenAPI 規範新增為 API 代理程式套件的資源,如「管理資源」一文所述。
哪些內容會經過驗證?
下表依元件分類,概述由 OASValidation 政策驗證的要求訊息內容。
元件 | 提出驗證要求 |
---|---|
Basepath | 驗證 API Proxy 定義的 basepath,忽略 OpenAPI 規格中指定的 basepath。 |
路徑 | 驗證要求路徑 (不含 basepath) 是否符合 OpenAPI 規格中定義的路徑模式。 |
動詞 | 驗證 OpenAPI 規格中路徑的動詞是否已定義。 |
要求訊息主體 |
注意:只有在 Content-Type 設為 |
參數 |
|
下表依元件分類,摘要列出 OASValidation 政策驗證的回應訊息內容。
元件 | 回應驗證 |
---|---|
路徑 | 驗證要求路徑 (不含 basepath) 是否符合 OpenAPI 規格中定義的路徑模式。 |
動詞 | 驗證 OpenAPI 規格中路徑的動詞是否已定義。 |
回應訊息主體 |
|
範例
以下範例說明瞭您可以使用 OASValidation 政策驗證訊息是否符合 OpenAPI 3.0 規範的幾種方式。
驗證要求訊息
在以下範例中,myoaspolicy
政策會根據 my-spec.json
OpenAPI 規格中定義的作業要求訊息內容結構定義,驗證要求訊息的內容。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.json</OASResource> <Options> <ValidateMessageBody>true</ValidateMessageBody> </Options> <Source>request</Source> </OASValidation>
如果訊息主體不符合 OpenAPI 規範,系統會傳回 policies.oasvalidation.Failed
錯誤。
驗證參數
以下範例會將政策設為在要求中指定的標頭、查詢或 Cookie 參數未在 OpenAPI 規格中定義時失敗。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <AllowUnspecifiedParameters> <Header>false</Header> <Query>false</Query> <Cookie>false</Cookie> </AllowUnspecifiedParameters> </Options> </OASValidation>
<OASValidation>
元素
定義 OpenAPI 規格驗證政策。
預設值 | 請參閱下方的「預設政策」分頁 |
是否必要? | 必填 |
類型 | 複雜物件 |
上層元素 | n/a |
子元素 |
<DisplayName> <OASResource> <Source> <Options> <Source> |
語法
<OASValidation>
元素使用以下語法:
<OASValidation continueOnError="[true|false]" enabled="[true|false]" name="policy_name" > <!-- All OASValidation child elements are optional except OASResource --> <DisplayName>policy_display_name</DisplayName> <OASResource>validation_JSON_or_YAML</OASResource> <Options> <ValidateMessageBody>[true|false]</ValidateMessageBody> <AllowUnspecifiedParameters> <Header>[true|false]</Header> <Query>[true|false]</Query> <Cookie>[true|false]</Cookie> </AllowUnspecifiedParameters> </Options> <Source>message_to_validate</Source> </OASValidation>
預設政策
以下範例顯示在 Apigee UI 中,將 OAS 驗證政策新增至流程時的預設設定:
<OASValidation continueOnError="false" enabled="true" name="OpenAPI-Spec-Validation-1"> <DisplayName>OpenAPI Spec Validation-1</DisplayName> <Properties/> <Source>request</Source> <OASResource>oas://OpenAPI-Spec-Validation-1.yaml</OASResource> </OASValidation>
This element has the following attributes that are common to all policies:
Attribute | Default | Required? | Description |
---|---|---|---|
name |
N/A | Required |
The internal name of the policy. The value of the Optionally, use the |
continueOnError |
false | Optional | Set to "false" to return an error when a policy fails. This is expected behavior for most policies. Set to "true" to have flow execution continue even after a policy fails. |
enabled |
true | Optional | Set to "true" to enforce the policy. Set to "false" to "turn off" the policy. The policy will not be enforced even if it remains attached to a flow. |
async |
false | Deprecated | This attribute is deprecated. |
子元素參照
本節將說明 <OASValidation>
的子元素。
<DisplayName>
Use in addition to the name
attribute to label the policy in the
management UI proxy editor with a different, more natural-sounding name.
The <DisplayName>
element is common to all policies.
Default Value | n/a |
Required? | Optional. If you omit <DisplayName> , the value of the
policy's name attribute is used |
Type | String |
Parent Element | <PolicyElement> |
Child Elements | None |
The <DisplayName>
element uses the following syntax:
Syntax
<PolicyElement> <DisplayName>policy_display_name</DisplayName> ... </PolicyElement>
Example
<PolicyElement> <DisplayName>My Validation Policy</DisplayName> </PolicyElement>
The <DisplayName>
element has no attributes or child elements.
<OASResource>
指定要驗證的 OpenAPI 規範。您可以儲存這個檔案:
- 在 API Proxy 套件中
/apiproxy/resources/oas
下的 API Proxy 範圍 - 在 API 代理程式編輯器的 Navigator 檢視畫面中的
Resources
部分。
詳情請參閱「管理資源」。
您可以使用訊息範本 (例如 {oas.resource.url}
) 指定 OpenAPI 規範。在這種情況下,流程變數 oas.resource.url
的值 (在大括號中) 會在執行階段評估並替換至酬載字串。詳情請參閱「訊息範本」。
預設值 | 無 |
是否必要? | 必填 |
類型 | 字串 |
上層元素 |
<OASValidation>
|
子元素 | 無 |
語法
<OASResource>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> ... </OASValidation>
範例
以下範例參照 API 代理程式套件中 /apiproxy/resources/oas
底下的 my-spec.yaml
規格:
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> </OASValidation>
<OASResource>
元素沒有屬性或子項元素。
<選項>
設定政策的選項。
預設值 | 不適用 |
是否必要? | 選用 |
類型 | 複雜類型 |
上層元素 |
<OASValidation>
|
子元素 |
<ValidateMessageBody> <AllowUnspecifiedParameters> |
語法
<Options>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Options> <ValidateMessageBody>[true|false]</ValidateMessageBody> <AllowUnspecifiedParameters> <Header>[true|false]</Header> <Query>[true|false]</Query> <Cookie>[true|false]</Cookie> </AllowUnspecifiedParameters> </Options> ... </OASValidation>
範例
以下範例說明如何設定政策選項。以下將詳細說明各個選項。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <ValidateMessageBody>false</ValidateMessageBody> <AllowUnspecifiedParameters> <Header>false</Header> <Query>false</Query> <Cookie>false</Cookie> </AllowUnspecifiedParameters> </Options> </OASValidation>
<ValidateMessageBody>
指定政策應根據 OpenAPI 規格中的作業要求主體結構定義,驗證訊息主體。設為 true 即可驗證郵件主旨內容。將其設為 false,只驗證郵件主旨是否存在。
您可以將 <OASValidation>
元素的 continueOnError
屬性設為 true,藉此控制流程在發生驗證錯誤後是否繼續執行。
預設值 | false |
是否必要? | 選用 |
類型 | 布林值 |
上層元素 |
<Options>
|
子元素 | 無 |
語法
<ValidateMessageBody>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Options> <ValidateMessageBody>[true|false]</ValidateMessageBody> </Options> ... </OASValidation>
範例
以下範例會啟用訊息主體內容的驗證功能:
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <ValidateMessageBody>true</ValidateMessageBody> </Options> </OASValidation>
<AllowUnspecifiedParameters>
如果要求中含有 OpenAPI 規格未定義的標頭、查詢或 Cookie 參數,則可設定政策的行為。
預設值 | 不適用 |
是否必要? | 選用 |
類型 | 複雜類型 |
上層元素 |
<Options>
|
子元素 |
<Header> <Query> <Cookie> |
語法
<AllowUnspecifiedParameters>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Options> <AllowUnspecifiedParameters> <Header>[true|false]</Header> <Query>[true|false]</Query> <Cookie>[true|false]</Cookie> </AllowUnspecifiedParameters> </Options> ... </OASValidation>
範例
以下範例會將政策設為在要求中指定的標頭、查詢或 Cookie 參數未在 OpenAPI 規格中定義時失敗。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <AllowUnspecifiedParameters> <Header>false</Header> <Query>false</Query> <Cookie>false</Cookie> </AllowUnspecifiedParameters> </Options> </OASValidation>
<Header>
(<AllowUnspecifiedParameters>
的子項)
如果要求中含有 OpenAPI 規格中未定義的標頭參數,則可設定政策的行為。
如要在要求中指定未在 OpenAPI 規格中定義的標頭參數,請將此參數設為 true。否則,請將這個參數設為 false,以免政策執行失敗。
預設值 | true |
是否必要? | 布林值 |
類型 | 複雜類型 |
上層元素 |
<AllowUnspecifiedParameters>
|
子元素 | 無 |
語法
<Header>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Options> <AllowUnspecifiedParameters> <Header>[true|false]</Header> </AllowUnspecifiedParameters> </Options> ... </OASValidation>
範例
以下範例會將政策設為在要求中指定標頭參數時失敗,但該參數並未在 OpenAPI 規格中定義。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <AllowUnspecifiedParameters> <Header>false</Header> </AllowUnspecifiedParameters> </Options> </OASValidation>
<Query>
(<AllowUnspecifiedParameters>
的子項)
如果要求中含有 OpenAPI 規格中未定義的查詢參數,則可設定政策的行為。
如要在要求中指定 OpenAPI 規範中未定義的查詢參數,請將此參數設為 true。否則,請將這個參數設為 false,以免政策執行失敗。
預設值 | true |
是否必要? | 布林值 |
類型 | 複雜類型 |
上層元素 |
<AllowUnspecifiedParameters>
|
子元素 | 無 |
語法
<Query>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Options> <AllowUnspecifiedParameters> <Query>[true|false]</Query> </AllowUnspecifiedParameters> </Options> ... </OASValidation>
範例
以下範例會將政策設為在要求中指定的查詢參數未在 OpenAPI 規格中定義時失敗。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <AllowUnspecifiedParameters> <Query>false</Query> </AllowUnspecifiedParameters> </Options> </OASValidation>
如果要求中含有 OpenAPI 規格中未定義的 Cookie 參數,則會設定政策的行為。
如要在要求中指定未在 OpenAPI 規範中定義的 Cookie 參數,請將此參數設為 true。否則,請將這個參數設為 false,以免政策執行失敗。
預設值 | true |
是否必要? | 布林值 |
類型 | 複雜類型 |
上層元素 |
<AllowUnspecifiedParameters>
|
子元素 | 無 |
語法
<Cookie>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Options> <AllowUnspecifiedParameters> <Query>[true|false]</Query> </AllowUnspecifiedParameters> </Options> ... </OASValidation>
範例
以下範例會將政策設為在要求中指定的查詢參數未在 OpenAPI 規格中定義時失敗。
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Options> <AllowUnspecifiedParameters> <Cookie>false</Cookie> </AllowUnspecifiedParameters> </Options> </OASValidation>
<Source>
要評估 JSON 酬載攻擊的 JSON 訊息。這項屬性通常會設為 request
,因為您通常需要評估來自用戶端應用程式的傳入要求。設為「response」,即可評估回覆訊息。將其設為「message」,即可在政策附加至要求流程時自動評估要求訊息,以及在政策附加至回應流程時評估回應訊息。
預設值 | 申請。 |
是否必要? | 選用 |
類型 | 字串 |
上層元素 |
<Source>
|
子元素 | 無 |
語法
<Source>
元素使用以下語法:
<OASValidation name="policy_name"> <OASResource>oas://specname[.json|.yaml|.yml]</OASResource> <Source>[message|request|response]</Source> ... </OASValidation>
範例
以下範例會在政策附加至要求流程時自動評估要求訊息,以及在政策附加至回應流程時自動評估回應訊息:
<OASValidation name="myoaspolicy"> <OASResource>oas://my-spec.yaml</OASResource> <Source>message</Source> </OASValidation>
<Source>
元素沒有屬性或子項元素。
結構定義
每個政策類型都由 XML 架構 (.xsd
) 定義。如需參考,請前往 GitHub 查看政策架構。
錯誤代碼
This section describes the fault codes and error messages that are returned and fault variables that are set by Edge when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.
Runtime errors
These errors can occur when the policy executes.
Fault code | HTTP status | Cause | |
---|---|---|---|
steps.oasvalidation.Failed |
500 | Request message body cannot be validated against the provided OpenAPI Specification. | |
steps.oasvalidation.SourceMessageNotAvailable |
500 |
Variable specified in the |
|
steps.oasvalidation.NotMessageVariable |
500 |
|
build |
Deployment errors
These errors can occur when you deploy a proxy containing this policy.
Error name | Cause | |
---|---|---|
ResourceDoesNotExist |
OpenAPI Specification referenced in the <OASResource> element does not exist.
|
|
ResourceCompileFailed |
OpenAPI Specification that is included in the deployment contains errors that prevent it from being compiled. This generally indicates that the specification is not a well-formed OpenAPI Specification 3.0. | |
BadResourceURL |
OpenAPI Specification referenced in the <OASResource> element cannot be processed. This can occur if the file is not a JSON or YAML file or the
file URL is not specified correctly.
|
Fault variables
These variables are set when this policy triggers an error at runtime. For more information, see What you need to know about policy errors.
Variables | Where | Example |
---|---|---|
fault.name="fault_name" |
fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. | fault.name Matches "ResourceDoesNotExist" |
oasvalidation.policy_name.failed |
policy_name is the user-specified name of the policy that threw the fault. | oasvalidation.myoaspolicy.failed = true |
支援的 OpenAPI 規範功能
OASValidation 政策支援 OpenAPI 規範功能,下表依類別列出這些功能。系統也會列出不支援的功能。
類別 | 支援 | 不支援 |
---|---|---|
資料類型格式 | boolean date date-time double float int32/int64 ipv4/ipv6 md5 sha1/sha256/sha512 string uri uri-template uuid |
binary byte password |
鑑別器物件 | mapping propertyName |
不適用 |
媒體類型物件 | 架構 | 編碼 範例 範例 |
作業物件 | 參數 requestBody responses 安全性 (部分支援) |
回呼 已淘汰 伺服器 |
參數物件 | allowEmptyValue in ( query , header , path )required responses schema style ( deepObject , form , formmatrix , label , pipeDelimited , simple , spaceDelimited )注意: deepObject 僅支援字串參數,不支援陣列和巢狀物件。 |
allowReserved 已淘汰 example examples 內容 |
Paths 物件 | delete get head options parameters patch post put trace 變數 |
伺服器 |
要求主體物件 | application/json application/hal+json application/x-www-form-urlencoded ( encoding 物件不支援)content 必要 |
application/xml multipart/form-data text/plain text/xml |
回應物件 | application/json application/hal+json application/x-www-form-urlencoded ( encoding 物件不支援)內容 標頭 |
application/xml links text/plain text/xml |
Responses 物件 | default HTTP 狀態碼 |
無 |
結構定義物件 | $ref additionalProperties (僅限布林值旗標變化版本) allOf (如果 additionalProperties 為 false ,則會忽略)anyOf enum exclusiveMaximum/exclusiveMinimum format items maximum/minimum maxItems/minItems maxLength/minLength maxProperties/minProperties multipleOf not nullable oneOf pattern properties required title type uniqueItems |
deprecated example readOnly writeOnly xml |
安全性配置文件物件 | in (header , query ) (如果 type 是 http ,則會忽略)name type ( apiKey , http )
|
bearerFormat flows openIdConnectUrl scheme |
伺服器物件 | url 變數 |
多個伺服器定義 |