---
schema_version: "1"
id: 2c62c739f76d588017b5859182fd2028bf09ca4b5d92e6cf2ee7df9199247371
title: JSON Module を使用した JSON スキーマに対するドキュメントの検証
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/json-module/2.1/json-schema-validation.md
canonical_url: https://docs.mulesoft.com/jp/json-module/2.1/json-schema-validation
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2f6bdea48d528f90142f6130390a3128916118f346f2ccca8ddc560ba4a540a7
description: MuleSoft Documentation Site
release: "2.1"
latest: false
---
# JSON Module を使用した JSON スキーマに対するドキュメントの検証

`<json:validate-schema>`​ 操作は、入力コンテンツが指定した JSON スキーマに準拠していることを検証します。

```xml
<flow name="process">
    <json:validate-schema schema="/schema/my-schema.json" />
</flow>
```

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">このコネクタでは JSON スキーマ検証のドラフト 3 および 4 のみがサポートされます。</td></tr></tbody></table>

この操作はデフォルトで、メッセージペイロードレベルで入力ドキュメントを検索します。他方、次のような独自の入力を行うこともできます。

```xml
<flow name="process">
    <file:read path="document.json" target="jsonDoc" />
    <json:validate-schema schema="/schema/my-schema.json">
        <json:content>#[vars.jsonDoc]</json:content>
    </json:module:validate-schema>
</flow>
```

`<validation:all>`​ 要素内の ​`<json:validate-schema>`​ コンポーネントを使用できます。

## [](#検証エラーの処理)検証エラーの処理

検証に成功した場合は何も起こらず、フローの次の操作に進みます。失敗した場合は、​`JSON:SCHEMA_NOT_HONOURED`​ エラーが生じます。

`JSON:SCHEMA_NOT_HONOURED`​ は複雑なエラーです。検証は複数の理由で失敗する可能性があるため、エラーにはオブジェクトの JSON 配列が記載されます。これらのオブジェクトごとに、検出された各エラーに関する情報が示されます。

たとえば、次の入力 JSON について考えてみます。

無効な JSON

```json
[
  {"a": "", "b": ""},
  {"a": "", "b": ""},
  {"a":""},
  {"a": "", "b":""},
  {"b": ""}
]
```

そして、JSON スキーマが次のとおりであるとします。

JSON スキーマ

```json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "a": {
        "type": "string",
		"minLength": 1,
		"maxLength": 255
		},
      "b": {
        "type": "string",
		"minLength": 1,
		"maxLength": 255
      }
    },
    "required": ["a", "b"]
  }
}
```

ご覧のとおり、​`a`​ と ​`b`​ のどちらの文字列属性も必須で、双方とも 1 文字以上にする必要があるため、この入力 JSON は無効です。

ユースケースで検証を実行して失敗した場合に、レポートをファイルに追加するには、以下のようにすることができます。

```xml
<flow name="validate">
    <try>
        <json:validate-schema schema="/schema/object-array-schema.json" />
        <error-handler>
            <on-error-propagate type="JSON:SCHEMA_NOT_HONOURED">
                <file:write config-ref="file" path="errors/jsonSchemas.json" mode="APPEND">
                    <file:content>#[error.errorMessage.payload]</file:content>
                </file:write>
            </on-error-propagate>
        </error-handler>
    </try>
</flow>
```

ファイルに追加されるテキストは次のようになります。

```json
[ {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/a"
  },
  "instance" : {
    "pointer" : "/0/a"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/b"
  },
  "instance" : {
    "pointer" : "/0/b"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/a"
  },
  "instance" : {
    "pointer" : "/1/a"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/b"
  },
  "instance" : {
    "pointer" : "/1/b"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items"
  },
  "instance" : {
    "pointer" : "/2"
  },
  "domain" : "validation",
  "keyword" : "required",
  "message" : "object has missing required properties ([\"b\"])",
  "required" : [ "a", "b" ],
  "missing" : [ "b" ]
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/a"
  },
  "instance" : {
    "pointer" : "/2/a"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/a"
  },
  "instance" : {
    "pointer" : "/3/a"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/b"
  },
  "instance" : {
    "pointer" : "/3/b"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items"
  },
  "instance" : {
    "pointer" : "/4"
  },
  "domain" : "validation",
  "keyword" : "required",
  "message" : "object has missing required properties ([\"a\"])",
  "required" : [ "a", "b" ],
  "missing" : [ "a" ]
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "file:/schema/object-array-schema.json#",
    "pointer" : "/items/properties/b"
  },
  "instance" : {
    "pointer" : "/4/b"
  },
  "domain" : "validation",
  "keyword" : "minLength",
  "message" : "string \"\" is too short (length: 0, required minimum: 1)",
  "value" : "",
  "found" : 0,
  "minLength" : 1
} ]
```

## [](#スキーマのリダイレクト)スキーマのリダイレクト

JSON スキーマの中には、公開 URI を介して他のスキーマを参照するものがあります。けれども、主としてパフォーマンスやセキュリティ上の理由から、これらのスキーマをインターネットから取得したくないことがあります。

この問題の解決法は、メインスキーマと参照スキーマをすべてアプリケーションに含めることです。ただし、この解決法によって新たな問題が生じます。元のスキーマに、公開スキーマではなく、ローカルスキーマをポイントさせる必要があることです。

この問題に対処するために、このモジュールではスキーマのリダイレクトという概念が導入され、スキーマ自体を変更しなくても、外部参照をローカル参照に置換することができます。

```xml
<flow name="process">
    <json:validate-schema schema="/schema/my-schema.json">
        <json:schema-redirects>
            <json:schema-redirect from="http://mule.org/schemas/fstab.json" to="schema/fstab.json"/>
        </json:schema-redirects>
    </json:validate-schema>
</flow>
```

上記の例では、​`[http://mule.org/schemas/fstab.json](http://mule.org/schemas/fstab.json)`​ のスキーマが、アプリケーション内に付属する、​`schema/fstab.json`​ というパスのスキーマにリダイレクトされます。

## [](#追加オプション)追加オプション

バリデーターには、重複キーを許可するかどうか、参照解決をどのように処理するかなどの追加オプションがあります。

詳細は、​[「JSON Module ドキュメントリファレンス」](https://docs.mulesoft.com/jp/json-module/2.1/json-reference)​を参照してください。

## [](#関連情報)関連情報

- [JSON Module の概要](https://docs.mulesoft.com/jp/json-module/2.1/)