You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1004 lines
29 KiB

4 years ago
  1. import os
  2. import re
  3. import datetime
  4. import sys
  5. from functools import wraps
  6. from decimal import Decimal, InvalidOperation
  7. from voluptuous.schema_builder import Schema, raises, message
  8. from voluptuous.error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid,
  9. AnyInvalid, AllInvalid, MatchInvalid, UrlInvalid, EmailInvalid, FileInvalid, DirInvalid,
  10. RangeInvalid, PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid,
  11. DateInvalid, InInvalid, TypeInvalid, NotInInvalid, ContainsInvalid, NotEnoughValid,
  12. TooManyValid)
  13. if sys.version_info >= (3,):
  14. import urllib.parse as urlparse
  15. basestring = str
  16. else:
  17. import urlparse
  18. # Taken from https://github.com/kvesteri/validators/blob/master/validators/email.py
  19. USER_REGEX = re.compile(
  20. # dot-atom
  21. r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+"
  22. r"(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$"
  23. # quoted-string
  24. r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|'
  25. r"""\\[\001-\011\013\014\016-\177])*"$)""",
  26. re.IGNORECASE
  27. )
  28. DOMAIN_REGEX = re.compile(
  29. # domain
  30. r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+'
  31. r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?$)'
  32. # literal form, ipv4 address (SMTP 4.1.3)
  33. r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)'
  34. r'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
  35. re.IGNORECASE)
  36. __author__ = 'tusharmakkar08'
  37. def truth(f):
  38. """Convenience decorator to convert truth functions into validators.
  39. >>> @truth
  40. ... def isdir(v):
  41. ... return os.path.isdir(v)
  42. >>> validate = Schema(isdir)
  43. >>> validate('/')
  44. '/'
  45. >>> with raises(MultipleInvalid, 'not a valid value'):
  46. ... validate('/notavaliddir')
  47. """
  48. @wraps(f)
  49. def check(v):
  50. t = f(v)
  51. if not t:
  52. raise ValueError
  53. return v
  54. return check
  55. class Coerce(object):
  56. """Coerce a value to a type.
  57. If the type constructor throws a ValueError or TypeError, the value
  58. will be marked as Invalid.
  59. Default behavior:
  60. >>> validate = Schema(Coerce(int))
  61. >>> with raises(MultipleInvalid, 'expected int'):
  62. ... validate(None)
  63. >>> with raises(MultipleInvalid, 'expected int'):
  64. ... validate('foo')
  65. With custom message:
  66. >>> validate = Schema(Coerce(int, "moo"))
  67. >>> with raises(MultipleInvalid, 'moo'):
  68. ... validate('foo')
  69. """
  70. def __init__(self, type, msg=None):
  71. self.type = type
  72. self.msg = msg
  73. self.type_name = type.__name__
  74. def __call__(self, v):
  75. try:
  76. return self.type(v)
  77. except (ValueError, TypeError, InvalidOperation):
  78. msg = self.msg or ('expected %s' % self.type_name)
  79. raise CoerceInvalid(msg)
  80. def __repr__(self):
  81. return 'Coerce(%s, msg=%r)' % (self.type_name, self.msg)
  82. @message('value was not true', cls=TrueInvalid)
  83. @truth
  84. def IsTrue(v):
  85. """Assert that a value is true, in the Python sense.
  86. >>> validate = Schema(IsTrue())
  87. "In the Python sense" means that implicitly false values, such as empty
  88. lists, dictionaries, etc. are treated as "false":
  89. >>> with raises(MultipleInvalid, "value was not true"):
  90. ... validate([])
  91. >>> validate([1])
  92. [1]
  93. >>> with raises(MultipleInvalid, "value was not true"):
  94. ... validate(False)
  95. ...and so on.
  96. >>> try:
  97. ... validate([])
  98. ... except MultipleInvalid as e:
  99. ... assert isinstance(e.errors[0], TrueInvalid)
  100. """
  101. return v
  102. @message('value was not false', cls=FalseInvalid)
  103. def IsFalse(v):
  104. """Assert that a value is false, in the Python sense.
  105. (see :func:`IsTrue` for more detail)
  106. >>> validate = Schema(IsFalse())
  107. >>> validate([])
  108. []
  109. >>> with raises(MultipleInvalid, "value was not false"):
  110. ... validate(True)
  111. >>> try:
  112. ... validate(True)
  113. ... except MultipleInvalid as e:
  114. ... assert isinstance(e.errors[0], FalseInvalid)
  115. """
  116. if v:
  117. raise ValueError
  118. return v
  119. @message('expected boolean', cls=BooleanInvalid)
  120. def Boolean(v):
  121. """Convert human-readable boolean values to a bool.
  122. Accepted values are 1, true, yes, on, enable, and their negatives.
  123. Non-string values are cast to bool.
  124. >>> validate = Schema(Boolean())
  125. >>> validate(True)
  126. True
  127. >>> validate("1")
  128. True
  129. >>> validate("0")
  130. False
  131. >>> with raises(MultipleInvalid, "expected boolean"):
  132. ... validate('moo')
  133. >>> try:
  134. ... validate('moo')
  135. ... except MultipleInvalid as e:
  136. ... assert isinstance(e.errors[0], BooleanInvalid)
  137. """
  138. if isinstance(v, basestring):
  139. v = v.lower()
  140. if v in ('1', 'true', 'yes', 'on', 'enable'):
  141. return True
  142. if v in ('0', 'false', 'no', 'off', 'disable'):
  143. return False
  144. raise ValueError
  145. return bool(v)
  146. class _WithSubValidators(object):
  147. """Base class for validators that use sub-validators.
  148. Special class to use as a parent class for validators using sub-validators.
  149. This class provides the `__voluptuous_compile__` method so the
  150. sub-validators are compiled by the parent `Schema`.
  151. """
  152. def __init__(self, *validators, **kwargs):
  153. self.validators = validators
  154. self.msg = kwargs.pop('msg', None)
  155. def __voluptuous_compile__(self, schema):
  156. self._compiled = [
  157. schema._compile(v)
  158. for v in self.validators
  159. ]
  160. return self._run
  161. def _run(self, path, value):
  162. return self._exec(self._compiled, value, path)
  163. def __call__(self, v):
  164. return self._exec((Schema(val) for val in self.validators), v)
  165. def __repr__(self):
  166. return '%s(%s, msg=%r)' % (
  167. self.__class__.__name__,
  168. ", ".join(repr(v) for v in self.validators),
  169. self.msg
  170. )
  171. class Any(_WithSubValidators):
  172. """Use the first validated value.
  173. :param msg: Message to deliver to user if validation fails.
  174. :param kwargs: All other keyword arguments are passed to the sub-Schema constructors.
  175. :returns: Return value of the first validator that passes.
  176. >>> validate = Schema(Any('true', 'false',
  177. ... All(Any(int, bool), Coerce(bool))))
  178. >>> validate('true')
  179. 'true'
  180. >>> validate(1)
  181. True
  182. >>> with raises(MultipleInvalid, "not a valid value"):
  183. ... validate('moo')
  184. msg argument is used
  185. >>> validate = Schema(Any(1, 2, 3, msg="Expected 1 2 or 3"))
  186. >>> validate(1)
  187. 1
  188. >>> with raises(MultipleInvalid, "Expected 1 2 or 3"):
  189. ... validate(4)
  190. """
  191. def _exec(self, funcs, v, path=None):
  192. error = None
  193. for func in funcs:
  194. try:
  195. if path is None:
  196. return func(v)
  197. else:
  198. return func(path, v)
  199. except Invalid as e:
  200. if error is None or len(e.path) > len(error.path):
  201. error = e
  202. else:
  203. if error:
  204. raise error if self.msg is None else AnyInvalid(
  205. self.msg, path=path)
  206. raise AnyInvalid(self.msg or 'no valid value found',
  207. path=path)
  208. # Convenience alias
  209. Or = Any
  210. class All(_WithSubValidators):
  211. """Value must pass all validators.
  212. The output of each validator is passed as input to the next.
  213. :param msg: Message to deliver to user if validation fails.
  214. :param kwargs: All other keyword arguments are passed to the sub-Schema constructors.
  215. >>> validate = Schema(All('10', Coerce(int)))
  216. >>> validate('10')
  217. 10
  218. """
  219. def _exec(self, funcs, v, path=None):
  220. try:
  221. for func in funcs:
  222. if path is None:
  223. v = func(v)
  224. else:
  225. v = func(path, v)
  226. except Invalid as e:
  227. raise e if self.msg is None else AllInvalid(self.msg, path=path)
  228. return v
  229. # Convenience alias
  230. And = All
  231. class Match(object):
  232. """Value must be a string that matches the regular expression.
  233. >>> validate = Schema(Match(r'^0x[A-F0-9]+$'))
  234. >>> validate('0x123EF4')
  235. '0x123EF4'
  236. >>> with raises(MultipleInvalid, "does not match regular expression"):
  237. ... validate('123EF4')
  238. >>> with raises(MultipleInvalid, 'expected string or buffer'):
  239. ... validate(123)
  240. Pattern may also be a _compiled regular expression:
  241. >>> validate = Schema(Match(re.compile(r'0x[A-F0-9]+', re.I)))
  242. >>> validate('0x123ef4')
  243. '0x123ef4'
  244. """
  245. def __init__(self, pattern, msg=None):
  246. if isinstance(pattern, basestring):
  247. pattern = re.compile(pattern)
  248. self.pattern = pattern
  249. self.msg = msg
  250. def __call__(self, v):
  251. try:
  252. match = self.pattern.match(v)
  253. except TypeError:
  254. raise MatchInvalid("expected string or buffer")
  255. if not match:
  256. raise MatchInvalid(self.msg or 'does not match regular expression')
  257. return v
  258. def __repr__(self):
  259. return 'Match(%r, msg=%r)' % (self.pattern.pattern, self.msg)
  260. class Replace(object):
  261. """Regex substitution.
  262. >>> validate = Schema(All(Replace('you', 'I'),
  263. ... Replace('hello', 'goodbye')))
  264. >>> validate('you say hello')
  265. 'I say goodbye'
  266. """
  267. def __init__(self, pattern, substitution, msg=None):
  268. if isinstance(pattern, basestring):
  269. pattern = re.compile(pattern)
  270. self.pattern = pattern
  271. self.substitution = substitution
  272. self.msg = msg
  273. def __call__(self, v):
  274. return self.pattern.sub(self.substitution, v)
  275. def __repr__(self):
  276. return 'Replace(%r, %r, msg=%r)' % (self.pattern.pattern,
  277. self.substitution,
  278. self.msg)
  279. def _url_validation(v):
  280. parsed = urlparse.urlparse(v)
  281. if not parsed.scheme or not parsed.netloc:
  282. raise UrlInvalid("must have a URL scheme and host")
  283. return parsed
  284. @message('expected an Email', cls=EmailInvalid)
  285. def Email(v):
  286. """Verify that the value is an Email or not.
  287. >>> s = Schema(Email())
  288. >>> with raises(MultipleInvalid, 'expected an Email'):
  289. ... s("a.com")
  290. >>> with raises(MultipleInvalid, 'expected an Email'):
  291. ... s("a@.com")
  292. >>> with raises(MultipleInvalid, 'expected an Email'):
  293. ... s("a@.com")
  294. >>> s('t@x.com')
  295. 't@x.com'
  296. """
  297. try:
  298. if not v or "@" not in v:
  299. raise EmailInvalid("Invalid Email")
  300. user_part, domain_part = v.rsplit('@', 1)
  301. if not (USER_REGEX.match(user_part) and DOMAIN_REGEX.match(domain_part)):
  302. raise EmailInvalid("Invalid Email")
  303. return v
  304. except:
  305. raise ValueError
  306. @message('expected a Fully qualified domain name URL', cls=UrlInvalid)
  307. def FqdnUrl(v):
  308. """Verify that the value is a Fully qualified domain name URL.
  309. >>> s = Schema(FqdnUrl())
  310. >>> with raises(MultipleInvalid, 'expected a Fully qualified domain name URL'):
  311. ... s("http://localhost/")
  312. >>> s('http://w3.org')
  313. 'http://w3.org'
  314. """
  315. try:
  316. parsed_url = _url_validation(v)
  317. if "." not in parsed_url.netloc:
  318. raise UrlInvalid("must have a domain name in URL")
  319. return v
  320. except:
  321. raise ValueError
  322. @message('expected a URL', cls=UrlInvalid)
  323. def Url(v):
  324. """Verify that the value is a URL.
  325. >>> s = Schema(Url())
  326. >>> with raises(MultipleInvalid, 'expected a URL'):
  327. ... s(1)
  328. >>> s('http://w3.org')
  329. 'http://w3.org'
  330. """
  331. try:
  332. _url_validation(v)
  333. return v
  334. except:
  335. raise ValueError
  336. @message('not a file', cls=FileInvalid)
  337. @truth
  338. def IsFile(v):
  339. """Verify the file exists.
  340. >>> os.path.basename(IsFile()(__file__)).startswith('validators.py')
  341. True
  342. >>> with raises(FileInvalid, 'not a file'):
  343. ... IsFile()("random_filename_goes_here.py")
  344. >>> with raises(FileInvalid, 'Not a file'):
  345. ... IsFile()(None)
  346. """
  347. try:
  348. if v:
  349. v = str(v)
  350. return os.path.isfile(v)
  351. else:
  352. raise FileInvalid('Not a file')
  353. except TypeError:
  354. raise FileInvalid('Not a file')
  355. @message('not a directory', cls=DirInvalid)
  356. @truth
  357. def IsDir(v):
  358. """Verify the directory exists.
  359. >>> IsDir()('/')
  360. '/'
  361. >>> with raises(DirInvalid, 'Not a directory'):
  362. ... IsDir()(None)
  363. """
  364. try:
  365. if v:
  366. v = str(v)
  367. return os.path.isdir(v)
  368. else:
  369. raise DirInvalid("Not a directory")
  370. except TypeError:
  371. raise DirInvalid("Not a directory")
  372. @message('path does not exist', cls=PathInvalid)
  373. @truth
  374. def PathExists(v):
  375. """Verify the path exists, regardless of its type.
  376. >>> os.path.basename(PathExists()(__file__)).startswith('validators.py')
  377. True
  378. >>> with raises(Invalid, 'path does not exist'):
  379. ... PathExists()("random_filename_goes_here.py")
  380. >>> with raises(PathInvalid, 'Not a Path'):
  381. ... PathExists()(None)
  382. """
  383. try:
  384. if v:
  385. v = str(v)
  386. return os.path.exists(v)
  387. else:
  388. raise PathInvalid("Not a Path")
  389. except TypeError:
  390. raise PathInvalid("Not a Path")
  391. def Maybe(validator):
  392. """Validate that the object matches given validator or is None.
  393. :raises Invalid: if the value does not match the given validator and is not
  394. None
  395. >>> s = Schema(Maybe(int))
  396. >>> s(10)
  397. 10
  398. >>> with raises(Invalid):
  399. ... s("string")
  400. """
  401. return Any(None, validator)
  402. class Range(object):
  403. """Limit a value to a range.
  404. Either min or max may be omitted.
  405. Either min or max can be excluded from the range of accepted values.
  406. :raises Invalid: If the value is outside the range.
  407. >>> s = Schema(Range(min=1, max=10, min_included=False))
  408. >>> s(5)
  409. 5
  410. >>> s(10)
  411. 10
  412. >>> with raises(MultipleInvalid, 'value must be at most 10'):
  413. ... s(20)
  414. >>> with raises(MultipleInvalid, 'value must be higher than 1'):
  415. ... s(1)
  416. >>> with raises(MultipleInvalid, 'value must be lower than 10'):
  417. ... Schema(Range(max=10, max_included=False))(20)
  418. """
  419. def __init__(self, min=None, max=None, min_included=True,
  420. max_included=True, msg=None):
  421. self.min = min
  422. self.max = max
  423. self.min_included = min_included
  424. self.max_included = max_included
  425. self.msg = msg
  426. def __call__(self, v):
  427. if self.min_included:
  428. if self.min is not None and not v >= self.min:
  429. raise RangeInvalid(
  430. self.msg or 'value must be at least %s' % self.min)
  431. else:
  432. if self.min is not None and not v > self.min:
  433. raise RangeInvalid(
  434. self.msg or 'value must be higher than %s' % self.min)
  435. if self.max_included:
  436. if self.max is not None and not v <= self.max:
  437. raise RangeInvalid(
  438. self.msg or 'value must be at most %s' % self.max)
  439. else:
  440. if self.max is not None and not v < self.max:
  441. raise RangeInvalid(
  442. self.msg or 'value must be lower than %s' % self.max)
  443. return v
  444. def __repr__(self):
  445. return ('Range(min=%r, max=%r, min_included=%r,'
  446. ' max_included=%r, msg=%r)' % (self.min, self.max,
  447. self.min_included,
  448. self.max_included,
  449. self.msg))
  450. class Clamp(object):
  451. """Clamp a value to a range.
  452. Either min or max may be omitted.
  453. >>> s = Schema(Clamp(min=0, max=1))
  454. >>> s(0.5)
  455. 0.5
  456. >>> s(5)
  457. 1
  458. >>> s(-1)
  459. 0
  460. """
  461. def __init__(self, min=None, max=None, msg=None):
  462. self.min = min
  463. self.max = max
  464. self.msg = msg
  465. def __call__(self, v):
  466. if self.min is not None and v < self.min:
  467. v = self.min
  468. if self.max is not None and v > self.max:
  469. v = self.max
  470. return v
  471. def __repr__(self):
  472. return 'Clamp(min=%s, max=%s)' % (self.min, self.max)
  473. class Length(object):
  474. """The length of a value must be in a certain range."""
  475. def __init__(self, min=None, max=None, msg=None):
  476. self.min = min
  477. self.max = max
  478. self.msg = msg
  479. def __call__(self, v):
  480. if self.min is not None and len(v) < self.min:
  481. raise LengthInvalid(
  482. self.msg or 'length of value must be at least %s' % self.min)
  483. if self.max is not None and len(v) > self.max:
  484. raise LengthInvalid(
  485. self.msg or 'length of value must be at most %s' % self.max)
  486. return v
  487. def __repr__(self):
  488. return 'Length(min=%s, max=%s)' % (self.min, self.max)
  489. class Datetime(object):
  490. """Validate that the value matches the datetime format."""
  491. DEFAULT_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
  492. def __init__(self, format=None, msg=None):
  493. self.format = format or self.DEFAULT_FORMAT
  494. self.msg = msg
  495. def __call__(self, v):
  496. try:
  497. datetime.datetime.strptime(v, self.format)
  498. except (TypeError, ValueError):
  499. raise DatetimeInvalid(
  500. self.msg or 'value does not match'
  501. ' expected format %s' % self.format)
  502. return v
  503. def __repr__(self):
  504. return 'Datetime(format=%s)' % self.format
  505. class Date(Datetime):
  506. """Validate that the value matches the date format."""
  507. DEFAULT_FORMAT = '%Y-%m-%d'
  508. def __call__(self, v):
  509. try:
  510. datetime.datetime.strptime(v, self.format)
  511. except (TypeError, ValueError):
  512. raise DateInvalid(
  513. self.msg or 'value does not match'
  514. ' expected format %s' % self.format)
  515. return v
  516. def __repr__(self):
  517. return 'Date(format=%s)' % self.format
  518. class In(object):
  519. """Validate that a value is in a collection."""
  520. def __init__(self, container, msg=None):
  521. self.container = container
  522. self.msg = msg
  523. def __call__(self, v):
  524. try:
  525. check = v not in self.container
  526. except TypeError:
  527. check = True
  528. if check:
  529. raise InInvalid(self.msg or 'value is not allowed')
  530. return v
  531. def __repr__(self):
  532. return 'In(%s)' % (self.container,)
  533. class NotIn(object):
  534. """Validate that a value is not in a collection."""
  535. def __init__(self, container, msg=None):
  536. self.container = container
  537. self.msg = msg
  538. def __call__(self, v):
  539. try:
  540. check = v in self.container
  541. except TypeError:
  542. check = True
  543. if check:
  544. raise NotInInvalid(self.msg or 'value is not allowed')
  545. return v
  546. def __repr__(self):
  547. return 'NotIn(%s)' % (self.container,)
  548. class Contains(object):
  549. """Validate that the given schema element is in the sequence being validated.
  550. >>> s = Contains(1)
  551. >>> s([3, 2, 1])
  552. [3, 2, 1]
  553. >>> with raises(ContainsInvalid, 'value is not allowed'):
  554. ... s([3, 2])
  555. """
  556. def __init__(self, item, msg=None):
  557. self.item = item
  558. self.msg = msg
  559. def __call__(self, v):
  560. try:
  561. check = self.item not in v
  562. except TypeError:
  563. check = True
  564. if check:
  565. raise ContainsInvalid(self.msg or 'value is not allowed')
  566. return v
  567. def __repr__(self):
  568. return 'Contains(%s)' % (self.item,)
  569. class ExactSequence(object):
  570. """Matches each element in a sequence against the corresponding element in
  571. the validators.
  572. :param msg: Message to deliver to user if validation fails.
  573. :param kwargs: All other keyword arguments are passed to the sub-Schema
  574. constructors.
  575. >>> from voluptuous import Schema, ExactSequence
  576. >>> validate = Schema(ExactSequence([str, int, list, list]))
  577. >>> validate(['hourly_report', 10, [], []])
  578. ['hourly_report', 10, [], []]
  579. >>> validate(('hourly_report', 10, [], []))
  580. ('hourly_report', 10, [], [])
  581. """
  582. def __init__(self, validators, **kwargs):
  583. self.validators = validators
  584. self.msg = kwargs.pop('msg', None)
  585. self._schemas = [Schema(val, **kwargs) for val in validators]
  586. def __call__(self, v):
  587. if not isinstance(v, (list, tuple)) or len(v) != len(self._schemas):
  588. raise ExactSequenceInvalid(self.msg)
  589. try:
  590. v = type(v)(schema(x) for x, schema in zip(v, self._schemas))
  591. except Invalid as e:
  592. raise e if self.msg is None else ExactSequenceInvalid(self.msg)
  593. return v
  594. def __repr__(self):
  595. return 'ExactSequence([%s])' % (", ".join(repr(v)
  596. for v in self.validators))
  597. class Unique(object):
  598. """Ensure an iterable does not contain duplicate items.
  599. Only iterables convertable to a set are supported (native types and
  600. objects with correct __eq__).
  601. JSON does not support set, so they need to be presented as arrays.
  602. Unique allows ensuring that such array does not contain dupes.
  603. >>> s = Schema(Unique())
  604. >>> s([])
  605. []
  606. >>> s([1, 2])
  607. [1, 2]
  608. >>> with raises(Invalid, 'contains duplicate items: [1]'):
  609. ... s([1, 1, 2])
  610. >>> with raises(Invalid, "contains duplicate items: ['one']"):
  611. ... s(['one', 'two', 'one'])
  612. >>> with raises(Invalid, regex="^contains unhashable elements: "):
  613. ... s([set([1, 2]), set([3, 4])])
  614. >>> s('abc')
  615. 'abc'
  616. >>> with raises(Invalid, regex="^contains duplicate items: "):
  617. ... s('aabbc')
  618. """
  619. def __init__(self, msg=None):
  620. self.msg = msg
  621. def __call__(self, v):
  622. try:
  623. set_v = set(v)
  624. except TypeError as e:
  625. raise TypeInvalid(
  626. self.msg or 'contains unhashable elements: {0}'.format(e))
  627. if len(set_v) != len(v):
  628. seen = set()
  629. dupes = list(set(x for x in v if x in seen or seen.add(x)))
  630. raise Invalid(
  631. self.msg or 'contains duplicate items: {0}'.format(dupes))
  632. return v
  633. def __repr__(self):
  634. return 'Unique()'
  635. class Equal(object):
  636. """Ensure that value matches target.
  637. >>> s = Schema(Equal(1))
  638. >>> s(1)
  639. 1
  640. >>> with raises(Invalid):
  641. ... s(2)
  642. Validators are not supported, match must be exact:
  643. >>> s = Schema(Equal(str))
  644. >>> with raises(Invalid):
  645. ... s('foo')
  646. """
  647. def __init__(self, target, msg=None):
  648. self.target = target
  649. self.msg = msg
  650. def __call__(self, v):
  651. if v != self.target:
  652. raise Invalid(self.msg or 'Values are not equal: value:{} != target:{}'.format(v, self.target))
  653. return v
  654. def __repr__(self):
  655. return 'Equal({})'.format(self.target)
  656. class Unordered(object):
  657. """Ensures sequence contains values in unspecified order.
  658. >>> s = Schema(Unordered([2, 1]))
  659. >>> s([2, 1])
  660. [2, 1]
  661. >>> s([1, 2])
  662. [1, 2]
  663. >>> s = Schema(Unordered([str, int]))
  664. >>> s(['foo', 1])
  665. ['foo', 1]
  666. >>> s([1, 'foo'])
  667. [1, 'foo']
  668. """
  669. def __init__(self, validators, msg=None, **kwargs):
  670. self.validators = validators
  671. self.msg = msg
  672. self._schemas = [Schema(val, **kwargs) for val in validators]
  673. def __call__(self, v):
  674. if not isinstance(v, (list, tuple)):
  675. raise Invalid(self.msg or 'Value {} is not sequence!'.format(v))
  676. if len(v) != len(self._schemas):
  677. raise Invalid(self.msg or 'List lengths differ, value:{} != target:{}'.format(len(v), len(self._schemas)))
  678. consumed = set()
  679. missing = []
  680. for index, value in enumerate(v):
  681. found = False
  682. for i, s in enumerate(self._schemas):
  683. if i in consumed:
  684. continue
  685. try:
  686. s(value)
  687. except Invalid:
  688. pass
  689. else:
  690. found = True
  691. consumed.add(i)
  692. break
  693. if not found:
  694. missing.append((index, value))
  695. if len(missing) == 1:
  696. el = missing[0]
  697. raise Invalid(self.msg or 'Element #{} ({}) is not valid against any validator'.format(el[0], el[1]))
  698. elif missing:
  699. raise MultipleInvalid([Invalid(self.msg or 'Element #{} ({}) is not valid against any validator'.format(
  700. el[0], el[1])) for el in missing])
  701. return v
  702. def __repr__(self):
  703. return 'Unordered([{}])'.format(", ".join(repr(v) for v in self.validators))
  704. class Number(object):
  705. """
  706. Verify the number of digits that are present in the number(Precision),
  707. and the decimal places(Scale)
  708. :raises Invalid: If the value does not match the provided Precision and Scale.
  709. >>> schema = Schema(Number(precision=6, scale=2))
  710. >>> schema('1234.01')
  711. '1234.01'
  712. >>> schema = Schema(Number(precision=6, scale=2, yield_decimal=True))
  713. >>> schema('1234.01')
  714. Decimal('1234.01')
  715. """
  716. def __init__(self, precision=None, scale=None, msg=None, yield_decimal=False):
  717. self.precision = precision
  718. self.scale = scale
  719. self.msg = msg
  720. self.yield_decimal = yield_decimal
  721. def __call__(self, v):
  722. """
  723. :param v: is a number enclosed with string
  724. :return: Decimal number
  725. """
  726. precision, scale, decimal_num = self._get_precision_scale(v)
  727. if self.precision is not None and self.scale is not None and precision != self.precision\
  728. and scale != self.scale:
  729. raise Invalid(self.msg or "Precision must be equal to %s, and Scale must be equal to %s" % (self.precision,
  730. self.scale))
  731. else:
  732. if self.precision is not None and precision != self.precision:
  733. raise Invalid(self.msg or "Precision must be equal to %s" % self.precision)
  734. if self.scale is not None and scale != self.scale:
  735. raise Invalid(self.msg or "Scale must be equal to %s" % self.scale)
  736. if self.yield_decimal:
  737. return decimal_num
  738. else:
  739. return v
  740. def __repr__(self):
  741. return ('Number(precision=%s, scale=%s, msg=%s)' % (self.precision, self.scale, self.msg))
  742. def _get_precision_scale(self, number):
  743. """
  744. :param number:
  745. :return: tuple(precision, scale, decimal_number)
  746. """
  747. try:
  748. decimal_num = Decimal(number)
  749. except InvalidOperation:
  750. raise Invalid(self.msg or 'Value must be a number enclosed with string')
  751. return (len(decimal_num.as_tuple().digits), -(decimal_num.as_tuple().exponent), decimal_num)
  752. class SomeOf(_WithSubValidators):
  753. """Value must pass at least some validations, determined by the given parameter.
  754. Optionally, number of passed validations can be capped.
  755. The output of each validator is passed as input to the next.
  756. :param min_valid: Minimum number of valid schemas.
  757. :param validators: a list of schemas or validators to match input against
  758. :param max_valid: Maximum number of valid schemas.
  759. :param msg: Message to deliver to user if validation fails.
  760. :param kwargs: All other keyword arguments are passed to the sub-Schema constructors.
  761. :raises NotEnoughValid: if the minimum number of validations isn't met
  762. :raises TooManyValid: if the more validations than the given amount is met
  763. >>> validate = Schema(SomeOf(min_valid=2, validators=[Range(1, 5), Any(float, int), 6.6]))
  764. >>> validate(6.6)
  765. 6.6
  766. >>> validate(3)
  767. 3
  768. >>> with raises(MultipleInvalid, 'value must be at most 5, not a valid value'):
  769. ... validate(6.2)
  770. """
  771. def __init__(self, validators, min_valid=None, max_valid=None, **kwargs):
  772. assert min_valid is not None or max_valid is not None, \
  773. 'when using "%s" you should specify at least one of min_valid and max_valid' % (type(self).__name__,)
  774. self.min_valid = min_valid or 0
  775. self.max_valid = max_valid or len(validators)
  776. super(SomeOf, self).__init__(*validators, **kwargs)
  777. def _exec(self, funcs, v, path=None):
  778. errors = []
  779. funcs = list(funcs)
  780. for func in funcs:
  781. try:
  782. if path is None:
  783. v = func(v)
  784. else:
  785. v = func(path, v)
  786. except Invalid as e:
  787. errors.append(e)
  788. passed_count = len(funcs) - len(errors)
  789. if self.min_valid <= passed_count <= self.max_valid:
  790. return v
  791. msg = self.msg
  792. if not msg:
  793. msg = ', '.join(map(str, errors))
  794. if passed_count > self.max_valid:
  795. raise TooManyValid(msg)
  796. raise NotEnoughValid(msg)
  797. def __repr__(self):
  798. return 'SomeOf(min_valid=%s, validators=[%s], max_valid=%s, msg=%r)' % (
  799. self.min_valid, ", ".join(repr(v) for v in self.validators), self.max_valid, self.msg)