11from coreapi .compat import string_types
22from coreapi .document import Document , Link
3+ from coreapi .exceptions import NodeLookupError
34import collections
45
56
@@ -36,23 +37,6 @@ def validate_parameters(link, parameters):
3637
3738 Raises a `ValueError` if any parameters do not validate.
3839 """
39- provided = set (parameters .keys ())
40- required = set ([
41- field .name for field in link .fields if field .required
42- ])
43-
44- # Determine if any required field names not supplied.
45- missing = required - provided
46- missing = ['"' + item + '"' for item in sorted (missing )]
47- if missing :
48- fmt = 'Missing required parameter{plural}: {listing}'
49- plural = 's' if len (missing ) > 1 else ''
50- listing = ', ' .join (missing )
51- raise ValueError (fmt .format (
52- plural = plural ,
53- listing = listing
54- ))
55-
5640 # Ensure all parameter values are valid types.
5741 for value in parameters .values ():
5842 validate_is_primative (value )
@@ -65,7 +49,7 @@ def validate_keys_to_link(document, keys):
6549 Returns a two-tuple of (link, link_ancestors).
6650 """
6751 if not isinstance (keys , (list , tuple )):
68- msg = "'keys' must be a dot seperated string or a list of strings."
52+ msg = "'keys' must be a list of strings or intes ."
6953 raise TypeError (msg )
7054 if any ([
7155 not isinstance (key , string_types ) and not isinstance (key , int )
@@ -79,15 +63,22 @@ def validate_keys_to_link(document, keys):
7963 node = document
8064 link_ancestors = [Ancestor (document = document , keys = [])]
8165 for idx , key in enumerate (keys , start = 1 ):
82- node = node [key ]
66+ try :
67+ node = node [key ]
68+ except (KeyError , IndexError ):
69+ index_string = '' .join ('[%s]' % repr (key ).strip ('u' ) for key in keys )
70+ msg = 'Index %s did not reference a link. Key %s was not found.'
71+ raise NodeLookupError (msg % (index_string , repr (key ).strip ('u' )))
8372 if isinstance (node , Document ):
8473 ancestor = Ancestor (document = node , keys = keys [:idx ])
8574 link_ancestors .append (ancestor )
8675
8776 # Ensure that we've correctly indexed into a link.
8877 if not isinstance (node , Link ):
89- raise ValueError (
90- "Can only call 'action' on a Link. Got type '%s'." % type (node )
78+ index_string = '' .join ('[%s]' % repr (key ).strip ('u' ) for key in keys )
79+ msg = "Can only call 'action' on a Link. Index %s returned type '%s'."
80+ raise NodeLookupError (
81+ msg % (index_string , type (node ).__name__ )
9182 )
9283
9384 return (node , link_ancestors )
0 commit comments