diff --git a/schemas/commodity-v0.1.json b/schemas/commodity-v0.1.json new file mode 100644 index 0000000..4bb1f4a --- /dev/null +++ b/schemas/commodity-v0.1.json @@ -0,0 +1,103 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://schemas.elite-markets.net/eddn/commodity/1#", + "type": "object", + "additionalProperties": false, + "properties": { + "header": { + "id": "#header", + "type": "object", + "additionalProperties": false, + "properties": { + "uploaderID": { + "id": "#uploaderID", + "type": "string", + "additionalProperties": false + }, + "softwareName": { + "id": "#softwareName", + "type": "string", + "additionalProperties": false + }, + "softwareVersion": { + "id": "#softwareVersion", + "type": "string", + "additionalProperties": false + } + }, + "required": [ + "uploaderID", + "softwareName", + "softwareVersion" + ] + }, + "message": { + "id": "#message", + "type": "object", + "additionalProperties": false, + "properties": { + "categoryName": { + "id": "#categoryName", + "type": "string", + "additionalProperties": false + }, + "buyPrice": { + "id": "#buyPrice", + "type": "integer", + "additionalProperties": false + }, + "timestamp": { + "id": "#timestamp", + "type": "string", + "additionalProperties": false + }, + "stationStock": { + "id": "#stationStock", + "type": "integer", + "additionalProperties": false + }, + "stationName": { + "id": "#stationName", + "type": "string", + "additionalProperties": false + }, + "systemName": { + "id": "#systemName", + "type": "string", + "additionalProperties": false + }, + "demand": { + "id": "#demand", + "type": "integer", + "additionalProperties": false + }, + "sellPrice": { + "id": "#sellPrice", + "type": "integer", + "additionalProperties": false + }, + "itemName": { + "id": "#itemName", + "type": "string", + "additionalProperties": false + } + }, + "required": [ + "categoryName", + "buyPrice", + "timestamp", + "stationStock", + "stationName", + "systemName", + "demand", + "sellPrice", + "itemName" + ] + } + }, + "required": [ + "$schemaRef", + "header", + "message" + ] +} \ No newline at end of file diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index baaedbb..88246a1 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -30,6 +30,9 @@ for binding in Settings.GATEWAY_SENDER_BINDINGS: validator = Validator() +for schemaRef, schemaFile in Settings.GATEWAY_JSON_SCHEMAS.iteritems(): + validator.addSchemaResource(schemaRef, schemaFile) + def push_message(string_message): """ diff --git a/src/eddn/Validator.py b/src/eddn/Validator.py index e3be991..2b58294 100644 --- a/src/eddn/Validator.py +++ b/src/eddn/Validator.py @@ -1,3 +1,4 @@ +import simplejson from enum import IntEnum @@ -5,6 +6,12 @@ class Validator(object): schemas = {"http://example.com": {}} + def addSchemaResource(self, schemaRef, schemaFile): + if schemaRef in self.schemas.keys(): + raise Exception("Attempted to redefine schema for " + schemaRef) + schema = simplejson.load(open(schemaFile, "r")) + self.schemas[schemaRef] = schema + def validate(self, json_object): results = ValidationResults() diff --git a/src/eddn/conf/Settings.py b/src/eddn/conf/Settings.py index aa9b61a..c9b7064 100644 --- a/src/eddn/conf/Settings.py +++ b/src/eddn/conf/Settings.py @@ -24,6 +24,10 @@ GATEWAY_SENDER_BINDINGS = ["tcp://*:8500"] GATEWAY_IP_KEY_SALT = None +GATEWAY_JSON_SCHEMAS = { + "http://schemas.elite-markets.net/eddn/commodity/1": "../schemas/commodity-v0.1.json" + } + ############################################################################### # Logging Settings ###############################################################################