Validate Json Schema Draft V4 - json

I have a Json schema as below , when i validate it on a online validatior like http://jsonschemalint.com/draft4/# it says its valid.I changed "type" to "object" , "array" , "string" and kept the rest of the structure same .still it says valid schema. My understanding was when
type=object there should be "properties"
when type=array there should be"items"
{
"title": "Example Schema",
"type": "string",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
},
"required": [
"firstName",
"lastName"
]
}

According to the docs:
Some validation keywords only apply to one or more primitive types.
When the primitive type of the instance cannot be validated by a given
keyword, validation for this keyword and instance SHOULD succeed.
So the behavior you mention is correct.

Related

How to declare an optional field in Google Pub/Sub Topic Schema?

I'm trying to create a Pub/Sub Schema Topic on AVRO respecting the indications on the documentation with "default" : null indication.
I declared my optional field this way :
{
"name": "myField",
"type": ["null","string"],
"default": null
}
The error I get :
Incorrect token in the stream. Expected: Object start, found String
Do you have any idea on how to solve this ?
Not sure if this is exactly what you would like, but I would probably try something like this:
{
"type": "record",
"name": "SomeName",
"fields": [
{
"name": "myField",
"type": ["string", "null"]
}
]
}
as defined in Apache Avro specification and described in PubSub Schema creation
#Snowfire, you have mentioned in the comment that you choose to continue with schema-less topic.
You can follow this document to create Pub/Sub Schema.
To create a schema of Avro type you can try this:
{
"type": "record",
"name": "Avro",
"fields": [
{
"name": "testname",
"type": ["null", "string"],
"default": null
},
{
"name": "testId",
"type": ["null", "int"],
"default": null
}
]
}
I tested below message against the schema and it works.
{
"testname": {
"string": "Jack"
},
"testId": {
"int": 101
}
}

Everit validator: JsonSchema property named "type"

Using Everit, I'm trying to come up with a json schema that validates
{
"type": "my content type",
"name": "content name"
}
My solution:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "model.json",
"type": "object",
"properties": {
"type": {
"type": "string",
"minLength": 1
},
"name": {
"type": "string",
"minLength": 1
}
},
"additionalProperties": false,
"required": [
"type",
"name"
]
}
This generates the error:
properties/type: expected type is one of JsonArray or String, found: JsonObject
This schema loads just fine:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "model.json",
"type": "object",
"properties": {
"some-type": { <<-- Only field name changed
"type": "string",
"minLength": 1
},
"name": {
"type": "string",
"minLength": 1
}
},
"additionalProperties": false,
"required": [
"some-type",
"name"
]
}
But the 'type' field is a database field that I don't want to rename.
How can I define the json schema to work with the property named 'type'?
Is the 'type' property name directly under properties interpreted as a reserved keyword?
Thx,
Ronald
According to another validator (https://www.jsonschemavalidator.net/) your schema is completely fine.
This might be a bug in the Everit validator, so you could consider looking into alternatives as well. Personally, I've liked networknt/json-schema-validator so far but haven't tested with your particular example.
That being said, according to the Everit documentation when it determines the applicable Draft version, it looks for this:
"$schema": "http://json-schema.org/draft-07/schema"
However in your example you use:
"$schema": "http://json-schema.org/draft-07/schema#"
Might be worth trying again without the trailing fragment (#).

How to use JSON Schema to require one of two fields

I want to validate JSON to make one of two fields manadatory.
Let's assume we have two fields (Email Address and Phone Number). I want to make sure that one of the two fields is required for the record to be valid.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "ExampleID-0212",
"title": "objectExamples",
"description": "Demo",
"type": "object",
"properties": {
"RecordObject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string"
},
"PhoneNumber": {
"type": "number"
}
}
}
},
"required": [
"RecordObject"
]
}
You need to add:
"anyOf": [
{ "required":
[ "emailAddress" ] },
{ "required":
[ "PhoneNumber" ] }
]
to the schema of RecordObject property.
It requires that at least one of fields is present. If you need exactly one field (i.e., not both) present, you need to use "oneOf" keyword (the rest should be the same).
This reference of JSON Schema keywords can be useful.
Sooperb Answer from ESP.. that is from jsonSchema ...
you can also do that validation thorugh condition..see below
if(EmailAddress == null && PhoneNumber == null){
//statements or error message response
}

What is a JSON schema?

I'm trying to understand what JSON schema is. I understand its related to JSON. But what is it used for? How does one create a schema? Can any JSON be a JSON schema?
JSON Schema describes your JSON data format, for sample, the general structure of a json content if the schema, for sample:
{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
You have a property called title which is a string, you have a property called properties which is an object with other properties.
See more
http://json-schema.org/examples.html

JSON Hyper-Schema: different schemas for GET and POST

I want to describe an API that has fields which allows for different ways to define values when POSTing an item, but only ever output in the field in one specific way.
For example, I might want to describe an API where an item can be created or updated like this: {"name": "Task", "due": "2014-12-31"} or like this: {"name": "Task", "due": {"$date": 1419984000000}}, but it is only ever returned from the API in the first way.
The schema for POST/PUT could therefore be:
{
"type": "object"
"properties": {
"name": {
"type": "string"
},
"due": {
"oneOf": [
{
"type": "string",
"format": "date"
},
{
"type": "object",
"properties": {
"$date": {
"type": "number"
}
},
"required": ["$date"],
"additionalProperties": false
}
]
}
}
}
Whereas the schema for access via GET would be much simpler:
{
"type": "object"
"properties": {
"name": {
"type": "string"
},
"due": {
"type": "string",
"format": "date"
}
}
}
It would be good for consumers of the API to know that they only have to account for one possible output method rather then all of them.
Is there any accepted standard approach to specify the different schemas within the context of JSON Hyper-Schema? I've thought about specifying these differences via the "links" property, but I do not know what "rel" I would define these schemas under and it seems very-non-standard.
If I understood correctly, and you want to specify one schema per operation you can do it with standard hyper-schema. Let's see and example for a post operation:
{
"description": "create an item.",
"href": "/items",
"method": "POST",
"rel": "create",
"schema": {
"$ref": "#/api/createitem"
},
"title": "Create an item"
}
The actual schema that is required is referenced in "schema" property through "$ref".
If you also wanted to describe the response types, then you could use "targetSchema" property. Be aware that this is advisory only (as it is explained in the docs)