

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用验证 FHIR 资源 `$validate`
<a name="reference-fhir-operations-validate"></a>

AWS HealthLake 现在支持 FHIR 资源的`$validate`操作，使您无需执行任何存储操作即可根据 FHIR 规范验证资源并检查其是否符合指定的配置文件或基本资源定义。当您需要执行以下操作时，此操作特别有用：
+ 验证 FHIR CMS 合规性要求
+ 在生产环境中使用资源之前对其进行测试
+ 在用户编辑临床数据时提供实时验证反馈
+ 减少要创建和更新的无效数据提交 APIs

## 用法
<a name="validate-usage"></a>

可以使用 POST 方法在 FHIR 资源上调用该`$validate`操作：

**支持的操作**  


```
POST [base]/[type]/[id]/$validate
POST [base]/[type]/$validate
```

## 支持的负载
<a name="validate-payloads"></a>

**参数资源**  


HealthLake 支持以下 FHIR `$validate` 参数：


| 参数 | Type | 必需 | 说明 | 
| --- | --- | --- | --- | 
| resource | 资源 | 是 | 要验证的资源 | 
| profile | 权威性的 | 否 | 要验证的个人资料的规范网址 | 
| mode | 代码 | 否 | 验证模式：create，或 update | 

**带有查询参数的直接资源**  



| 参数 | Type | 必需 | 说明 | 
| --- | --- | --- | --- | 
| profile | 权威性的 | 否 | 要验证的个人资料的规范网址 | 
| mode | 代码 | 否 | 验证模式：create，或 update | 

## 示例
<a name="validate-examples"></a>

**带有 ID 和参数负载的 POST 资源请求**  


```
POST [base]/Patient/example-patient/$validate
Content-Type: application/fhir+json

{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "resource",
      "resource": {
        "resourceType": "Patient",
        "id": "example-patient",
        "name": [
          {
            "family": "Smith",
            "given": ["John"]
          }
        ],
        "gender": "male",
        "birthDate": "1990-01-01"
      }
    },
    {
      "name": "profile",
      "valueCanonical": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"
    },
    {
      "name": "mode",
      "valueString": "create"
    }
  ]
}
```

**资源类型和参数负载的 POST 请求**  


```
POST [base]/Patient/$validate
Content-Type: application/fhir+json

{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "resource",
      "resource": {
        "resourceType": "Patient",
        "name": [
          {
            "family": "Doe",
            "given": ["Jane"]
          }
        ],
        "gender": "female",
        "birthDate": "1985-05-15"
      }
    },
    {
      "name": "profile",
      "valueCanonical": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"
    },
    {
      "name": "mode",
      "valueString": "update"
    }
  ]
}
```

**POST 请求带有 ID 和直接资源负载的资源**  


```
POST [base]/Patient/example-patient/$validate?profile=http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient&mode=create
Content-Type: application/fhir+json

{
    "resourceType": "Patient",
    "id": "example-patient",
    "name": [
        {
        "family": "Smith",
        "given": ["John"]
        }
    ],
    "gender": "male",
    "birthDate": "1990-01-01"
}
```

**资源类型和直接资源负载的 POST 请求**  


```
POST [base]/Patient/$validate?profile=http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient&mode=create
Content-Type: application/fhir+json

{
    "resourceType": "Patient",
    "id": "example-patient",
    "name": [
        {
        "family": "Smith",
        "given": ["John"]
        }
    ],
    "gender": "male",
    "birthDate": "1990-01-01"
}
```

**示例响应**  
该操作返回一个包含验证结果的 OperationOutcome 资源：

```
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "information",
      "code": "informational",
      "diagnostics": "Validation successful"
    }
  ]
}
```

**包含验证错误的示例响应**  


```
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "required",
      "details": {
        "text": "Missing required element"
      },
      "diagnostics": "Patient.identifier is required by the US Core Patient profile",
      "location": [
        "Patient.identifier"
      ]
    },
    {
      "severity": "warning",
      "code": "code-invalid",
      "details": {
        "text": "Invalid code value"
      },
      "diagnostics": "The provided gender code is not from the required value set",
      "location": [
        "Patient.gender"
      ]
    }
  ]
}
```

## 行为
<a name="validate-behavior"></a>

该`$validate`操作：

1. 根据 FHIR 规范和基础资源定义验证资源

1. 提供`profile`参数时检查与指定配置文件的一致性

1. 根据指定的模式（`create`或）进行`update`验证

1. 返回详细的验证结果，包括错误、警告和信息性消息

1. 不执行任何存储操作-仅限验证

1. 无论是否发现验证问题，当可以执行验证时，都返回 HTTP 200 OK

## 验证模式
<a name="validate-modes"></a>
+ **创建**：像创建资源一样验证资源（新资源）
+ **更新**：像更新一样验证资源（现有资源）

## 错误处理
<a name="validate-error-handling"></a>

该操作返回：
+ 200 OK：验证已成功执行（无论验证结果如何）
+ 400 错误请求：请求格式或参数无效
+ 404 未找到：未找到资源类型或配置文件

有关`$validate`操作规范的更多信息，请参阅 [FHIR R4 资源`$validate`文档](https://www.hl7.org/fhir/R4/operation-resource-validate.html)。