json-schema
PHP implementation of JSON schema. Fork of the <a href="http://jsonschemaphpv.sourceforge.net/">http://jsonschemaphpv.sourceforge.net/</a> project
What are the features present in the schema draft 4 that are not in the JSON schema draft 3 produced by IETF ?
Source: (StackOverflow)
I am programmatically generating a JSON-Schema schema. I wish to ensure that the schema is valid. Is there a schema I can validate my schema against?
Please note my use of schema twice in that sentence and the title. I don't want to validate data against my schema, I want to validate my schema.
Source: (StackOverflow)
Is anyone aware of a tool or approach from which we can generate a JSON schema from XML schema or XML schema from JSON schema by Java?
Source: (StackOverflow)
My JSON string would be formatted as:
{
"count":3,
"data":[
{
"a":{"ax":1}
},
{
"b":{"bx":2}
},
{
"c":{"cx":4}
}
]
}
The data
array contains many a
and b
and c
. And no other kinds of objects.
If count==0
, data
should be an empty array []
.
I'm using https://github.com/hoxworth/json-schema to validate such JSON objects in Ruby.
require 'rubygems'
require 'json-schema'
p JSON::Validator.fully_validate('schema.json',"test.json")
The schema.json
is:
{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":true,
"properties":{
"count": { "type":"number", "id": "count", "required":true },
"data": { "type":"array", "id": "data", "required":true,
"items":[
{ "type":"object", "required":false, "properties":{ "a": { "type":"object", "id": "a", "required":true, "properties":{ "ax": { "type":"number", "id": "ax", "required":true } } } } },
{ "type":"object", "required":false, "properties":{ "b": { "type":"object", "id": "b", "required":true, "properties":{ "bx": { "type":"number", "id": "bx", "required":true } } } } },
{ "type":"object", "required":false, "properties":{ "c": { "type":"object", "id": "c", "required":true, "properties":{ "cx": { "type":"number", "id": "cx", "required":true } } } } }
]
}
}
}
But this for test.json
will pass the validation while I suppose it should fail:
{
"count":3,
"data":[
{
"a":{"ax":1}
},
{
"b":{"bx":2}
},
{
"c":{"cx":2}
},
{
"c": {"z":"aa"}
}
]
}
And this as test.json
will fail, while I suppose it should pass:
{
"count":3,
"data":[
{
"a":{"ax":1}
},
{
"b":{"bx":2}
}
]
}
Seems the wrong schema is validating that the data
array contains a,b,c
once.
What the right schema should be?
Source: (StackOverflow)
Let's say I have the following JSON Schema
{
"name":"Product",
"type":"object",
"properties":{
"id":{
"type":"number",
"required":true
},
"name":{
"description":"Name of the product",
"required":true
},
"price":{
"required":true,
"type": "number",
"minimum":0,
"required":true
},
"tags":{
"type":"array",
"items":{
"type":"any"
}
}
}
}
But, instead of tags being an array, I'd like it to be part of the root schema. So you could specify any property, but I give special attention to "id", "name" and "price"
Which of the following would be the correct way of doing it, and which ones are completely wrong?
{
"name":"Product",
"type":"object",
"properties":{
"id":{
"type":"number",
"required":true
},
"name":{
"description":"Name of the product",
"required":true
},
"price":{
"required":true,
"type": "number",
"minimum":0,
"required":true
}
},
"additionalProperties": {
"type":"any"
}
}
{
"name":"Product",
"type":"object",
"properties":{
"id":{
"type":"number",
"required":true
},
"name":{
"description":"Name of the product",
"required":true
},
"price":{
"required":true,
"type": "number",
"minimum":0,
"required":true
}
},
"extends": {
"type":"any"
}
}
{
"name":"Product",
"type":["object","any"],
"properties":{
"id":{
"type":"number",
"required":true
},
"name":{
"description":"Name of the product",
"required":true
},
"price":{
"required":true,
"type": "number",
"minimum":0,
"required":true
}
}
}
I can come up with a few more (such as inverting roles of "any" and "object"), but they are all derivative of these three examples.
Source: (StackOverflow)
I'm dealing with data input in the form of json documents.
These documents need to have a certain format, if they're not compliant, they should be ignored. I'm currently using a messy list of 'if thens' to check the format of the json document.
I have been experimenting a bit with different python json-schema libraries, which works ok, but I'm still able to submit a document with keys not described in the schema, which makes it useless to me.
This example doesn't generate an exception although I would expect it:
#!/usr/bin/python
from jsonschema import Validator
checker = Validator()
schema = {
"type" : "object",
"properties" : {
"source" : {
"type" : "object",
"properties" : {
"name" : {"type" : "string" }
}
}
}
}
data ={
"source":{
"name":"blah",
"bad_key":"This data is not allowed according to the schema."
}
}
checker.validate(data,schema)
My question is twofold:
- Am I overlooking something in the schema definition?
- If not, is there another lightweight way to approach this?
Thanks,
Jay
Source: (StackOverflow)
I have tried
voluptuous
and schema
, both of which are simple and great in validation, but they both do exception-based error reporting, i.e. they fail on first error. Is there a way I can get all validation errors of data in Voluptuous or Schema?
I found jsonschema
that seems matching some of the requirements, but does not have validation for object keys, and custom function based validation (e.g. lambdas).
Requirement:
def myMethod(input_dict):
#input_dict should abide to this schema ->
# { 'id' : INT , 'name':'string 5-10 chars','hobbies': LIST OF STRINGS }
# for incorrect input like
# {'id': 'hello','name':'Dhruv','hobbies':[1,2,3] }
# I should be able to return all errors like
# ['id' is not integer,'hobbies' is not list of strings ]
Source: (StackOverflow)
I am trying to figure out how to set required
on my json-schema array of objects. The required property works fine on an object just not an array.
Here is the items part of my json schema:
"items": {
"type": "array",
"properties": {
"item_id": {"type" : "number"},
"quantity": {"type": "number"},
"price": {"type" : "decimal"},
"title": {"type": "string"},
"description": {"type": "string"}
},
"required": ["item_id","quantity","price","title","description"],
"additionalProperties" : false
}
Here is the json array I am sending over. The json validation should fail since I am not passing a description in these items.
"items": [
{
"item_id": 1,
"quantity": 3,
"price": 30,
"title": "item1 new name"
},
{
"item_id": 1,
"quantity": 16,
"price": 30,
"title": "Test Two"
}
]
Source: (StackOverflow)
I have Swagger API Declaration for services using Swagger v 1.2
My original feeling about Swagger was, that is is very close to JSON Schema (Draft 3 and lately Draft 4) and it shall be relatively easy to generate JSON Schema for request and response objects.
However, while part of the Swagger reuses JSON Schema structures, it turned out, it uses only subset of features, and it also introduces it's own inheritance in Models (using subTypes
and discriminator
.
Question: Is there any existing project or piece of code, which can generate from Swagger API Declaration usable JSON Schema?
Optimally JSON Schema Draft 4 and using Python (but I will be happy to find anything).
Source: (StackOverflow)
I'm looking to generate JSON Schema for a WebAPI, including documentation from the XML comments.
Its primarily so that I can then import that into our API docs (using apiary.io)
I've managed to get a workaround solution by adding swagger (and swashbuckle) and then using the raw link on each service - but ideally I'd like something a bit cleaner, that works across all apis (this has to be done per service / controller), and didnt have so many dependencies.
Before I go and look at how swagger is doing this and seeing if it can be extracted, would be good to know if there are existing ways to do this?
Source: (StackOverflow)
Hi all and thanks in advance.
I am attempting to create a JSON schema to enforce an array to contain one A and B object and N C objects, where A and B are C objects and N is an integer inclusively between 0 and infinity.
Therefor :
[A, B]
[A, B, C1]
[A, B, C1, .., CN]
Are all valid, though :
[A]
[A, C1]
[A, C1, .., CN]
Are not valid.
To make clear, A and B must be present. C objects are optional, though you may have as many as you would like.
C object schemas :
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "C Object",
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
},
"additionalProperties": false
}
So a C object is any valid JSON object containing only the properties "id" and "name" where "id" is an integer and "name" is a string.
A and B object schemas :
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "A Object",
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string",
"enum": ["A"]
}
},
"additionalProperties": false
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "B Object",
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string",
"enum": ["B"]
}
},
"additionalProperties": false
}
A and B objects differ from C objects in that there name value is enforced. The name value of an A object must be a value contained in the field enum, where enum contains a single value.
My most complete schema to date is :
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "To Date Solution",
"description": "So far this is the most complete attempt at enforcing values to be contained within a JSON structure using JSON schemas.",
"type": "array"
"items": {
"allOf": [
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "C Object",
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
},
"additionalProperties": false
}
]
}
}
This enforces that all objects contained within must be of type C, which A and B are, though I am unsure how to enforce that at least single instance of A and B are contained within my array.
Source: (StackOverflow)
Wondering if this is possible with schema draft 03. I've gotten dependencies working elsewhere, I think there is possibly just some creative use of them required in order to use them for specifying the required
property of some field.
My current best attempt (which doesn't work) should give you some idea of what I'm after. I want a value required by default, and optional when another field has a particular value.
{
"description" : "An address...",
"type" : "object",
"properties" : {
"postcode": {
"type" : "string",
// postcode should be required by default
"required" : true,
// postcode shouldn't be required if the country is new zealand
"dependencies" : {
"country" : {
"enum" : ["NZ", "NZL", "NEW ZEALAND"]
},
"postcode" : {
"required" : false
}
}
},
"country": {
"type" : "string",
"enum" : [
// various country codes and names...
],
"default" : "AUS"
}
}
}
Source: (StackOverflow)