draft04.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. import decimal
  2. import re
  3. from .exceptions import JsonSchemaDefinitionException
  4. from .generator import CodeGenerator, enforce_list
  5. JSON_TYPE_TO_PYTHON_TYPE = {
  6. 'null': 'NoneType',
  7. 'boolean': 'bool',
  8. 'number': 'int, float, Decimal',
  9. 'integer': 'int',
  10. 'string': 'str',
  11. 'array': 'list, tuple',
  12. 'object': 'dict',
  13. }
  14. DOLLAR_FINDER = re.compile(r"(?<!\\)\$") # Finds any un-escaped $ (including inside []-sets)
  15. # pylint: disable=too-many-instance-attributes,too-many-public-methods
  16. class CodeGeneratorDraft04(CodeGenerator):
  17. # pylint: disable=line-too-long
  18. # I was thinking about using ipaddress module instead of regexps for example, but it's big
  19. # difference in performance. With a module I got this difference: over 100 ms with a module
  20. # vs. 9 ms with a regex! Other modules are also ineffective or not available in standard
  21. # library. Some regexps are not 100% precise but good enough, fast and without dependencies.
  22. FORMAT_REGEXS = {
  23. 'date-time': r'^\d{4}-[01]\d-[0-3]\d(t|T)[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:[+-][0-2]\d:[0-5]\d|[+-][0-2]\d[0-5]\d|z|Z)\Z',
  24. 'email': r'^(?!.*\.\..*@)[^@.][^@]*(?<!\.)@[^@]+\.[^@]+\Z',
  25. 'hostname': r'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9])\Z',
  26. 'ipv4': r'^((25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\Z',
  27. 'ipv6': r'^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,4}:){5}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)\Z',
  28. 'uri': r'^\w+:(\/?\/?)[^\s]+\Z',
  29. }
  30. def __init__(self, definition, resolver=None, formats={}, use_default=True, use_formats=True, detailed_exceptions=True):
  31. super().__init__(definition, resolver, detailed_exceptions)
  32. self._custom_formats = formats
  33. self._use_formats = use_formats
  34. self._use_default = use_default
  35. self._json_keywords_to_function.update((
  36. ('type', self.generate_type),
  37. ('enum', self.generate_enum),
  38. ('allOf', self.generate_all_of),
  39. ('anyOf', self.generate_any_of),
  40. ('oneOf', self.generate_one_of),
  41. ('not', self.generate_not),
  42. ('minLength', self.generate_min_length),
  43. ('maxLength', self.generate_max_length),
  44. ('pattern', self.generate_pattern),
  45. ('format', self.generate_format),
  46. ('minimum', self.generate_minimum),
  47. ('maximum', self.generate_maximum),
  48. ('multipleOf', self.generate_multiple_of),
  49. ('minItems', self.generate_min_items),
  50. ('maxItems', self.generate_max_items),
  51. ('uniqueItems', self.generate_unique_items),
  52. ('items', self.generate_items),
  53. ('minProperties', self.generate_min_properties),
  54. ('maxProperties', self.generate_max_properties),
  55. ('required', self.generate_required),
  56. # Check dependencies before properties generates default values.
  57. ('dependencies', self.generate_dependencies),
  58. ('properties', self.generate_properties),
  59. ('patternProperties', self.generate_pattern_properties),
  60. ('additionalProperties', self.generate_additional_properties),
  61. ))
  62. self._any_or_one_of_count = 0
  63. @property
  64. def global_state(self):
  65. res = super().global_state
  66. res['custom_formats'] = self._custom_formats
  67. return res
  68. def generate_type(self):
  69. """
  70. Validation of type. Can be one type or list of types.
  71. .. code-block:: python
  72. {'type': 'string'}
  73. {'type': ['string', 'number']}
  74. """
  75. types = enforce_list(self._definition['type'])
  76. try:
  77. python_types = ', '.join(JSON_TYPE_TO_PYTHON_TYPE[t] for t in types)
  78. except KeyError as exc:
  79. raise JsonSchemaDefinitionException('Unknown type: {}'.format(exc))
  80. extra = ''
  81. if ('number' in types or 'integer' in types) and 'boolean' not in types:
  82. extra = ' or isinstance({variable}, bool)'.format(variable=self._variable)
  83. with self.l('if not isinstance({variable}, ({})){}:', python_types, extra):
  84. self.exc('{name} must be {}', ' or '.join(types), rule='type')
  85. def generate_enum(self):
  86. """
  87. Means that only value specified in the enum is valid.
  88. .. code-block:: python
  89. {
  90. 'enum': ['a', 'b'],
  91. }
  92. """
  93. enum = self._definition['enum']
  94. if not isinstance(enum, (list, tuple)):
  95. raise JsonSchemaDefinitionException('enum must be an array')
  96. with self.l('if {variable} not in {enum}:'):
  97. self.exc('{name} must be one of {}', self.e(enum), rule='enum')
  98. def generate_all_of(self):
  99. """
  100. Means that value have to be valid by all of those definitions. It's like put it in
  101. one big definition.
  102. .. code-block:: python
  103. {
  104. 'allOf': [
  105. {'type': 'number'},
  106. {'minimum': 5},
  107. ],
  108. }
  109. Valid values for this definition are 5, 6, 7, ... but not 4 or 'abc' for example.
  110. """
  111. for definition_item in self._definition['allOf']:
  112. self.generate_func_code_block(definition_item, self._variable, self._variable_name, clear_variables=True)
  113. def generate_any_of(self):
  114. """
  115. Means that value have to be valid by any of those definitions. It can also be valid
  116. by all of them.
  117. .. code-block:: python
  118. {
  119. 'anyOf': [
  120. {'type': 'number', 'minimum': 10},
  121. {'type': 'number', 'maximum': 5},
  122. ],
  123. }
  124. Valid values for this definition are 3, 4, 5, 10, 11, ... but not 8 for example.
  125. """
  126. self._any_or_one_of_count += 1
  127. count = self._any_or_one_of_count
  128. self.l('{variable}_any_of_count{count} = 0', count=count)
  129. for definition_item in self._definition['anyOf']:
  130. # When we know it's passing (at least once), we do not need to do another expensive try-except.
  131. with self.l('if not {variable}_any_of_count{count}:', count=count, optimize=False):
  132. with self.l('try:', optimize=False):
  133. self.generate_func_code_block(definition_item, self._variable, self._variable_name, clear_variables=True)
  134. self.l('{variable}_any_of_count{count} += 1', count=count)
  135. self.l('except JsonSchemaValueException: pass')
  136. with self.l('if not {variable}_any_of_count{count}:', count=count, optimize=False):
  137. self.exc('{name} cannot be validated by any definition', rule='anyOf')
  138. def generate_one_of(self):
  139. """
  140. Means that value have to be valid by only one of those definitions. It can't be valid
  141. by two or more of them.
  142. .. code-block:: python
  143. {
  144. 'oneOf': [
  145. {'type': 'number', 'multipleOf': 3},
  146. {'type': 'number', 'multipleOf': 5},
  147. ],
  148. }
  149. Valid values for this definition are 3, 5, 6, ... but not 15 for example.
  150. """
  151. self._any_or_one_of_count += 1
  152. count = self._any_or_one_of_count
  153. self.l('{variable}_one_of_count{count} = 0', count=count)
  154. for definition_item in self._definition['oneOf']:
  155. # When we know it's failing (one of means exactly once), we do not need to do another expensive try-except.
  156. with self.l('if {variable}_one_of_count{count} < 2:', count=count, optimize=False):
  157. with self.l('try:', optimize=False):
  158. self.generate_func_code_block(definition_item, self._variable, self._variable_name, clear_variables=True)
  159. self.l('{variable}_one_of_count{count} += 1', count=count)
  160. self.l('except JsonSchemaValueException: pass')
  161. with self.l('if {variable}_one_of_count{count} != 1:', count=count):
  162. dynamic = '" (" + str({variable}_one_of_count{}) + " matches found)"'
  163. self.exc('{name} must be valid exactly by one definition', count, append_to_msg=dynamic, rule='oneOf')
  164. def generate_not(self):
  165. """
  166. Means that value have not to be valid by this definition.
  167. .. code-block:: python
  168. {'not': {'type': 'null'}}
  169. Valid values for this definition are 'hello', 42, {} ... but not None.
  170. Since draft 06 definition can be boolean. False means nothing, True
  171. means everything is invalid.
  172. """
  173. not_definition = self._definition['not']
  174. if not_definition is True:
  175. self.exc('{name} must not be there', rule='not')
  176. elif not_definition is False:
  177. return
  178. elif not not_definition:
  179. self.exc('{name} must NOT match a disallowed definition', rule='not')
  180. else:
  181. with self.l('try:', optimize=False):
  182. self.generate_func_code_block(not_definition, self._variable, self._variable_name)
  183. self.l('except JsonSchemaValueException: pass')
  184. with self.l('else:'):
  185. self.exc('{name} must NOT match a disallowed definition', rule='not')
  186. def generate_min_length(self):
  187. with self.l('if isinstance({variable}, str):'):
  188. self.create_variable_with_length()
  189. if not isinstance(self._definition['minLength'], (int, float)):
  190. raise JsonSchemaDefinitionException('minLength must be a number')
  191. with self.l('if {variable}_len < {minLength}:'):
  192. self.exc('{name} must be longer than or equal to {minLength} characters', rule='minLength')
  193. def generate_max_length(self):
  194. with self.l('if isinstance({variable}, str):'):
  195. self.create_variable_with_length()
  196. if not isinstance(self._definition['maxLength'], (int, float)):
  197. raise JsonSchemaDefinitionException('maxLength must be a number')
  198. with self.l('if {variable}_len > {maxLength}:'):
  199. self.exc('{name} must be shorter than or equal to {maxLength} characters', rule='maxLength')
  200. def generate_pattern(self):
  201. with self.l('if isinstance({variable}, str):'):
  202. pattern = self._definition['pattern']
  203. safe_pattern = pattern.replace('\\', '\\\\').replace('"', '\\"')
  204. end_of_string_fixed_pattern = DOLLAR_FINDER.sub(r'\\Z', pattern)
  205. self._compile_regexps[pattern] = re.compile(end_of_string_fixed_pattern)
  206. with self.l('if not REGEX_PATTERNS[{}].search({variable}):', repr(pattern)):
  207. self.exc('{name} must match pattern {}', safe_pattern, rule='pattern')
  208. def generate_format(self):
  209. """
  210. Means that value have to be in specified format. For example date, email or other.
  211. .. code-block:: python
  212. {'format': 'email'}
  213. Valid value for this definition is user@example.com but not @username
  214. """
  215. if not self._use_formats:
  216. return
  217. with self.l('if isinstance({variable}, str):'):
  218. format_ = self._definition['format']
  219. # Checking custom formats - user is allowed to override default formats.
  220. if format_ in self._custom_formats:
  221. custom_format = self._custom_formats[format_]
  222. if isinstance(custom_format, str):
  223. self._generate_format(format_, format_ + '_re_pattern', custom_format)
  224. else:
  225. with self.l('if not custom_formats["{}"]({variable}):', format_):
  226. self.exc('{name} must be {}', format_, rule='format')
  227. elif format_ in self.FORMAT_REGEXS:
  228. format_regex = self.FORMAT_REGEXS[format_]
  229. self._generate_format(format_, format_ + '_re_pattern', format_regex)
  230. # Format regex is used only in meta schemas.
  231. elif format_ == 'regex':
  232. self._extra_imports_lines = ['import re']
  233. with self.l('try:', optimize=False):
  234. self.l('re.compile({variable})')
  235. with self.l('except Exception:'):
  236. self.exc('{name} must be a valid regex', rule='format')
  237. else:
  238. raise JsonSchemaDefinitionException('Unknown format: {}'.format(format_))
  239. def _generate_format(self, format_name, regexp_name, regexp):
  240. if self._definition['format'] == format_name:
  241. if not regexp_name in self._compile_regexps:
  242. self._compile_regexps[regexp_name] = re.compile(regexp)
  243. with self.l('if not REGEX_PATTERNS["{}"].match({variable}):', regexp_name):
  244. self.exc('{name} must be {}', format_name, rule='format')
  245. def generate_minimum(self):
  246. with self.l('if isinstance({variable}, (int, float, Decimal)):'):
  247. if not isinstance(self._definition['minimum'], (int, float, decimal.Decimal)):
  248. raise JsonSchemaDefinitionException('minimum must be a number')
  249. if self._definition.get('exclusiveMinimum', False):
  250. with self.l('if {variable} <= {minimum}:'):
  251. self.exc('{name} must be bigger than {minimum}', rule='minimum')
  252. else:
  253. with self.l('if {variable} < {minimum}:'):
  254. self.exc('{name} must be bigger than or equal to {minimum}', rule='minimum')
  255. def generate_maximum(self):
  256. with self.l('if isinstance({variable}, (int, float, Decimal)):'):
  257. if not isinstance(self._definition['maximum'], (int, float, decimal.Decimal)):
  258. raise JsonSchemaDefinitionException('maximum must be a number')
  259. if self._definition.get('exclusiveMaximum', False):
  260. with self.l('if {variable} >= {maximum}:'):
  261. self.exc('{name} must be smaller than {maximum}', rule='maximum')
  262. else:
  263. with self.l('if {variable} > {maximum}:'):
  264. self.exc('{name} must be smaller than or equal to {maximum}', rule='maximum')
  265. def generate_multiple_of(self):
  266. with self.l('if isinstance({variable}, (int, float, Decimal)):'):
  267. if not isinstance(self._definition['multipleOf'], (int, float, decimal.Decimal)):
  268. raise JsonSchemaDefinitionException('multipleOf must be a number')
  269. # For proper multiplication check of floats we need to use decimals,
  270. # because for example 19.01 / 0.01 = 1901.0000000000002.
  271. if isinstance(self._definition['multipleOf'], float):
  272. self.l('quotient = Decimal(repr({variable})) / Decimal(repr({multipleOf}))')
  273. else:
  274. self.l('quotient = {variable} / {multipleOf}')
  275. with self.l('if int(quotient) != quotient:'):
  276. self.exc('{name} must be multiple of {multipleOf}', rule='multipleOf')
  277. # For example, 1e308 / 0.123456789
  278. with self.l('if {variable} / {multipleOf} == float("inf"):'):
  279. self.exc('inifinity reached', rule='multipleOf')
  280. def generate_min_items(self):
  281. self.create_variable_is_list()
  282. with self.l('if {variable}_is_list:'):
  283. if not isinstance(self._definition['minItems'], (int, float)):
  284. raise JsonSchemaDefinitionException('minItems must be a number')
  285. self.create_variable_with_length()
  286. with self.l('if {variable}_len < {minItems}:'):
  287. self.exc('{name} must contain at least {minItems} items', rule='minItems')
  288. def generate_max_items(self):
  289. self.create_variable_is_list()
  290. with self.l('if {variable}_is_list:'):
  291. if not isinstance(self._definition['maxItems'], (int, float)):
  292. raise JsonSchemaDefinitionException('maxItems must be a number')
  293. self.create_variable_with_length()
  294. with self.l('if {variable}_len > {maxItems}:'):
  295. self.exc('{name} must contain less than or equal to {maxItems} items', rule='maxItems')
  296. def generate_unique_items(self):
  297. """
  298. With Python 3.4 module ``timeit`` recommended this solutions:
  299. .. code-block:: python
  300. >>> timeit.timeit("len(x) > len(set(x))", "x=range(100)+range(100)", number=100000)
  301. 0.5839540958404541
  302. >>> timeit.timeit("len({}.fromkeys(x)) == len(x)", "x=range(100)+range(100)", number=100000)
  303. 0.7094449996948242
  304. >>> timeit.timeit("seen = set(); any(i in seen or seen.add(i) for i in x)", "x=range(100)+range(100)", number=100000)
  305. 2.0819358825683594
  306. >>> timeit.timeit("np.unique(x).size == len(x)", "x=range(100)+range(100); import numpy as np", number=100000)
  307. 2.1439831256866455
  308. """
  309. unique_definition = self._definition['uniqueItems']
  310. if not unique_definition:
  311. return
  312. self.create_variable_is_list()
  313. with self.l('if {variable}_is_list:'):
  314. self.l(
  315. 'def fn(var): '
  316. 'return frozenset(dict((k, fn(v)) '
  317. 'for k, v in var.items()).items()) '
  318. 'if hasattr(var, "items") else tuple(fn(v) '
  319. 'for v in var) '
  320. 'if isinstance(var, (dict, list)) else str(var) '
  321. 'if isinstance(var, bool) else var')
  322. self.create_variable_with_length()
  323. with self.l('if {variable}_len > len(set(fn({variable}_x) for {variable}_x in {variable})):'):
  324. self.exc('{name} must contain unique items', rule='uniqueItems')
  325. def generate_items(self):
  326. """
  327. Means array is valid only when all items are valid by this definition.
  328. .. code-block:: python
  329. {
  330. 'items': [
  331. {'type': 'integer'},
  332. {'type': 'string'},
  333. ],
  334. }
  335. Valid arrays are those with integers or strings, nothing else.
  336. Since draft 06 definition can be also boolean. True means nothing, False
  337. means everything is invalid.
  338. """
  339. items_definition = self._definition['items']
  340. if items_definition is True:
  341. return
  342. self.create_variable_is_list()
  343. with self.l('if {variable}_is_list:'):
  344. self.create_variable_with_length()
  345. if items_definition is False:
  346. with self.l('if {variable}:'):
  347. self.exc('{name} must not be there', rule='items')
  348. elif isinstance(items_definition, list):
  349. for idx, item_definition in enumerate(items_definition):
  350. with self.l('if {variable}_len > {}:', idx):
  351. self.l('{variable}__{0} = {variable}[{0}]', idx)
  352. self.generate_func_code_block(
  353. item_definition,
  354. '{}__{}'.format(self._variable, idx),
  355. '{}[{}]'.format(self._variable_name, idx),
  356. )
  357. if self._use_default and isinstance(item_definition, dict) and 'default' in item_definition:
  358. self.l('else: {variable}.append({})', repr(item_definition['default']))
  359. if 'additionalItems' in self._definition:
  360. if self._definition['additionalItems'] is False:
  361. with self.l('if {variable}_len > {}:', len(items_definition)):
  362. self.exc('{name} must contain only specified items', rule='items')
  363. else:
  364. with self.l('for {variable}_x, {variable}_item in enumerate({variable}[{0}:], {0}):', len(items_definition)):
  365. count = self.generate_func_code_block(
  366. self._definition['additionalItems'],
  367. '{}_item'.format(self._variable),
  368. '{}[{{{}_x}}]'.format(self._variable_name, self._variable),
  369. )
  370. if count == 0:
  371. self.l('pass')
  372. else:
  373. if items_definition:
  374. with self.l('for {variable}_x, {variable}_item in enumerate({variable}):'):
  375. count = self.generate_func_code_block(
  376. items_definition,
  377. '{}_item'.format(self._variable),
  378. '{}[{{{}_x}}]'.format(self._variable_name, self._variable),
  379. )
  380. if count == 0:
  381. self.l('pass')
  382. def generate_min_properties(self):
  383. self.create_variable_is_dict()
  384. with self.l('if {variable}_is_dict:'):
  385. if not isinstance(self._definition['minProperties'], (int, float)):
  386. raise JsonSchemaDefinitionException('minProperties must be a number')
  387. self.create_variable_with_length()
  388. with self.l('if {variable}_len < {minProperties}:'):
  389. self.exc('{name} must contain at least {minProperties} properties', rule='minProperties')
  390. def generate_max_properties(self):
  391. self.create_variable_is_dict()
  392. with self.l('if {variable}_is_dict:'):
  393. if not isinstance(self._definition['maxProperties'], (int, float)):
  394. raise JsonSchemaDefinitionException('maxProperties must be a number')
  395. self.create_variable_with_length()
  396. with self.l('if {variable}_len > {maxProperties}:'):
  397. self.exc('{name} must contain less than or equal to {maxProperties} properties', rule='maxProperties')
  398. def generate_required(self):
  399. self.create_variable_is_dict()
  400. with self.l('if {variable}_is_dict:'):
  401. if not isinstance(self._definition['required'], (list, tuple)):
  402. raise JsonSchemaDefinitionException('required must be an array')
  403. if len(self._definition['required']) != len(set(self._definition['required'])):
  404. raise JsonSchemaDefinitionException('required must contain unique elements')
  405. if not self._definition.get('additionalProperties', True):
  406. not_possible = [
  407. prop
  408. for prop in self._definition['required']
  409. if
  410. prop not in self._definition.get('properties', {})
  411. and not any(re.search(regex, prop) for regex in self._definition.get('patternProperties', {}))
  412. ]
  413. if not_possible:
  414. raise JsonSchemaDefinitionException('{}: items {} are required but not allowed'.format(self._variable, not_possible))
  415. self.l('{variable}__missing_keys = set({required}) - {variable}.keys()')
  416. with self.l('if {variable}__missing_keys:'):
  417. dynamic = 'str(sorted({variable}__missing_keys)) + " properties"'
  418. self.exc('{name} must contain ', self.e(self._definition['required']), rule='required', append_to_msg=dynamic)
  419. def generate_properties(self):
  420. """
  421. Means object with defined keys.
  422. .. code-block:: python
  423. {
  424. 'properties': {
  425. 'key': {'type': 'number'},
  426. },
  427. }
  428. Valid object is containing key called 'key' and value any number.
  429. """
  430. self.create_variable_is_dict()
  431. with self.l('if {variable}_is_dict:'):
  432. self.create_variable_keys()
  433. for key, prop_definition in self._definition['properties'].items():
  434. key_name = re.sub(r'($[^a-zA-Z]|[^a-zA-Z0-9])', '', key)
  435. if not isinstance(prop_definition, (dict, bool)):
  436. raise JsonSchemaDefinitionException('{}[{}] must be object'.format(self._variable, key_name))
  437. with self.l('if "{}" in {variable}_keys:', self.e(key)):
  438. self.l('{variable}_keys.remove("{}")', self.e(key))
  439. self.l('{variable}__{0} = {variable}["{1}"]', key_name, self.e(key))
  440. self.generate_func_code_block(
  441. prop_definition,
  442. '{}__{}'.format(self._variable, key_name),
  443. '{}.{}'.format(self._variable_name, self.e(key)),
  444. clear_variables=True,
  445. )
  446. if self._use_default and isinstance(prop_definition, dict) and 'default' in prop_definition:
  447. self.l('else: {variable}["{}"] = {}', self.e(key), repr(prop_definition['default']))
  448. def generate_pattern_properties(self):
  449. """
  450. Means object with defined keys as patterns.
  451. .. code-block:: python
  452. {
  453. 'patternProperties': {
  454. '^x': {'type': 'number'},
  455. },
  456. }
  457. Valid object is containing key starting with a 'x' and value any number.
  458. """
  459. self.create_variable_is_dict()
  460. with self.l('if {variable}_is_dict:'):
  461. self.create_variable_keys()
  462. for pattern, definition in self._definition['patternProperties'].items():
  463. self._compile_regexps[pattern] = re.compile(pattern)
  464. with self.l('for {variable}_key, {variable}_val in {variable}.items():'):
  465. for pattern, definition in self._definition['patternProperties'].items():
  466. with self.l('if REGEX_PATTERNS[{}].search({variable}_key):', repr(pattern)):
  467. with self.l('if {variable}_key in {variable}_keys:'):
  468. self.l('{variable}_keys.remove({variable}_key)')
  469. self.generate_func_code_block(
  470. definition,
  471. '{}_val'.format(self._variable),
  472. '{}.{{{}_key}}'.format(self._variable_name, self._variable),
  473. clear_variables=True,
  474. )
  475. def generate_additional_properties(self):
  476. """
  477. Means object with keys with values defined by definition.
  478. .. code-block:: python
  479. {
  480. 'properties': {
  481. 'key': {'type': 'number'},
  482. }
  483. 'additionalProperties': {'type': 'string'},
  484. }
  485. Valid object is containing key called 'key' and it's value any number and
  486. any other key with any string.
  487. """
  488. self.create_variable_is_dict()
  489. with self.l('if {variable}_is_dict:'):
  490. self.create_variable_keys()
  491. add_prop_definition = self._definition["additionalProperties"]
  492. if add_prop_definition is True or add_prop_definition == {}:
  493. return
  494. if add_prop_definition:
  495. properties_keys = list(self._definition.get("properties", {}).keys())
  496. with self.l('for {variable}_key in {variable}_keys:'):
  497. with self.l('if {variable}_key not in {}:', properties_keys):
  498. self.l('{variable}_value = {variable}.get({variable}_key)')
  499. self.generate_func_code_block(
  500. add_prop_definition,
  501. '{}_value'.format(self._variable),
  502. '{}.{{{}_key}}'.format(self._variable_name, self._variable),
  503. )
  504. else:
  505. with self.l('if {variable}_keys:'):
  506. self.exc('{name} must not contain "+str({variable}_keys)+" properties', rule='additionalProperties')
  507. def generate_dependencies(self):
  508. """
  509. Means when object has property, it needs to have also other property.
  510. .. code-block:: python
  511. {
  512. 'dependencies': {
  513. 'bar': ['foo'],
  514. },
  515. }
  516. Valid object is containing only foo, both bar and foo or none of them, but not
  517. object with only bar.
  518. Since draft 06 definition can be boolean or empty array. True and empty array
  519. means nothing, False means that key cannot be there at all.
  520. """
  521. self.create_variable_is_dict()
  522. with self.l('if {variable}_is_dict:'):
  523. is_empty = True
  524. for key, values in self._definition["dependencies"].items():
  525. if values == [] or values is True:
  526. continue
  527. is_empty = False
  528. with self.l('if "{}" in {variable}:', self.e(key)):
  529. if values is False:
  530. self.exc('{} in {name} must not be there', key, rule='dependencies')
  531. elif isinstance(values, list):
  532. for value in values:
  533. with self.l('if "{}" not in {variable}:', self.e(value)):
  534. self.exc('{name} missing dependency {} for {}', self.e(value), self.e(key), rule='dependencies')
  535. else:
  536. self.generate_func_code_block(values, self._variable, self._variable_name, clear_variables=True)
  537. if is_empty:
  538. self.l('pass')