Azure API管理策略:根据json架构验证请求主体



我正在尝试在Azure API管理中实现内容验证策略。我创建了一个名为event的模式。

政策如下:

<validate-content unspecified-content-type-action="prevent" max-size="12000" size-exceeded-action="prevent" errors-variable-name="validationErrors">
<content type="application/json" validate-as="json" action="prevent" schema-id="event" schema-ref="#/components/schemas/event" />
</validate-content>

当我尝试测试它时,我在跟踪中收到一条错误消息,说模式引用是错误的:

"details": "Could not resolve schema reference '#/components/schemas/event'. Path '', line 1, position 1."

文件显示:

<content type="content type string" validate-as="json|xml|soap" schema-id="schema id" schema-ref="#/local/reference/path" action="ignore|prevent|detect" />

但我没有发现任何线索如何找到schema-idschema-ref属性。

当我打开模式时,屏幕上没有类似的内容;概述";也不低于";属性";。请有人告诉我如何填写这些信息以使政策发挥作用。或者,如果它存在,请指向我可以阅读的文档:(

schema-idschema-ref都是根据json模式验证请求主体的可选属性。

根据此Azure API管理验证策略| Microsoft文档:

schema-id:已添加到API管理实例中进行内容验证的现有架构的名称。如果未指定,则使用API定义中的默认架构
schema ref:对于schema-id中指定的JSON架构,可选引用JSON文档中的有效本地引用路径。示例:#/components/schemas/address。该属性应返回一个JSON对象,API管理将其作为有效的JSON模式处理。

请确保检查Azure API管理验证策略|Microsoft Docs架构以进行内容验证,无论您是从文件还是URL导入,都要提供正确的架构位置。

正如@madhuraj所提到的,架构id是您在Azure APIM的"架构"部分中添加的架构的名称。schema-ref是API规范(swagger/OpenAPI(中的模式定义的路径。@vilmarcischema ref是可选的,但您提供了一个不正确的路径,即"#/components/schemas/event"。尝试不使用架构引用,它会起作用。

基本上在API下->架构我们可以用名称(Id(定义JSON文档。例如,我们可以定义模式LoginAPI,在其下有不同的定义,

{
"components": {
"schemas": {
"LoginModel": {
"type": "object",
"required": [
"email",
"password"
],
"properties": {
"email": "definition",
"password": "definition"
}
}
}
}
}

然后在验证内容配置中,您可以使用schema id=";LoginAPI">模式ref="#/组件/架构/LoginModel">

最新更新