Skip to content

Instantly share code, notes, and snippets.

@gadamc
Last active December 11, 2015 20:48
Show Gist options
  • Save gadamc/4657448 to your computer and use it in GitHub Desktop.
Save gadamc/4657448 to your computer and use it in GitHub Desktop.
simple use of json schema
{
"type": "measurement",
"grouping": "EXO(2008)",
"sample": {
"m_name": "Norddeutsche Affinerie, NSOV copper",
"m_description": "Norddeutsche Affinerie, NSOV copper made May 2002",
"m_id": "1",
"m_source": "Norddeutsche Affinerie",
"m_owner": {
"name": "a",
"contact": "b"
},
"u_extra":{"anumber":5, "astring":"hello"}
},
"measurement": {
"m_institution": "Shiva Inc.",
"m_technique": "GD-MS",
"m_date": ["2012-3-4"],
"m_requestor": {
"name": "a",
"contact": "b"
},
"m_practitioner": {
"name": "a",
"contact": "b"
},
"m_description": "For each of K, Th, and U, natural terrestrial abundance ratios were used to covert from isotopic to total elemental abundances.",
"m_results": [
{ "isotope":"K", "value":0.4, "error":[0.01], "unit":"ppb" },
{ "isotope":"Th", "limit":5, "cl":95, "unit":"ppt" },
{ "isotope":"U", "limit":5, "cl":95, "unit":"ppt" }
]
},
"data_source": {
"m_reference": "D.S.Leonard et al., Nucl. Instr. and Meth. A 591 (2008) (10.1016/j.nima.2008.03.001)",
"m_input": {
"name": "James Loach",
"contact": "[email protected]",
"date": "2013-01-09"
},
"u_extra" : {"sourceInfo":"in the cellar"},
"m_notes": "Uncertainties based on notes in table caption."
},
"specification": 1.01
}
#first, download these data to a local directory
#
#wget https://gist.github.com/raw/4657448/5f62088de8232b08af179afd4ae4b380bd5c35e2/schema.json
#wget https://gist.github.com/raw/4657448/c520101e57b06c1c00b2e8e01d5ffbcc90f6cc8f/data.json
#
import json
schdoc = json.load( open('schema.json') )
ddoc = json.load( open('data.json') )
import jsonschema # you need to sudo easy_install jsonschema.
try:
jsonschema.validate(ddoc, schdoc) #this raises an error if document is not valid, otherwise it returns None
except Exception as e:
print 'The data was not valid!'
raise e
{
"description": "",
"title": "AARM database data document schema",
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-03/schema#",
"required": false,
"type": "object",
"properties": {
"sample": {
"additionalProperties": false,
"required": true,
"type": "object",
"properties": {
"u_extra":{
"type":"object",
"additionalProperties":false,
"patternProperties":{
".+":{ "type":["string","number"]}
}
},
"m_id": {
"required": false,
"type": "string",
"title": "Identification number"
},
"m_owner": {
"required": false,
"type": "object",
"properties": {
"contact": {
"required": true,
"type": "string",
"title": "Institution or email/postal address"
},
"name": {
"required": true,
"type": "string",
"title": "the owner's name"
}
}
},
"m_description": {
"required": false,
"type": "string",
"title": "detailed description"
},
"m_source": {
"required": false,
"type": "string",
"title": "where the source came from"
},
"m_name": {
"required": true,
"type": "string",
"title": "short description"
}
}
},
"data_source": {
"additionalProperties": false,
"required": true,
"type": "object",
"properties": {
"u_extra":{
"type":"object",
"additionalProperties":false,
"patternProperties":{
".+":{ "type":["string","number"]}
}
},
"m_notes": {
"required": false,
"type": "string",
"title": "Comments on issues with data entry"
},
"m_input": {
"required": true,
"type": "object",
"properties": {
"date": {
"pattern":"^(18|19|20)[0-9]{2}-[0-9]{1,2}-[0-9]{1,2}$",
"required": true,
"type": "string",
"description": "must be in the form of YYYY-MM-DD",
"title": "creation date of this document"
},
"contact": {
"required": true,
"type": "string",
"title": "Institution or email/postal address"
},
"name": {
"required": true,
"type": "string",
"title": "The name of the creator of this document"
}
}
},
"m_reference": {
"required": true,
"type": "string",
"title": "A reference to data source, such as a publication"
}
}
},
"measurement": {
"additionalProperties": false,
"required": true,
"type": "object",
"properties": {
"u_extra":{
"type":"object",
"additionalProperties":false,
"patternProperties":{
".+":{ "type":["string","number"]}
}
},
"m_requestor": {
"required": false,
"type": "object",
"properties": {
"contact": {
"required": true,
"type": "string",
"title": "Institution or email/postal address"
},
"name": {
"required": true,
"type": "string",
"title": "The person's name"
}
},
"title": "Responsible for commissioning/managing/funding of measurement"
},
"m_results": {
"uniqueItems": true,
"additionalItems": false,
"title": "A list of results",
"minItems": 0,
"items": {
"type": [
{
"type": "object",
"properties": {
"value": {
"minimum": 0,
"required": true,
"type": "number",
"exclusiveMinimum": true
},
"error": {
"title": "68% confidence level on measurement",
"minItems": 1,
"items": {
"exclusiveMinimum":true,
"minimum": 0,
"type": "number"
},
"required": true,
"maxItems": 2,
"uniqueItems": false,
"type": "array"
},
"unit": {
"pattern": "pct|ppm|ppb|ppt|ppq|mBq/kg|uBq/kg|nBq/kg",
"required": true,
"type": "string"
},
"isotope": {
"pattern": "[A-Z][a-z]{0,1}-[0-9]{0,3}|[A-Z][a-z]{0,1}",
"required": true,
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"limit": {
"minimum": 0,
"required": true,
"type": "number",
"exclusiveMinimum": true
},
"cl": {
"exclusiveMaximum": true,
"exclusiveMinimum": true,
"required": true,
"maximum": 100,
"minimum": 0,
"type": "number"
},
"unit": {
"pattern": "pct|ppm|ppb|ppt|ppq|mBq/kg|uBq/kg|nBq/kg",
"required": true,
"type": "string"
},
"isotope": {
"pattern": "[A-Z][a-z]{0,1}-[0-9]{0,3}|[A-Z][a-z]{0,1}",
"required": true,
"type": "string"
}
}
}
]
},
"required": false,
"type": "array"
},
"m_practitioner": {
"required": false,
"type": "object",
"properties": {
"contact": {
"required": true,
"type": "string",
"title": "Institution or email/postal address"
},
"name": {
"required": true,
"type": "string",
"title": "The person's name"
}
},
"title": "Person responsible for the measurement"
},
"m_technique": {
"required": false,
"type": "string"
},
"m_date": {
"minItems": 1,
"items": {
"pattern":"^(18|19|20)[0-9]{2}-[0-9]{1,2}-[0-9]{1,2}$",
"type":"string"
},
"required": false,
"maxItems": 2,
"uniqueItems": false,
"type": "array"
},
"m_institution": {
"required": false,
"type": "string",
"title": "Name of Institution where measurement was made"
},
"m_description": {
"required": false,
"type": "string",
"title": "Detailed description of the procedures for data acquisition and analysis"
}
}
},
"_rev": {
"required": false,
"type": "string",
"description": "this is NOT required since it is not required to upload the document to couchdb",
"title": "couchdb _rev document revision number"
},
"_id": {
"required": false,
"type": "string",
"description": "this is NOT required since it is not required to upload the document to couchdb",
"title": "couchdb _id document identifier"
},
"type": {
"required": true,
"enum": ["measurement"],
"title": "Must be set to measurement"
},
"specification": {
"required": true,
"enum": [1.01],
"description": "must be set to 1.01",
"title": "The version number of the specification"
},
"grouping": {
"required": true,
"type": "string",
"description": "Usually this will be the name of the experiment or collaboration that made the measurement",
"title": "The group to which the document belongs"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment