How to validate number of properties in JSON schema - json

I am trying to create a schema for a piece of JSON and have slimmed down an example of what I am trying to achieve.
I have the following JSON schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Set name",
"description": "The exmaple schema",
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": false
}
The following JSON is classed as valid when compared to the schema:
{
"name": "W",
"name": "W"
}
I know that there should be a warning about the two fields having the same name, but is there a way to force the validation to fail if the above is submitted? I want it to only validate when there is only one occurrence of the field 'name'

This is outside of the responsibility of JSON Schema. JSON Schema is built on top of JSON. In JSON, the behavior of duplicate properties in an object is undefined. If you want to get warning about this you should run it through a separate validation step to ensure valid JSON before passing it to a JSON Schema validator.

There is a maxProperties constraint that can limit total number of properties in an object.
Though having data with duplicated properties is a tricky case as many json decoding implementions would ignore duplicate.
So your JSON schema validation lib would not even know duplicate existed.

Related

JSON Schema object properties defined by enum

I'm attempting to reuse an enum in my JSON Schema to define the properties for an object.
I was wondering if the following is correct.
JSON Schema
{
"type": "object",
"propertyNames": {
"enum": ["Foo","Bar"]
},
"patternProperties": {
".*": {
"type": "number"
}
}
}
JSON Data
{
"Foo": 123,
"Bar": 456
}
The reason I ask is that I get inconsistent results from JSON Schema validation libraries. Some indicate the JSON validates, while others indicate the JSON is invalid.
p.s. if anyone is wondering "why" I'm trying to define the properties with an enum, it is because the enum is shared in various parts of my json schema. In some cases it is a constraint on a string, but I need the identical set of possible values both on those string properties and also on the object properties. As an enum I can maintain the set of possible values in one place.
Yes, that's a valid JSON Schema. You could also express it like this:
{
"type": "object",
"propertyNames": {
"enum": ["Foo","Bar"]
},
"additionalProperties": {
"type": "number"
}
}
It says "all property names must conform to this schema: (one of these values listed in the enum); also, all property values must conform to this schema: (must be numeric type)."
What errors do you get from the implementations that report this as invalid? Those implementations have a bug; would you consider reporting it to them?

Storing JSON blob in an Avro Field

I have inherited project where an avro file that is being consumed by Snowflake. The schema of the avro is as follows:
{
"name": "TableName",
"namespace": "sqlserver",
"type": "record",
"fields": [
{
"name": "hAccount",
"type": "string"
},
{
"name": "hTableName",
"type": "string"
},
{
"name": "hRawJSON",
"type": "string"
}
]
}
The hRawJSON is a blob of JSON itself. The previous dev put this as a type of string, and this is where I believe the problem lies.
The application takes a JSON object (the JSON is varible so I never know the contents or what it contains) and populates the hRawJSON field in the Avro record. But it contains the escape characters for the double quotes in the string:
hAccount:"H11122"
hTableName:"Departments"
hRawJSON:"{\"DepartmentID\":1,\"ModelID\":0,\"Description\":\"P Medicines\",\"Margin\":\"3.300000000000000e+001\",\"UCSVATRateID\":0,\"References\":719,\"HeadOfficeID\":1,\"DividendID\":0}"
As a result the JSON blob is staged into Snowflake as a VARIANT field but still retains the escape characters:
Snowflake image
This means when querying the data in the JSON I constantly have to use this:
PARSE_JSON(RAW_FILE:hRawJSON):DepartmentID
I can't help feeling that the field type of string in the Avro file is causing the issue and that a different type should be used. I've tried Record, but without fields it's unuable. Doc also not working.
The other alternative is that this behavior is correct and when moving the hRawJSON from staging into "proper" tables I should use something like:
INSERT INTO DATA.PUBLIC.DEPARTMENTS
SELECT
RAW_FILE:hAccount::VARCHAR(4) as Account,
PARSE_JSON(RAW_FILE:hRawJSON) as JsonRaw
FROM DATA.STAGING.AVRO_RAW WHERE RAW_FILE:hTableName::STRING = 'Department';
So if this should be the correct approach and I'm over thinking this I'd appreciate guidance.

