validate json schema

10
Validate JSON Schema

Upload: sivachandra-mandalapu

Post on 12-Jan-2017

71 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Validate json schema

Validate JSON Schema

Page 2: Validate json schema

Abstract

• The main motto of this PPT is How to use Validate JSON Schema in our applications.

Page 3: Validate json schema

Introduction• The JSON Schema validator evaluates JSON payloads at

runtime and verifies that they match a referenced JSON schema. You can match against schemas that exist in a local file or in an external URI. If the validation fails, an exception is raised with feedback about what went wrong and a reference to the original invalid payload. The JSON Schema Validator supports schema drafts of version 4 and older.

• This feature is specially handy when exposing a service that expects JSON inputs from users that must match a specific schema. You can use it to match the incoming external calls against the expected structure.

Page 4: Validate json schema

Example

Page 5: Validate json schema

.mflow• <?xml version="1.0" encoding="UTF-8"?>

• <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

• xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-current.xsd• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd• http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-

json.xsd• http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-

http.xsd">• <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8087" doc:name="HTTP

Listener Configuration"/>• <flow name="JsonSchemaValidatorFlow">• <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>• <json:validate-schema schemaLocation="Schema.json" doc:name="Validate JSON Schema"/>• <logger message="--Valid input" level="INFO" doc:name="Logger"/>• </flow>• </mule>

Page 6: Validate json schema

• Place the schema file in src/main/resources

Schema.json

Page 7: Validate json schema

• Output:• If the input is valid input:• INFO 2016-12-22 09:23:32,157

[[JsonSchemaValidator].HTTP_Listener_Configuration.worker.02] org.mule.api.processor.LoggerMessageProcessor: --Valid input

Page 8: Validate json schema

• If the input is invalid input:

• ERROR 2016-12-22 09:23:15,139 [[JsonSchemaValidator].HTTP_Listener_Configuration.worker.02] org.mule.exception.DefaultMessagingExceptionStrategy: • ********************************************************************************• Message : Json content is not compliant with schema• com.github.fge.jsonschema.core.report.ListProcessingReport: failure• --- BEGIN MESSAGES ---• error: object has missing required properties (["firstName"])• level: "error"• schema: {"loadingURI":"file:/C:/Users/sivachandra.mandalap/AnypointStudio/workspace1/.mule/apps/JsonSchemaValidator/classes/Schema.json#","pointer":""}• instance: {"pointer":""}• domain: "validation"• keyword: "required"• required: ["firstName","lastName"]• missing: ["firstName"]• --- END MESSAGES ---

• Payload : {"lastName":"def","age":10}• Payload Type : java.lang.String• Element : /JsonSchemaValidatorFlow/processors/0 @ JsonSchemaValidator• --------------------------------------------------------------------------------• Root Exception stack trace:• org.mule.module.json.validation.JsonSchemaValidationException: Json content is not compliant with schema• com.github.fge.jsonschema.core.report.ListProcessingReport: failure• --- BEGIN MESSAGES ---• error: object has missing required properties (["firstName"])• level: "error"• schema: {"loadingURI":"file:/C:/Users/sivachandra.mandalap/AnypointStudio/workspace1/.mule/apps/JsonSchemaValidator/classes/Schema.json#","pointer":""}• instance: {"pointer":""}• domain: "validation"• keyword: "required"• required: ["firstName","lastName"]• missing: ["firstName"]• --- END MESSAGES ---

• at org.mule.module.json.validation.JsonSchemaValidator.validate(JsonSchemaValidator.java:286)• at org.mule.module.json.validation.ValidateJsonSchemaMessageProcessor.process(ValidateJsonSchemaMessageProcessor.java:46)• at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)• at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108)• at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)

Page 9: Validate json schema

• Flow of execution:1. URL to trigger the service from browser