Last active
August 11, 2020 18:28
-
-
Save ThomasG77/d9c04724e63f3d327bd3ff7534bc0968 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"$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"]}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.