Can I import table in a JSON Schema validation?

I am writing a JSON schema validation. I have an ID field whose values are imported from a table in SQL Server. These values are large and are frequently updated, so is there a way to dynamically connect to this table in the server and validate the JSON? Below is an example code of my schema:
{
"type": "object",
"required": ["employees"],
"properties": {
"employees": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer", enum = [134,2123,3213,444,5525,6234,7532,825,9342]}
}
}
}
}
In place of 'enum' I want to connect to a table so the ID values are updated when the table is updated.
As Greg said, there is nothing in JSON Schema which allows you to do this.
Some implementations have created their own extensions to allow external sources. Many implementations allow custom keywords. You would have to check your documentation.
You should consider the cost of querying the database at the same time as checking structural correctness. It may be benficial to do ID checking which hits your database after you've confirmed the data is of the correct format and structure.

json-schema-validator custom message

I am using json-schema-validator2.2.6 library to validate my json against json schema. The problem is that it gives generic error messages that are not relevant to me. I want to send custom message or code to user.
Do we have any option like this :
"properties": {
"myKey": {
"type": "string"
**"errorMessage" : "My error message"**
},
}
Or any other way by which I can provide custom error message?
You can create Custom Error Messages in JSON Schema. Sort Of!(In NodeJS). Lets take an Example -
We have to check a key 'DOB' in JSON which should is a required field and it should be in format 'dd-mmm-yyyy'.
Now we have to use two validation in JSON. First, It should be present and it should follow the pattern of `dd-mmm-yyyy'
Now JSON Schema would be
{
"id": "DOBChecker",
"type": "object",
"properties": {
"DOB": {
"type": "string",
"required": true,
"pattern": "/^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-](JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC)[-](\d{4}))$/i",
"message": {
"required": "Date of Birth is Required Property",
"pattern": "Correct format of Date Of Birth is dd-mmm-yyyy"
}
}
}
Now If you have got the error while validations. You will get the whole schema back at errors key array and in that access schema object. The Schema Object will contain exactly same keys as the schema defined above.
You can now access it . The failed Validation name will be in the 'name' key. Now you can access your custom Message using
schema.message[name]

JSON Schema Validation in Mule: get failing field

I am using APIkit in Mule with RAML 0.8 and a JSON schema, as follows (example):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"cart": {
"title": "",
"description": "",
"type": "object",
"properties": {
"internalNumber": {
"type": "integer"
}
},
"required": [
"internalNumber"
]
}
},
"required": [
"cart"
]
}
and in the Mule Flow, I catch the exception and show the following result:
#[exception.cause.message]
When a validation error occurs, I want to get the name of the field in which the validation failed. Instead, this is what I got:
Input
{
"cart": {
"internalNumber": "I must be an integer"
}
}
Output
"instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
Expected output
{
"field": "cart.internalNumber",
"error": "instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
}
All I want to know is if there is a way to get the name of the field in which the validation errors occurs.
Regarding the Mule Documentation, I can get the whole JSON string but not the name of the failing field...
I hope someone can give me a better solution.
Thanks!
Within your JSON Schema, add "required":"true" attribute, to make the fields mandatory.
You can also use JSON schema validator, in your mule flow, by referring to the updated schema.
Any of the case should through you an error with missing field.
Use below expression to get expected error message.
{
"errorMessage": "#[exception].toString().replace("\"","\\\"")"
}
Not sure if you are expecting it as an output or looking for a way to validate your input and schema.
I can try to suggest on "All I want to know is if there is a way to get the name of the field in wich the validation errors occurs."; to do this better validate your JSON and input data through online validator before defining definitions. Like using http://www.jsonschemavalidator.net/, it will help you with error and fields. Hope this may help!