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.
Related
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.
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
I am trying to validate my json response in my rest assured tests and i get the following error in my schema. not sure how to resolve this. any help will be appreciated :) thank you.
error:
io.restassured.module.jsv.JsonSchemaValidationException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('s' (code 115)): was expecting double-quote to start field name
at [Source: java.io.StringReader#54c622a7; line: 1, column: 3]
schema:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"status"
],
"properties": {
"status": {
"$id": "#/properties/status",
"type": "string",
"title": "The Status Schema",
"default": "",
"examples": [
"PROCESSING"
],
"pattern": "^(.*)$"
}
}
}
method:
Assert.assertThat(bookingResponse, matchesJsonSchemaInClasspath("book-response-schema.json"));
What is the correct way to create avro schema for object with array of strings?
I am trying to create avro schema to object that have array of strings according to official documenation? but I get error.
https://avro.apache.org/docs/1.8.1/spec.html
[ERROR] Failed to execute goal org.apache.avro:avro-maven-plugin:1.8.2:schema (default) on project email: Execution default of goal org.apache.avro:avro-maven-plugin:1.8.2:schema failed: "array" is not a defined name. The type of the "parameters" field must be a defined name or a {"type": ...} expression. -> [Help 1]
Why my schema is inccorect?
[
{
"type": "record",
"namespace": "com.example",
"name": "Topic",
"fields": [
{ "name": "subject", "type": "string" },
{ "name": "parameters", "type": "array", "items": "string" }
]
}
]
Think this should work:
{
"name":"parameters",
"type": {
"type": "array",
"items": "string"
}
}
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.