Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active August 11, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/d9c04724e63f3d327bd3ff7534bc0968 to your computer and use it in GitHub Desktop.
Save ThomasG77/d9c04724e63f3d327bd3ff7534bc0968 to your computer and use it in GitHub Desktop.
var fs = require('fs');
// Node.js require:
var Ajv = require('ajv');
// or ESM/TypeScript import
//import Ajv from 'ajv';
var GenerateSchema = require('generate-schema')
jsonbody = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102,0.5]},"properties":{"prop0":"value0"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[102,0],[103,1],[104,0],[105,1]]},"properties":{"prop0":"value0","prop1":0}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{"prop0":"value0","prop1":{"this":"that"}}}]};
var schema = GenerateSchema.json(jsonbody);
let data = JSON.stringify(schema);
fs.writeFileSync('json_schema.json', data);
schema = {
"$id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"type": "object",
"properties": {
"type": {
"type": "string"
},
"features": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"geometry": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"coordinates": {
"type": "array"
}
}
},
"properties": {
"type": "object"
}
},
"required": ["type", "geometry", "properties"]
}
}
}
}
var ajv = new Ajv({schemaId: 'id'});
// If you want to use both draft-04 and draft-06/07 schemas:
// var ajv = new Ajv({schemaId: 'auto'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
var validate = ajv.compile(schema);
var valid = validate(jsonbody);
if (!valid) console.log(validate.errors);
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{"$schema":"http://json-schema.org/draft-04/schema#","type":"object","properties":{"type":{"type":"string"},"features":{"type":"array","items":{"type":"object","properties":{"type":{"type":"string"},"geometry":{"type":"object","properties":{"type":{"type":"string"},"coordinates":{"type":"array","items":{"oneOf":[{"type":"number"},{"type":"number"},{"type":"number"},{"type":"number"},{"type":"number"}],"type":"array"}}}},"properties":{"type":"object","properties":{"prop0":{"type":"string"},"prop1":{"type":"object","properties":{"this":{"type":"string"}}}}}},"required":["type","geometry","properties"]}}}}
{
"name": "json-schema-validator-debug",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ajv": "^6.12.3",
"generate-schema": "^2.6.0"
}
}
import json
from jsonschema import validate
# ...
def fonction_demo(dict_to_test, dict_valid):
try:
validate(dict_to_test, dict_valid)
except Exception as valid_err:
print("Validation KO: {}".format(valid_err))
raise valid_err
else:
# Realise votre travail
print("JSON validé")
if __name__ == '__main__':
with open("./json_ok.json", "r") as fichier:
dict_to_test = json.load(fichier)
with open("./json_schema.json", "r") as fichier:
dict_valid = json.load(fichier)
fonction_demo(dict_to_test, dict_valid)
@ThomasG77
Copy link
Author

Heterogeneous coordinates with different nesting is the cause of the issue we patched.

With the following no more issues as the array in coordinates is nested the same way between features.

jsonbody = {"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Point","coordinates":[103,1]},"properties":{"prop0":"value0","prop1":0}},
{"type":"Feature","geometry":{"type":"Point","coordinates":[100,0]},"properties":{"prop0":"value4","prop1": {"this": "that"}}},
{"type":"Feature","geometry":{"type":"Point","coordinates":[102,0.5]},"properties":{"prop0":"val", "prop1": 2}}
]};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment