How to validate JSON against hyper-schema with json-schema-validator - java

I cannot figure out how to properly setup a hyper-schema to work with json-schema-validator. I am using the java version of json-schema-validator, version is 2.2.5.
My schema is:
{
"$schema": "http://json-schema.org/draftv4/hyper-schema#",
"title": "User object",
"description": "A user representation",
"type": "object",
"properties": {
"email": {
"description": "The user's email address",
"format":"email",
"maxLength": 255
},
"picture": {
"description": "The user's picture",
"type": "string",
"media": {
"binaryEncoding": "base64",
"type": "image/png"
}
}
}
}
My json object is:
{"email":"k#w.de",
"picture":null}
Now, when I load up the schema into JsonSchemaFactory and intend to start validating I get the following warning:
warning: the following keywords are unknown and will be ignored: [media]
level: "warning"
schema: {"loadingURI":"#","pointer":"/properties/picture"}
domain: "syntax"
ignored: ["media"]
Is there anything else to configure for using the hyper-schema besides the $schema field?

This is because your $schema is wrong!
It should be http://json-schema.org/draft-04/hyper-schema#. See section 6 of the core specification for the list of well-known URIs.

Related

Plugin jsonschema2pojo: Properties are shown as required but they should be optional

i have a personschema which has e.g. those two fields:
"name": {
"title": "Last name",
"type": "string",
"minLength": 1,
"isRequired" : true
}
"birthday": {
"title": "Date of birth",
"$ref": "resource:schema/general/dateSchema.json",
"isRequired" : true
},
"birthCountry": {
"$ref": "resource:schema/general/countrySchema.json",
"title": "Country of birth",
"isRequired" : true
}
Then I have another schema which extends the person schema. In this schema, I want those two properties (birthCountry and birthday) to be optional and only the name to be mandatory. I've tried it like that:
{
"$schema": "http://json-schema.org/draft/2019-09/schema",
"title": "Other Schema",
"type": "object",
"javaType": "..json_schema_model.dto.OtherSchema",
"description": "Other Schema Description",
"extendsJavaClass": "..json_schema_model.dto.PersonSchema",
"allOf": [{
"required": [
"name"
],
"$ref": "resource:schema/general/personSchema.json"
}
But unfortunately, in the API-Docs they are still remarked as mandatory.
The issue was that properties can only be added in child classes and not modified, so I rearranged the class structure to modify the decision whether a value is mandatory will only be decided in the child classes themselves. Also please note, that sibling values alongsid $refs are ignored. To add properties to a $ref, you need to wrap it into an allOf.

How to include validations for key as well as value in a json schema

I need to add validations for a property as well as a value in json schema.
I tried to use the below schema but none of the validations work :
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"additionalProperties": false,
"minProperties": 1,
"properties": {
"add": {
"type": "object",
"patternProperties": {
"^VOF979[0-9]{11}-NDG[0-9]{2}$": {
"description": "Some description",
"type": "string",
"maxLength": 2
}
}
}
}
}
I used below json data and all the validations passes although the key and value both are wrong :
{
"add": {"VOF98999990005235-NDG01": "121"}
}
JSON Schema is constraints based.
patternProperties applies its value schema to the instance location based on the key match (in this case, regex match).
It does not prohibit additional keys in the object.
If you want to prevent additional keys, you need to specify so.
To do this, you need "additionalProperties": false.
Do not allow additional properties to keep strict validation
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"add": {
"type": "object",
"patternProperties": {
"^VOF979[0-9]{11}-NDG[0-9]{2}$": {
"description": "Some description",
"type": "string",
"maxLength": 2
}
},
"additionalProperties": false // This One
}
},
"additionalProperties": false,
"minProperties": 1
}
Reference to Docs Have a look at this

How to generate json data from Json Schema Programmatically in Java

