JSON Schema not validating required field when it is an empty string - json

I'm using mule validate JSON schema component to validate my incoming json request.
It validates the type but not the required field attributes
{
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema",
"properties": {
"Employees": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"BirthDate": {
"type": "string",
"format": "date-time"
},
"EmpNum": {
"type": "number"
},
"FirstName": {
"type": "string"
},
"Gender": {
"type": "string"
},
"LastName": {
"type": "string"
},
"LicenseNumber": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": ["EmpNum", "LastName", "FirstName", "Street", "ZipCode", "BirthDate" ]
}
}
}
}
I have json as shown below:
{
"Employees": [
{
"EmpNum": 3,
"FirstName": "Finder",
"LastName": "Path",
"Street": "392 CDI CDIJUW",
"ZipCode": "12345",
"BirthDate": "1943-05-19T04:00:00Z",
"Gender": "M"
},
{
"EmpNum": 3,
"FirstName": "",
"LastName": "Path",
"Street": "392 CDI CDIJUW",
"ZipCode": "12345",
"BirthDate": "1943-05-19T04:00:00Z",
"Gender": "M"
}
]
}
Even though I have set a field to an empty string, it still takes as a valid request and proceeds further.

If you intentionally set FirstName to be an empty string and want to invalidate it, try adding minLength:
"FirstName": {
"type": "string",
"minLength": 1
},

Related

Json Schema: Require a property only when a specific property is present

good day everyone, I am new here,
I have a json response looking like this
{
"Number": "20.09.00001",
"Supplier": {
"Name": "John Doe",
"Title": "Mr.",
"FirstName": "John",
"LastName": "Doe",
"Phone": "0212341234",
"Email": "foobar#gmail.com",
"Code": "Foo123",
"Gender": "Male"
}
}
I want to make inside the supplier properties required so I make a JSON schema validation looking like this
{
"type": "object",
"required": [
"Supplier"
],
"properties": {
"Supplier": {
"type": "object",
"required": [
"Name",
"Title",
"FirstName",
"LastName",
"Phone",
"Email",
"Code",
"Gender"
],
"properties": {
"Name": {
"type": "string"
},
"Title": {
"type": "string"
},
"FirstName": {
"type": "string"
},
"LastName": {
"type": "string"
},
"Phone": {
"type": "string"
},
"Email": {
"type": "string"
},
"Code": {
"type": "string"
},
"Gender": {
"type": "string"
}
}
}
}
}
but the problem is, the inside of supplier properties are not always present, when it's not supplied, it will only return empty objects like this
{
"Number": "20.09.00001",
"Supplier": {}
}
how can I validate only IF the inside supplier returns full property object, and ignore if the supplier returns an empty object?
I have tried using if else and anyOff but resulting in no luck.
my code with if else that did not work:
"Supplier": {
"type": ["object"],
"if": {
"properties": {
"Name": {
"type": ["string", "null"]
},
"Title": {
"type": ["string", "null"]
},
"FirstName": {
"type": ["string", "null"]
},
"LastName": {
"type": ["string", "null"]
},
"Phone": {
"type": ["string", "null"]
},
"Email": {
"type": ["string", "null"]
},
"Code": {
"type": ["string", "null"]
},
"Gender": {
"type": ["string", "null"]
}
}
},
"then": {
"required": [
"Name",
"Title",
"FirstName",
"LastName",
"Phone",
"Email",
"Code",
"Gender"
]
},
"else": {
"required": []
}
}
I think anyOf is the best approach in this case. The object is either empty or all the properties are required. There are a couple of way to assert that an object is empty. You could use "maxProperties": 0 or "const": {}.
{
"type": "object",
"properties": {
... all your properties ...
},
"anyOf": [
{ "const": {} },
{ "required": [ ... all required properties ... ] }
]
}

allow one of all based on the same base type

I have the following json schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema validating people and vehicles",
"definitions": {
"base": {
"properties": {
"age": {
"type": "integer"
}
},
"required": [
"age"
]
},
"person": {
"$ref": "#/definitions/base",
"additionalProperties": false,
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"sport": {
"type": "string"
}
},
"required": [
"firstName"
]
},
"vehicle": {
"$ref": "#/definitions/base",
"additionalProperties": false,
"properties": {
"vehicle": {
"type": "string"
},
"price": {
"type": "integer"
}
}
}
},
"type": "object",
"oneOf": [
{
"$ref": "#/definitions/person",
},
{
"$ref": "#/definitions/vehicle",
}
]
}
And i want it to validate against
{"firstName":"John", "lastName":"Doe", "sport": "football", "age": 15}
and the following
{"type": "car", "price": 100, "age": 3}
I get the following error JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1..
Why is it valid against more then one? (firstName is only defined in person and type is only defined in vehicle.)
JSON Schema doesn't support inheritance.
See:
https://github.com/json-schema-org/json-schema-spec/issues/348
https://spacetelescope.github.io/understanding-json-schema/reference/combining.html
JSon schema and Inheritance

I want to validate the following JSON schema in datapower using schema file

