Skip to content

Commit

Permalink
Add description to Marker
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Oct 31, 2017
1 parent 48d1ea8 commit 6180781
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 11 additions & 7 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,11 @@ def __repr__(self):
class Marker(object):
"""Mark nodes for special treatment."""

def __init__(self, schema_, msg=None):
def __init__(self, schema_, msg=None, description=None):
self.schema = schema_
self._schema = Schema(schema_)
self.msg = msg
self.description = description

def __call__(self, v):
try:
Expand Down Expand Up @@ -930,8 +931,9 @@ class Optional(Marker):
{'key2': 'value'}
"""

def __init__(self, schema, msg=None, default=UNDEFINED):
super(Optional, self).__init__(schema, msg=msg)
def __init__(self, schema, msg=None, default=UNDEFINED, description=None):
super(Optional, self).__init__(schema, msg=msg,
description=description)
self.default = default_factory(default)


Expand Down Expand Up @@ -971,8 +973,9 @@ class Exclusive(Optional):
... 'social': {'social_network': 'barfoo', 'token': 'tEMp'}})
"""

def __init__(self, schema, group_of_exclusion, msg=None):
super(Exclusive, self).__init__(schema, msg=msg)
def __init__(self, schema, group_of_exclusion, msg=None, description=None):
super(Exclusive, self).__init__(schema, msg=msg,
description=description)
self.group_of_exclusion = group_of_exclusion


Expand Down Expand Up @@ -1038,8 +1041,9 @@ class Required(Marker):
{'key': []}
"""

def __init__(self, schema, msg=None, default=UNDEFINED):
super(Required, self).__init__(schema, msg=msg)
def __init__(self, schema, msg=None, default=UNDEFINED, description=None):
super(Required, self).__init__(schema, msg=msg,
description=description)
self.default = default_factory(default)


Expand Down
16 changes: 15 additions & 1 deletion voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nose.tools import assert_equal, assert_raises, assert_true

from voluptuous import (
Schema, Required, Optional, Extra, Invalid, In, Remove, Literal,
Schema, Required, Exclusive, Optional, Extra, Invalid, In, Remove, Literal,
Url, MultipleInvalid, LiteralInvalid, TypeInvalid, NotIn, Match, Email,
Replace, Range, Coerce, All, Any, Length, FqdnUrl, ALLOW_EXTRA, PREVENT_EXTRA,
validate, ExactSequence, Equal, Unordered, Number, Maybe, Datetime, Date,
Expand Down Expand Up @@ -915,3 +915,17 @@ def test_PathExists():
schema = Schema(PathExists())
assert_raises(MultipleInvalid, schema, 3)
schema(os.path.abspath(__file__))


def test_description():
marker = Marker(Schema(str), description='Hello')
assert marker.description == 'Hello'

optional = Optional('key', description='Hello')
assert optional.description == 'Hello'

exclusive = Exclusive('alpha', 'angles', description='Hello')
assert exclusive.description == 'Hello'

required = Required('key', description='Hello')
assert required.description == 'Hello'

0 comments on commit 6180781

Please sign in to comment.