JSON Module を使用した JSON スキーマに対するドキュメントの検証

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

<flow name="process">
    <json:validate-schema schema="/schema/my-schema.json" />
</flow>
このコネクタでは JSON スキーマ検証のドラフト 3 および 4 のみがサポートされます。

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

<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
[
  {"a": "", "b": ""},
  {"a": "", "b": ""},
  {"a":""},
  {"a": "", "b":""},
  {"b": ""}
]

そして、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 は無効です。

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

<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>

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

[ {
  "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 を介して他のスキーマを参照するものがあります。けれども、主としてパフォーマンスやセキュリティ上の理由から、これらのスキーマをインターネットから取得したくないことがあります。

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

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

<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​ のスキーマが、アプリケーション内に付属する、​schema/fstab.json​ というパスのスキーマにリダイレクトされます。

追加オプション

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

詳細は、​「JSON Module ドキュメントリファレンス」​を参照してください。