This is the place to share and discuss new xarf schemas. The schemas are written in json schema and use its extension mechanisms to allow sharing common sub schemas.
pip install jsonschema
python validate.py xarf.schema.json samples/*.json
If it validation fails for a sample you can get better error messages by using the specific schema:
python validate.py rpz.schema.json samples/rpz_sample.json
If you want to start validating xarf documents in your code you can use this snippet:
import jsonschema
schema_link = {'$ref': 'https://raw.githubusercontent.com/xarf/schema-discussion/master/xarf.schema.json'}
def validate_xarf(document):
jsonschema.validate(document, schema_link)
schema_link is only a reference to the mail xarf schema root, so that everything else is loaded from the web. The schemas are cached after first load.
The validate_xarf method can be called from your code (this is just an example):
import json
from jsonschema.exceptions import ValidationError
with open('samples/rpz_sample.json') as json_file:
document = json.load(json_file)
try:
validate_xarf(document)
except ValidationError as e:
//handle error here. For example return 400 status code in web app
File(s) | Content |
---|---|
xarf.schema.json | contains links to all specific schemas |
xarf_shared.schema.json | reusable sub schemas |
*.schema.json | specific schemas |
validate.py | script for validating documents against the schemas |
samples/* | example documents for the schemas |
- Add a new schema as [subtype].schema.json and try to reuse as much as possible from xarf_shared.schema.json
- Add an example sample to samples/*
- Add the new schema to the list in xarf.schema.json
- Discuss and improve
For some json schema tools and use cases (code generation, e.g.) you need a single schema file because for one reason or another the dereferencing doesn't work. The following steps allow to create a single schema file on the fly.
Download and Install from here
npm install jsonfile json-schema-ref-parser
const $RefParser = require('json-schema-ref-parser');
const jsonfile = require('jsonfile')
const fullUri = 'https://raw.githubusercontent.com/xarf/schema-discussion/master/xarf.schema.json'
$RefParser.dereference(fullUri)
.then(function(schema) {
var file = 'xarf_full.schema.json'
jsonfile.writeFile(file, schema, function (err) {
if (err != null){
console.error(err)
process.exit(2)
}
console.error('done')
process.exit()
})
})
.catch(function(err) {
console.error(err)
});
node script.js
The full schema can now be found in xarf_full.schema.json