validators.py 27 KB

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