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,
"options": ["es", "en"], "required": False,
}, "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 "default" in options: if options.get("required", False):
if options["default"]: wrongParams[param] = PARAMS[param]
outdict[param] = options["default"]
else: else:
missingParams.append(param) if "default" in options:
if missingParams: outdict[param] = options["default"]
message = {"status": "failed", "message": "Missing parameters"} else:
message["parameters"] = {param:PARAMS[param] for param in missingParams} if "options" in PARAMS[param] and \
outdict[param] not in PARAMS[param]["options"]:
wrongParams[param] = PARAMS[param]
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