Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description to Marker #307

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'