I am trying to create Body-parameter(JSON) for my POST Api , which is a JSON request . All I have is the JSON Schema . I am trying to come up with a list of different JSON test data covering Positive and negative flows for it .
Is there any option to generate / create the JSON data programmatic using Java ? . I have attached a small Json schema (just for understanding purpose) but my actual schema is more complicated with lot of Array's and Nested Json's .
My Json Schema :
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The Root Schema",
"description": "The root schema comprises the entire JSON document.",
"required": [
"FirstName",
"LastName",
"Age",
"Interest"
],
"properties": {
"FirstName": {
"$id": "#/properties/FirstName",
"type": "string",
"title": "The Firstname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Vijay"
]
},
"LastName": {
"$id": "#/properties/LastName",
"type": "string",
"title": "The Lastname Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Karthik"
]
},
"Age": {
"$id": "#/properties/Age",
"type": "integer",
"title": "The Age Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
30
]
},
"Interest": {
"$id": "#/properties/Interest",
"type": "array",
"title": "The Interest Schema",
"description": "An explanation about the purpose of this instance.",
"default": [],
"items": {
"$id": "#/properties/Interest/items",
"type": "string",
"title": "The Items Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Food",
"movie",
"Learning",
"VideoGames"
]
}
}
}
}enter code here
My TestData looks like :
{
"FirstName":"Vivi",
"LastName":"Karrri",
"Age":30,
"Interest":["Food","movie","Learning","VideoGames"]
}
Any suggestions how can we achive this ?
Note : I am using Springboot and I have complete POJO for the request object
You can generate fake java-objects and then map them to JSON.
POJOs
If you already have the POJOs matching the schema, then we can skip this step.
If no, to generate a POJO from the schema, for example, can be used this library:
jsonschema2pojo.
Fake objects
Generating of objects with fake data can be done with a special library, some of them are listed here:
easy-random
podam
java-faker
Generating JSON
It's prettry simple and can be done with Jackson:
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter prettyPrinter = objectMapper.writerWithDefaultPrettyPrinter();
String json = prettyPrinter.writeValueAsString(yourFakeObject);
If you have json schema then you can directly generate a sample JSON message from it.
https://github.com/jignesh-dhua/json-generator

Json validation fails against json schema

I use com.github.fge.jsonschema.main.JsonSchema to validate json.
This is json schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Team data",
"description": "Validation schema",
"type": "object",
"additionalProperties": false,
"required": [
],
"properties": {
"name": {
"type": "string",
"minLength": 2,
"maxLength": 255,
"description": "Name"
}
}
}
And this is json to validate against schema:
{"name" : "name"}
This is valid when I use online validator to check, but in test I get an error:
Caused by: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: invalid JSON Schema, cannot continue
Syntax errors:
[ {
"level" : "error",
"message" : "array must have at least one element",
"domain" : "syntax",
"schema" : {
"loadingURI" : "#",
"pointer" : ""
},
"keyword" : "required"
} ]
level: "fatal"
at com.github.fge.jsonschema.processors.validation.InstanceValidator.process(InstanceValidator.java:114) ~[json-schema-validator-2.2.10.jar:?]
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:56) ~[json-schema-validator-2.2.10.jar:?]
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:34) ~[json-schema-validator-2.2.10.jar:?]
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79) ~[json-schema-core-1.2.10.jar:?]
at com.github.fge.jsonschema.main.JsonSchemaImpl.doValidate(JsonSchemaImpl.java:77) ~[json-schema-validator-2.2.10.jar:?]
at com.github.fge.jsonschema.main.JsonSchemaImpl.validate(JsonSchemaImpl.java:100) ~[json-schema-validator-2.2.10.jar:?]
at com.github.fge.jsonschema.main.JsonSchemaImpl.validate(JsonSchemaImpl.java:110) ~[json-schema-validator-2.2.10.jar:?]...
I can't see where the error is.
Well, the error seems to be in your scheme instead of in the file to validate. The required property is of the array type, but the array has no element.
The validation succeeds when you either remove the property required, or provide at least one string element indicating which properties should be required:
"required": [
"name"
]
Since Draft 4 of the JSON Schema documentation, the array must have at least one element.

Business Validation Error: Unexpected Internal Error. (-30003) - QuickBooks Java SDK

i cannot create invoice in QuickBooks online using Java SDK v2.9.1
company location: India
currency: INR
due to some validation error not very clearly returned in API fault response.
i believe the payload is complete as the same works with US sandbox environment.
any insight into this?
thanks in advance :)
Request:
{
"ApplyTaxAfterDiscount": false,
"AutoDocNumber": false,
"CurrencyRef": {
"value": "INR"
},
"DepartmentRef": {
"value": "1"
},
"DocNumber": "00006",
"DueDate": "2017-06-13",
"Line": [
{
"Amount": 110.0,
"Description": "00006",
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "25"
},
"Qty": 1,
"TaxCodeRef": {
"value": "3"
},
"UnitPrice": 110.0
}
}
],
"ShipAddr": {
"City": "HD",
"CountryCode": "IND",
"Line1": "55-DP-1",
"Line2": "",
"PostalCode": "600660"
},
"ShipDate": "2017-06-13",
"TotalAmt": 110.0,
"TxnDate": "2017-06-13"
}
Response:
{
"Fault": {
"Error": [
{
"Detail": "Business Validation Error: Unexpected Internal Error. (-30003)",
"Message": "A business validation error has occurred while processing your request",
"code": "6000",
"element": ""
}
],
"type": "ValidationFault"
},
"time": "2017-06-13T05:52:29.153-07:00"
}
while sending this request please check in your java code for correct date formats,
I got same error code when I trying to post QBO bill API with txnDate, but unfortunately it in my code format of date changed and I got above error. After converting date into correct format "yyyy-MM-DD", QBO bill payment API works fine for me.
So first check in your code or print json request data to check date format.

Categories

Resources