Allow field attributes which are lists to validate the type of the list items

Starting to apply this for tags too, however it is not correcting things
as would be expected.
This commit is contained in:
James Cammarata 2015-06-27 01:01:08 -04:00
parent f68223b9ed
commit bb8d87ceb6
3 changed files with 9 additions and 2 deletions

View file

@ -21,12 +21,13 @@ __metaclass__ = type
class Attribute:
def __init__(self, isa=None, private=False, default=None, required=False):
def __init__(self, isa=None, private=False, default=None, required=False, listof=None):
self.isa = isa
self.private = private
self.default = default
self.required = required
self.listof = listof
class FieldAttribute(Attribute):
pass

View file

@ -274,6 +274,10 @@ class Base:
elif attribute.isa == 'list':
if not isinstance(value, list):
value = [ value ]
if attribute.listof is not None:
for item in value:
if not isinstance(item, attribute.listof):
raise AnsibleParserError("the field '%s' should be a list of %s, but the item '%s' is a %s" % (name, attribute.listof, item, type(item)), obj=self.get_ds())
elif attribute.isa == 'dict' and not isinstance(value, dict):
raise TypeError()

View file

@ -19,6 +19,8 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from six import string_types
from ansible.errors import AnsibleError
from ansible.playbook.attribute import FieldAttribute
from ansible.template import Templar
@ -26,7 +28,7 @@ from ansible.template import Templar
class Taggable:
untagged = set(['untagged'])
_tags = FieldAttribute(isa='list', default=[])
_tags = FieldAttribute(isa='list', default=[], listof=(string_types,int))
def __init__(self):
super(Taggable, self).__init__()