From d0d137da3dd27bde3c6cf4f9ea07b5dd2bd2dba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Sun, 13 Jul 2014 15:57:42 +0200 Subject: [PATCH] Improved validation --- nif_server.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/nif_server.py b/nif_server.py index c11a907..a6266ec 100644 --- a/nif_server.py +++ b/nif_server.py @@ -24,25 +24,31 @@ import json nif_server = Blueprint("NIF Sentiment Analysis Server", __name__) PARAMS = {"input": {"aliases": ["i", "input"], + "required": True, "help": "Input text" }, "informat": {"aliases": ["f", "informat"], + "required": False, "default": "text", "options": ["turtle", "text"], }, "intype": {"aliases": ["intype", "t"], + "required": False, "default": "direct", "options": ["direct", "url", "file"], }, "outformat": {"aliases": ["outformat", "o"], "default": "json-ld", + "required": False, "options": ["json-ld"], }, "language": {"aliases": ["language", "l"], - "default": None, - "options": ["es", "en"], - }, + "default": None, + "required": False, + "options": ["es", "en"], + }, "urischeme": {"aliases": ["urischeme", "u"], + "required": False, "default": "RFC5147String", "options": "RFC5147String" }, @@ -59,20 +65,25 @@ def get_params(req): raise ValueError("Invalid data") outdict = {} - missingParams = [] + wrongParams = {} for param, options in PARAMS.iteritems(): for alias in options["aliases"]: if alias in indict: outdict[param] = indict[alias] if param not in outdict: - if "default" in options: - if options["default"]: - outdict[param] = options["default"] + if options.get("required", False): + wrongParams[param] = PARAMS[param] else: - missingParams.append(param) - if missingParams: - message = {"status": "failed", "message": "Missing parameters"} - message["parameters"] = {param:PARAMS[param] for param in missingParams} + if "default" in options: + outdict[param] = options["default"] + else: + 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)) return outdict