1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 08:12:27 +00:00

Improved validation

This commit is contained in:
J. Fernando Sánchez 2014-07-13 15:57:42 +02:00
parent 92e0684359
commit d0d137da3d

View File

@ -24,25 +24,31 @@ import json
nif_server = Blueprint("NIF Sentiment Analysis Server", __name__) nif_server = Blueprint("NIF Sentiment Analysis Server", __name__)
PARAMS = {"input": {"aliases": ["i", "input"], PARAMS = {"input": {"aliases": ["i", "input"],
"required": True,
"help": "Input text" "help": "Input text"
}, },
"informat": {"aliases": ["f", "informat"], "informat": {"aliases": ["f", "informat"],
"required": False,
"default": "text", "default": "text",
"options": ["turtle", "text"], "options": ["turtle", "text"],
}, },
"intype": {"aliases": ["intype", "t"], "intype": {"aliases": ["intype", "t"],
"required": False,
"default": "direct", "default": "direct",
"options": ["direct", "url", "file"], "options": ["direct", "url", "file"],
}, },
"outformat": {"aliases": ["outformat", "o"], "outformat": {"aliases": ["outformat", "o"],
"default": "json-ld", "default": "json-ld",
"required": False,
"options": ["json-ld"], "options": ["json-ld"],
}, },
"language": {"aliases": ["language", "l"], "language": {"aliases": ["language", "l"],
"default": None, "default": None,
"required": False,
"options": ["es", "en"], "options": ["es", "en"],
}, },
"urischeme": {"aliases": ["urischeme", "u"], "urischeme": {"aliases": ["urischeme", "u"],
"required": False,
"default": "RFC5147String", "default": "RFC5147String",
"options": "RFC5147String" "options": "RFC5147String"
}, },
@ -59,20 +65,25 @@ def get_params(req):
raise ValueError("Invalid data") raise ValueError("Invalid data")
outdict = {} outdict = {}
missingParams = [] wrongParams = {}
for param, options in PARAMS.iteritems(): for param, options in PARAMS.iteritems():
for alias in options["aliases"]: for alias in options["aliases"]:
if alias in indict: if alias in indict:
outdict[param] = indict[alias] outdict[param] = indict[alias]
if param not in outdict: if param not in outdict:
if options.get("required", False):
wrongParams[param] = PARAMS[param]
else:
if "default" in options: if "default" in options:
if options["default"]:
outdict[param] = options["default"] outdict[param] = options["default"]
else: else:
missingParams.append(param) if "options" in PARAMS[param] and \
if missingParams: outdict[param] not in PARAMS[param]["options"]:
message = {"status": "failed", "message": "Missing parameters"} wrongParams[param] = PARAMS[param]
message["parameters"] = {param:PARAMS[param] for param in missingParams} if wrongParams:
message = {"status": "failed", "message": "Missing or invalid parameters"}
message["parameters"] = outdict
message["errors"] = {param:error for param, error in wrongParams.iteritems()}
raise ValueError(json.dumps(message)) raise ValueError(json.dumps(message))
return outdict return outdict