Permalink
Browse files

Added default_to validation function.

  • Loading branch information...
1 parent 5fda2ed commit 26ebd204ceb6207a6f6a28b4761e100a23cf085b @decbis decbis committed with Jul 26, 2012
Showing with 14 additions and 0 deletions.
  1. +14 −0 voluptuous.py
View
@@ -765,6 +765,20 @@ def title(v):
return str(v).title()
+def default_to(default_value, msg=None):
+ """Sets a value to default_value if none provided.
+
+ >>> s = Schema(default_to(42))
+ >>> s(None)
+ 42
+ """
+ def f(v):
+ if v is None:
+ v = default_value
+ return v
+ return f
+
+
if __name__ == '__main__':
import doctest
doctest.testmod()

0 comments on commit 26ebd20

Please sign in to comment.