

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 驗證 FHIR 資源 `$validate`
<a name="reference-fhir-operations-validate"></a>

AWS HealthLake 現在支援 FHIR 資源`$validate`的操作，可讓您根據 FHIR 規格驗證資源，並檢查其是否符合指定的設定檔或基本資源定義，而無需執行任何儲存操作。當您需要：
+ 驗證 FHIR CMS 合規要求
+ 在生產環境中使用資源之前進行測試
+ 在使用者編輯臨床資料時提供即時驗證意見回饋
+ 減少無效的資料提交以建立和更新 APIs

## Usage
<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 | 必要 | Description | 
| --- | --- | --- | --- | 
| resource | 資源 | 是 | 要驗證的資源 | 
| profile | 正式 | 否 | 要驗證之設定檔的正式 URL | 
| mode | code | 否 | 驗證模式： create或 update | 

**具有查詢參數的直接資源**  



| 參數 | Type | 必要 | Description | 
| --- | --- | --- | --- | 
| profile | 正式 | 否 | 要驗證之設定檔的正式 URL | 
| mode | code | 否 | 驗證模式： 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"
    }
  ]
}
```

**具有 ID 和直接資源承載的資源 POST 請求**  


```
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"
      ]
    }
  ]
}
```

## Behavior (行為)
<a name="validate-behavior"></a>

`$validate` 操作：

1. 根據 FHIR 規格和基本資源定義驗證資源

1. 提供 `profile` 參數時，檢查是否符合指定的設定檔

1. 根據指定的模式進行驗證 (`create` 或 `update`)

1. 傳回詳細的驗證結果，包括錯誤、警告和資訊性訊息

1. 不執行任何儲存操作 - 僅驗證

1. 在可執行驗證時傳回 HTTP 200 OK，無論是否發現驗證問題

## 驗證模式
<a name="validate-modes"></a>
+ **create**：將資源驗證為正在建立 （新資源）
+ **更新**：將資源驗證為正在更新 （現有資源）

## 錯誤處理
<a name="validate-error-handling"></a>

操作會傳回：
+ 200 OK：已成功執行驗證 （無論驗證結果為何）
+ 400 錯誤的請求：無效的請求格式或參數
+ 找不到 404：找不到資源類型或設定檔

如需 `$validate`操作規格的詳細資訊，請參閱 [FHIR R4 資源`$validate`](https://www.hl7.org/fhir/R4/operation-resource-validate.html)文件。