{
"email": "005foobar#gmail.com",
"phone": "9867534210",
"country_code": "91",
"firstname":"Rakesh",
"surname" :"A R"
}
I want either firstname or surname to be in my request(like one of: "firstname" or "surname")
For example:
{
"email": "005foobar#gmail.com",
"phone": "9867534210",
"country_code": "91",
"firstname":"Rakesh"
}
or
{
"email": "005foobar#gmail.com",
"phone": "9867534210",
"country_code": "91",
"surname":"A R"
}
Can you please help me with the json schema for my requirement?
For example something like this. It is a little bit complex, but if you want exactly EITHER surname OR fisrtName but NOT together and NOT absence of them it is what you need.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"email": {
"type": "string"
},
"phone": {
"type": "string"
},
"country_code": {
"type": "string"
}
},
"type": "object",
"oneOf": [
{
"properties": {
"email": {
"$ref": "#/definitions/email"
},
"phone": {
"$ref": "#/definitions/phone"
},
"county_code": {
"$ref": "#/definitions/country_code"
},
"firstname": {
"type": "string"
}
},
"required": [
"email",
"phone",
"country_code",
"firstname"
]
},
{
"properties": {
"email": {
"$ref": "#/definitions/email"
},
"phone": {
"$ref": "#/definitions/phone"
},
"county_code": {
"$ref": "#/definitions/country_code"
},
"surname": {
"type": "string"
}
},
"required": [
"email",
"phone",
"country_code",
"surname"
]
}
]
}

Fail to validate schema and correctly use additionalProperties

I'm trying to validate my JSON schema and use the additionalProperties: false to confirm there is not other properties.
My responseBody look like this:
[
{
"id": 1234567890987654,
"email": "eemail#domain.com",
"civility": 0,
"firstname": "john",
"lastname": "do",
"function": null,
"phone": null,
"cellphone": null,
"role": 1,
"passwordws": "jdnfjnshn55fff5g8"
},
{
...}
]
In the postman tests, I add this
var schema = {
"type": "array",
"properties": {
"id": {"type":"number"},
"email": {"type":"string"},
"civility": {"type":"number"},
"firstname": {"type":"string"},
"lastname": {"type":"string"},
"function": {"type":"string"},
"cellphone": {"type":"string"},
"role": {"type":"number"},
"passwordws": {"type":"string"},
},
"additionalProperties": false,
"required": ["id", "email", "civility", "role", "passwordws"]
};
var data = JSON.parse(responseBody);
var result = tv4.validateResult(data, schema);
tests["Valid schema"] = result.valid;
the test should return FAIL because I deleted the "phone" properties from schema, but test still run valid...
I tried to change the schema to {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} but test still return PASS instead of FAIL... any idea ?
Your response is an array of object, and i saw:
the array's object is not define
type id define with "number" instead of "integer"
Try this:
var schema = {
"type":"array",
"items": { "$ref": "#/definitions/MyObject" }
"definitions" : {
"MyObject" : {
"type":"object",
"required" : ["id", "email", "civility", "role", "passwordws"],
"properties": {
"id": {"type":"integer"},
"email": {"type":"string"},
"civility": {"type":"integer"},
"firstname": {"type":"string"},
"lastname": {"type":"string"},
"function": {"type":"string"},
"phone": {"type":"string"},
"cellphone": {"type":"string"},
"role": {"type":"integer"},
"passwordws": {"type":"string"}
},
"additionalProperties": false,
},
},
};
After some test, and log the result, the error was due to the null value I received sometimes in objects.
I changed the schema you sent me
{
"type": "array",
"items": {
"$ref": "#/definitions/MyObject"
},
"definitions": {
"MyObject": {
"type": "object",
"required": ["id", "email", "civility", "role", "passwordws"],
"properties": {
"id": {
"type": "integer"
},
"email": {
"type": "string"
},
"civility": {
"type": "integer"
},
"firstname": {
"type": ["string", "null"]
},
"lastname": {
"type": ["string", "null"]
},
"function": {
"type": ["string", "null"]
},
"phone": {
"type": ["string", "null"]
},
"cellphone": {
"type": ["string", "null"]
},
"role": {
"type": "integer"
},
"passwordws": {
"type": "string"
}
},
"additionalProperties": false
}
}
};
and I'm now able to validate correctly the schema.
Many thanks

JSON Schema: Require properties on an optional object type

I have defined a customer object type in my json schema:
"customer": {
"type": "object",
"properties": {
"id": { "type": "string" },
"first_name": { "type": "string" },
"last_name": { "type": "string"},
"email": { "type": "string" },
"billing_address": { "$ref": "#\/definitions\/street_address" },
"shipping_address": { "$ref": "#\/definitions\/street_address" },
},
"required": [ "id", "first_name", "last_name", "email", "billing_address"]
},
I would like to validate shipping_address (optional object) if it is sent and reject it if it is missing the required fields. Here is the street_address object definition:
"street_address": {
"type": "object",
"properties": {
"first_name": {"type": "string" },
"last_name": { "type": "string" },
"address": { "type": "string" },
"address2": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"zip_code": { "type": "string"},
"country_code": { "type": "string"},
"phone": { "type": "string"},
"fax": {"type": "string"}
},
"required": [
"first_name",
"last_name",
"address",
"city",
"state",
"zip_code",
"country_code"
]
},
How do I configure my JSON schema to accomplish this? When I send a shipping address now, the fields inside the object do not get validated.
You are using the reference "$ref": "#\/definitions\/street_address" (btw, you dont have to escape the slashes). In that case the street_address definition must be in in the same document, inside the "defintions" node. So your schema file looks like this
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "customer",
"type": "object",
"properties": {
"id": { "type": "string" },
"first_name": { "type": "string" },
"last_name": { "type": "string"},
"email": { "type": "string" },
"billing_address": { "$ref": "#/definitions/street_address" },
"shipping_address": { "$ref": "#/definitions/street_address" },
},
"required": [ "id", "first_name", "last_name", "email", "billing_address"],
"definitions" : {
"street_address" : {
/* here comes the street_address definition */
},
/* other entity definitions */
}
}
I'm using the nodejs module jayschema (see https://github.com/natesilva/jayschema) for validation, and it works fine that way.