validators.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. """
  2. Creation and extension of validators, with implementations for existing drafts.
  3. """
  4. from __future__ import annotations
  5. from collections import deque
  6. from collections.abc import Iterable, Mapping, Sequence
  7. from functools import lru_cache
  8. from operator import methodcaller
  9. from typing import TYPE_CHECKING
  10. from urllib.parse import unquote, urldefrag, urljoin, urlsplit
  11. from urllib.request import urlopen
  12. from warnings import warn
  13. import contextlib
  14. import json
  15. import reprlib
  16. import warnings
  17. from attrs import define, field, fields
  18. from jsonschema_specifications import REGISTRY as SPECIFICATIONS
  19. from rpds import HashTrieMap
  20. import referencing.exceptions
  21. import referencing.jsonschema
  22. from jsonschema import (
  23. _format,
  24. _keywords,
  25. _legacy_keywords,
  26. _types,
  27. _typing,
  28. _utils,
  29. exceptions,
  30. )
  31. if TYPE_CHECKING:
  32. from jsonschema.protocols import Validator
  33. _UNSET = _utils.Unset()
  34. _VALIDATORS: dict[str, Validator] = {}
  35. _META_SCHEMAS = _utils.URIDict()
  36. def __getattr__(name):
  37. if name == "ErrorTree":
  38. warnings.warn(
  39. "Importing ErrorTree from jsonschema.validators is deprecated. "
  40. "Instead import it from jsonschema.exceptions.",
  41. DeprecationWarning,
  42. stacklevel=2,
  43. )
  44. from jsonschema.exceptions import ErrorTree
  45. return ErrorTree
  46. elif name == "validators":
  47. warnings.warn(
  48. "Accessing jsonschema.validators.validators is deprecated. "
  49. "Use jsonschema.validators.validator_for with a given schema.",
  50. DeprecationWarning,
  51. stacklevel=2,
  52. )
  53. return _VALIDATORS
  54. elif name == "meta_schemas":
  55. warnings.warn(
  56. "Accessing jsonschema.validators.meta_schemas is deprecated. "
  57. "Use jsonschema.validators.validator_for with a given schema.",
  58. DeprecationWarning,
  59. stacklevel=2,
  60. )
  61. return _META_SCHEMAS
  62. elif name == "RefResolver":
  63. warnings.warn(
  64. _RefResolver._DEPRECATION_MESSAGE,
  65. DeprecationWarning,
  66. stacklevel=2,
  67. )
  68. return _RefResolver
  69. raise AttributeError(f"module {__name__} has no attribute {name}")
  70. def validates(version):
  71. """
  72. Register the decorated validator for a ``version`` of the specification.
  73. Registered validators and their meta schemas will be considered when
  74. parsing :kw:`$schema` keywords' URIs.
  75. Arguments:
  76. version (str):
  77. An identifier to use as the version's name
  78. Returns:
  79. collections.abc.Callable:
  80. a class decorator to decorate the validator with the version
  81. """
  82. def _validates(cls):
  83. _VALIDATORS[version] = cls
  84. meta_schema_id = cls.ID_OF(cls.META_SCHEMA)
  85. _META_SCHEMAS[meta_schema_id] = cls
  86. return cls
  87. return _validates
  88. def _warn_for_remote_retrieve(uri: str):
  89. from urllib.request import Request, urlopen
  90. headers = {"User-Agent": "python-jsonschema (deprecated $ref resolution)"}
  91. request = Request(uri, headers=headers) # noqa: S310
  92. with urlopen(request) as response: # noqa: S310
  93. warnings.warn(
  94. "Automatically retrieving remote references can be a security "
  95. "vulnerability and is discouraged by the JSON Schema "
  96. "specifications. Relying on this behavior is deprecated "
  97. "and will shortly become an error. If you are sure you want to "
  98. "remotely retrieve your reference and that it is safe to do so, "
  99. "you can find instructions for doing so via referencing.Registry "
  100. "in the referencing documentation "
  101. "(https://referencing.readthedocs.org).",
  102. DeprecationWarning,
  103. stacklevel=9, # Ha ha ha ha magic numbers :/
  104. )
  105. return referencing.Resource.from_contents(
  106. json.load(response),
  107. default_specification=referencing.jsonschema.DRAFT202012,
  108. )
  109. _REMOTE_WARNING_REGISTRY = SPECIFICATIONS.combine(
  110. referencing.Registry(retrieve=_warn_for_remote_retrieve), # type: ignore[call-arg]
  111. )
  112. def create(
  113. meta_schema: referencing.jsonschema.ObjectSchema,
  114. validators: (
  115. Mapping[str, _typing.SchemaKeywordValidator]
  116. | Iterable[tuple[str, _typing.SchemaKeywordValidator]]
  117. ) = (),
  118. version: str | None = None,
  119. type_checker: _types.TypeChecker = _types.draft202012_type_checker,
  120. format_checker: _format.FormatChecker = _format.draft202012_format_checker,
  121. id_of: _typing.id_of = referencing.jsonschema.DRAFT202012.id_of,
  122. applicable_validators: _typing.ApplicableValidators = methodcaller(
  123. "items",
  124. ),
  125. ):
  126. """
  127. Create a new validator class.
  128. Arguments:
  129. meta_schema:
  130. the meta schema for the new validator class
  131. validators:
  132. a mapping from names to callables, where each callable will
  133. validate the schema property with the given name.
  134. Each callable should take 4 arguments:
  135. 1. a validator instance,
  136. 2. the value of the property being validated within the
  137. instance
  138. 3. the instance
  139. 4. the schema
  140. version:
  141. an identifier for the version that this validator class will
  142. validate. If provided, the returned validator class will
  143. have its ``__name__`` set to include the version, and also
  144. will have `jsonschema.validators.validates` automatically
  145. called for the given version.
  146. type_checker:
  147. a type checker, used when applying the :kw:`type` keyword.
  148. If unprovided, a `jsonschema.TypeChecker` will be created
  149. with a set of default types typical of JSON Schema drafts.
  150. format_checker:
  151. a format checker, used when applying the :kw:`format` keyword.
  152. If unprovided, a `jsonschema.FormatChecker` will be created
  153. with a set of default formats typical of JSON Schema drafts.
  154. id_of:
  155. A function that given a schema, returns its ID.
  156. applicable_validators:
  157. A function that, given a schema, returns the list of
  158. applicable schema keywords and associated values
  159. which will be used to validate the instance.
  160. This is mostly used to support pre-draft 7 versions of JSON Schema
  161. which specified behavior around ignoring keywords if they were
  162. siblings of a ``$ref`` keyword. If you're not attempting to
  163. implement similar behavior, you can typically ignore this argument
  164. and leave it at its default.
  165. Returns:
  166. a new `jsonschema.protocols.Validator` class
  167. """
  168. # preemptively don't shadow the `Validator.format_checker` local
  169. format_checker_arg = format_checker
  170. specification = referencing.jsonschema.specification_with(
  171. dialect_id=id_of(meta_schema) or "urn:unknown-dialect",
  172. default=referencing.Specification.OPAQUE,
  173. )
  174. @define
  175. class Validator:
  176. VALIDATORS = dict(validators) # noqa: RUF012
  177. META_SCHEMA = dict(meta_schema) # noqa: RUF012
  178. TYPE_CHECKER = type_checker
  179. FORMAT_CHECKER = format_checker_arg
  180. ID_OF = staticmethod(id_of)
  181. _APPLICABLE_VALIDATORS = applicable_validators
  182. _validators = field(init=False, repr=False, eq=False)
  183. schema: referencing.jsonschema.Schema = field(repr=reprlib.repr)
  184. _ref_resolver = field(default=None, repr=False, alias="resolver")
  185. format_checker: _format.FormatChecker | None = field(default=None)
  186. # TODO: include new meta-schemas added at runtime
  187. _registry: referencing.jsonschema.SchemaRegistry = field(
  188. default=_REMOTE_WARNING_REGISTRY,
  189. kw_only=True,
  190. repr=False,
  191. )
  192. _resolver = field(
  193. alias="_resolver",
  194. default=None,
  195. kw_only=True,
  196. repr=False,
  197. )
  198. def __init_subclass__(cls):
  199. warnings.warn(
  200. (
  201. "Subclassing validator classes is not intended to "
  202. "be part of their public API. A future version "
  203. "will make doing so an error, as the behavior of "
  204. "subclasses isn't guaranteed to stay the same "
  205. "between releases of jsonschema. Instead, prefer "
  206. "composition of validators, wrapping them in an object "
  207. "owned entirely by the downstream library."
  208. ),
  209. DeprecationWarning,
  210. stacklevel=2,
  211. )
  212. def evolve(self, **changes):
  213. cls = self.__class__
  214. schema = changes.setdefault("schema", self.schema)
  215. NewValidator = validator_for(schema, default=cls)
  216. for field in fields(cls): # noqa: F402
  217. if not field.init:
  218. continue
  219. attr_name = field.name
  220. init_name = field.alias
  221. if init_name not in changes:
  222. changes[init_name] = getattr(self, attr_name)
  223. return NewValidator(**changes)
  224. cls.evolve = evolve
  225. def __attrs_post_init__(self):
  226. if self._resolver is None:
  227. registry = self._registry
  228. if registry is not _REMOTE_WARNING_REGISTRY:
  229. registry = SPECIFICATIONS.combine(registry)
  230. resource = specification.create_resource(self.schema)
  231. self._resolver = registry.resolver_with_root(resource)
  232. if self.schema is True or self.schema is False:
  233. self._validators = []
  234. else:
  235. self._validators = [
  236. (self.VALIDATORS[k], k, v)
  237. for k, v in applicable_validators(self.schema)
  238. if k in self.VALIDATORS
  239. ]
  240. # REMOVEME: Legacy ref resolution state management.
  241. push_scope = getattr(self._ref_resolver, "push_scope", None)
  242. if push_scope is not None:
  243. id = id_of(self.schema)
  244. if id is not None:
  245. push_scope(id)
  246. @classmethod
  247. def check_schema(cls, schema, format_checker=_UNSET):
  248. Validator = validator_for(cls.META_SCHEMA, default=cls)
  249. if format_checker is _UNSET:
  250. format_checker = Validator.FORMAT_CHECKER
  251. validator = Validator(
  252. schema=cls.META_SCHEMA,
  253. format_checker=format_checker,
  254. )
  255. for error in validator.iter_errors(schema):
  256. raise exceptions.SchemaError.create_from(error)
  257. @property
  258. def resolver(self):
  259. warnings.warn(
  260. (
  261. f"Accessing {self.__class__.__name__}.resolver is "
  262. "deprecated as of v4.18.0, in favor of the "
  263. "https://github.com/python-jsonschema/referencing "
  264. "library, which provides more compliant referencing "
  265. "behavior as well as more flexible APIs for "
  266. "customization."
  267. ),
  268. DeprecationWarning,
  269. stacklevel=2,
  270. )
  271. if self._ref_resolver is None:
  272. self._ref_resolver = _RefResolver.from_schema(
  273. self.schema,
  274. id_of=id_of,
  275. )
  276. return self._ref_resolver
  277. def evolve(self, **changes):
  278. schema = changes.setdefault("schema", self.schema)
  279. NewValidator = validator_for(schema, default=self.__class__)
  280. for (attr_name, init_name) in evolve_fields:
  281. if init_name not in changes:
  282. changes[init_name] = getattr(self, attr_name)
  283. return NewValidator(**changes)
  284. def iter_errors(self, instance, _schema=None):
  285. if _schema is not None:
  286. warnings.warn(
  287. (
  288. "Passing a schema to Validator.iter_errors "
  289. "is deprecated and will be removed in a future "
  290. "release. Call validator.evolve(schema=new_schema)."
  291. "iter_errors(...) instead."
  292. ),
  293. DeprecationWarning,
  294. stacklevel=2,
  295. )
  296. validators = [
  297. (self.VALIDATORS[k], k, v)
  298. for k, v in applicable_validators(_schema)
  299. if k in self.VALIDATORS
  300. ]
  301. else:
  302. _schema, validators = self.schema, self._validators
  303. if _schema is True:
  304. return
  305. elif _schema is False:
  306. yield exceptions.ValidationError(
  307. f"False schema does not allow {instance!r}",
  308. validator=None,
  309. validator_value=None,
  310. instance=instance,
  311. schema=_schema,
  312. )
  313. return
  314. for validator, k, v in validators:
  315. errors = validator(self, v, instance, _schema) or ()
  316. for error in errors:
  317. # set details if not already set by the called fn
  318. error._set(
  319. validator=k,
  320. validator_value=v,
  321. instance=instance,
  322. schema=_schema,
  323. type_checker=self.TYPE_CHECKER,
  324. )
  325. if k not in {"if", "$ref"}:
  326. error.schema_path.appendleft(k)
  327. yield error
  328. def descend(
  329. self,
  330. instance,
  331. schema,
  332. path=None,
  333. schema_path=None,
  334. resolver=None,
  335. ):
  336. if schema is True:
  337. return
  338. elif schema is False:
  339. yield exceptions.ValidationError(
  340. f"False schema does not allow {instance!r}",
  341. validator=None,
  342. validator_value=None,
  343. instance=instance,
  344. schema=schema,
  345. )
  346. return
  347. if self._ref_resolver is not None:
  348. evolved = self.evolve(schema=schema)
  349. else:
  350. if resolver is None:
  351. resolver = self._resolver.in_subresource(
  352. specification.create_resource(schema),
  353. )
  354. evolved = self.evolve(schema=schema, _resolver=resolver)
  355. for k, v in applicable_validators(schema):
  356. validator = evolved.VALIDATORS.get(k)
  357. if validator is None:
  358. continue
  359. errors = validator(evolved, v, instance, schema) or ()
  360. for error in errors:
  361. # set details if not already set by the called fn
  362. error._set(
  363. validator=k,
  364. validator_value=v,
  365. instance=instance,
  366. schema=schema,
  367. type_checker=evolved.TYPE_CHECKER,
  368. )
  369. if k not in {"if", "$ref"}:
  370. error.schema_path.appendleft(k)
  371. if path is not None:
  372. error.path.appendleft(path)
  373. if schema_path is not None:
  374. error.schema_path.appendleft(schema_path)
  375. yield error
  376. def validate(self, *args, **kwargs):
  377. for error in self.iter_errors(*args, **kwargs):
  378. raise error
  379. def is_type(self, instance, type):
  380. try:
  381. return self.TYPE_CHECKER.is_type(instance, type)
  382. except exceptions.UndefinedTypeCheck:
  383. exc = exceptions.UnknownType(type, instance, self.schema)
  384. raise exc from None
  385. def _validate_reference(self, ref, instance):
  386. if self._ref_resolver is None:
  387. try:
  388. resolved = self._resolver.lookup(ref)
  389. except referencing.exceptions.Unresolvable as err:
  390. raise exceptions._WrappedReferencingError(err) from err
  391. return self.descend(
  392. instance,
  393. resolved.contents,
  394. resolver=resolved.resolver,
  395. )
  396. else:
  397. resolve = getattr(self._ref_resolver, "resolve", None)
  398. if resolve is None:
  399. with self._ref_resolver.resolving(ref) as resolved:
  400. return self.descend(instance, resolved)
  401. else:
  402. scope, resolved = resolve(ref)
  403. self._ref_resolver.push_scope(scope)
  404. try:
  405. return list(self.descend(instance, resolved))
  406. finally:
  407. self._ref_resolver.pop_scope()
  408. def is_valid(self, instance, _schema=None):
  409. if _schema is not None:
  410. warnings.warn(
  411. (
  412. "Passing a schema to Validator.is_valid is deprecated "
  413. "and will be removed in a future release. Call "
  414. "validator.evolve(schema=new_schema).is_valid(...) "
  415. "instead."
  416. ),
  417. DeprecationWarning,
  418. stacklevel=2,
  419. )
  420. self = self.evolve(schema=_schema)
  421. error = next(self.iter_errors(instance), None)
  422. return error is None
  423. evolve_fields = [
  424. (field.name, field.alias)
  425. for field in fields(Validator)
  426. if field.init
  427. ]
  428. if version is not None:
  429. safe = version.title().replace(" ", "").replace("-", "")
  430. Validator.__name__ = Validator.__qualname__ = f"{safe}Validator"
  431. Validator = validates(version)(Validator) # type: ignore[misc]
  432. return Validator
  433. def extend(
  434. validator,
  435. validators=(),
  436. version=None,
  437. type_checker=None,
  438. format_checker=None,
  439. ):
  440. """
  441. Create a new validator class by extending an existing one.
  442. Arguments:
  443. validator (jsonschema.protocols.Validator):
  444. an existing validator class
  445. validators (collections.abc.Mapping):
  446. a mapping of new validator callables to extend with, whose
  447. structure is as in `create`.
  448. .. note::
  449. Any validator callables with the same name as an
  450. existing one will (silently) replace the old validator
  451. callable entirely, effectively overriding any validation
  452. done in the "parent" validator class.
  453. If you wish to instead extend the behavior of a parent's
  454. validator callable, delegate and call it directly in
  455. the new validator function by retrieving it using
  456. ``OldValidator.VALIDATORS["validation_keyword_name"]``.
  457. version (str):
  458. a version for the new validator class
  459. type_checker (jsonschema.TypeChecker):
  460. a type checker, used when applying the :kw:`type` keyword.
  461. If unprovided, the type checker of the extended
  462. `jsonschema.protocols.Validator` will be carried along.
  463. format_checker (jsonschema.FormatChecker):
  464. a format checker, used when applying the :kw:`format` keyword.
  465. If unprovided, the format checker of the extended
  466. `jsonschema.protocols.Validator` will be carried along.
  467. Returns:
  468. a new `jsonschema.protocols.Validator` class extending the one
  469. provided
  470. .. note:: Meta Schemas
  471. The new validator class will have its parent's meta schema.
  472. If you wish to change or extend the meta schema in the new
  473. validator class, modify ``META_SCHEMA`` directly on the returned
  474. class. Note that no implicit copying is done, so a copy should
  475. likely be made before modifying it, in order to not affect the
  476. old validator.
  477. """
  478. all_validators = dict(validator.VALIDATORS)
  479. all_validators.update(validators)
  480. if type_checker is None:
  481. type_checker = validator.TYPE_CHECKER
  482. if format_checker is None:
  483. format_checker = validator.FORMAT_CHECKER
  484. return create(
  485. meta_schema=validator.META_SCHEMA,
  486. validators=all_validators,
  487. version=version,
  488. type_checker=type_checker,
  489. format_checker=format_checker,
  490. id_of=validator.ID_OF,
  491. applicable_validators=validator._APPLICABLE_VALIDATORS,
  492. )
  493. Draft3Validator = create(
  494. meta_schema=SPECIFICATIONS.contents(
  495. "http://json-schema.org/draft-03/schema#",
  496. ),
  497. validators={
  498. "$ref": _keywords.ref,
  499. "additionalItems": _legacy_keywords.additionalItems,
  500. "additionalProperties": _keywords.additionalProperties,
  501. "dependencies": _legacy_keywords.dependencies_draft3,
  502. "disallow": _legacy_keywords.disallow_draft3,
  503. "divisibleBy": _keywords.multipleOf,
  504. "enum": _keywords.enum,
  505. "extends": _legacy_keywords.extends_draft3,
  506. "format": _keywords.format,
  507. "items": _legacy_keywords.items_draft3_draft4,
  508. "maxItems": _keywords.maxItems,
  509. "maxLength": _keywords.maxLength,
  510. "maximum": _legacy_keywords.maximum_draft3_draft4,
  511. "minItems": _keywords.minItems,
  512. "minLength": _keywords.minLength,
  513. "minimum": _legacy_keywords.minimum_draft3_draft4,
  514. "pattern": _keywords.pattern,
  515. "patternProperties": _keywords.patternProperties,
  516. "properties": _legacy_keywords.properties_draft3,
  517. "type": _legacy_keywords.type_draft3,
  518. "uniqueItems": _keywords.uniqueItems,
  519. },
  520. type_checker=_types.draft3_type_checker,
  521. format_checker=_format.draft3_format_checker,
  522. version="draft3",
  523. id_of=referencing.jsonschema.DRAFT3.id_of,
  524. applicable_validators=_legacy_keywords.ignore_ref_siblings,
  525. )
  526. Draft4Validator = create(
  527. meta_schema=SPECIFICATIONS.contents(
  528. "http://json-schema.org/draft-04/schema#",
  529. ),
  530. validators={
  531. "$ref": _keywords.ref,
  532. "additionalItems": _legacy_keywords.additionalItems,
  533. "additionalProperties": _keywords.additionalProperties,
  534. "allOf": _keywords.allOf,
  535. "anyOf": _keywords.anyOf,
  536. "dependencies": _legacy_keywords.dependencies_draft4_draft6_draft7,
  537. "enum": _keywords.enum,
  538. "format": _keywords.format,
  539. "items": _legacy_keywords.items_draft3_draft4,
  540. "maxItems": _keywords.maxItems,
  541. "maxLength": _keywords.maxLength,
  542. "maxProperties": _keywords.maxProperties,
  543. "maximum": _legacy_keywords.maximum_draft3_draft4,
  544. "minItems": _keywords.minItems,
  545. "minLength": _keywords.minLength,
  546. "minProperties": _keywords.minProperties,
  547. "minimum": _legacy_keywords.minimum_draft3_draft4,
  548. "multipleOf": _keywords.multipleOf,
  549. "not": _keywords.not_,
  550. "oneOf": _keywords.oneOf,
  551. "pattern": _keywords.pattern,
  552. "patternProperties": _keywords.patternProperties,
  553. "properties": _keywords.properties,
  554. "required": _keywords.required,
  555. "type": _keywords.type,
  556. "uniqueItems": _keywords.uniqueItems,
  557. },
  558. type_checker=_types.draft4_type_checker,
  559. format_checker=_format.draft4_format_checker,
  560. version="draft4",
  561. id_of=referencing.jsonschema.DRAFT4.id_of,
  562. applicable_validators=_legacy_keywords.ignore_ref_siblings,
  563. )
  564. Draft6Validator = create(
  565. meta_schema=SPECIFICATIONS.contents(
  566. "http://json-schema.org/draft-06/schema#",
  567. ),
  568. validators={
  569. "$ref": _keywords.ref,
  570. "additionalItems": _legacy_keywords.additionalItems,
  571. "additionalProperties": _keywords.additionalProperties,
  572. "allOf": _keywords.allOf,
  573. "anyOf": _keywords.anyOf,
  574. "const": _keywords.const,
  575. "contains": _legacy_keywords.contains_draft6_draft7,
  576. "dependencies": _legacy_keywords.dependencies_draft4_draft6_draft7,
  577. "enum": _keywords.enum,
  578. "exclusiveMaximum": _keywords.exclusiveMaximum,
  579. "exclusiveMinimum": _keywords.exclusiveMinimum,
  580. "format": _keywords.format,
  581. "items": _legacy_keywords.items_draft6_draft7_draft201909,
  582. "maxItems": _keywords.maxItems,
  583. "maxLength": _keywords.maxLength,
  584. "maxProperties": _keywords.maxProperties,
  585. "maximum": _keywords.maximum,
  586. "minItems": _keywords.minItems,
  587. "minLength": _keywords.minLength,
  588. "minProperties": _keywords.minProperties,
  589. "minimum": _keywords.minimum,
  590. "multipleOf": _keywords.multipleOf,
  591. "not": _keywords.not_,
  592. "oneOf": _keywords.oneOf,
  593. "pattern": _keywords.pattern,
  594. "patternProperties": _keywords.patternProperties,
  595. "properties": _keywords.properties,
  596. "propertyNames": _keywords.propertyNames,
  597. "required": _keywords.required,
  598. "type": _keywords.type,
  599. "uniqueItems": _keywords.uniqueItems,
  600. },
  601. type_checker=_types.draft6_type_checker,
  602. format_checker=_format.draft6_format_checker,
  603. version="draft6",
  604. id_of=referencing.jsonschema.DRAFT6.id_of,
  605. applicable_validators=_legacy_keywords.ignore_ref_siblings,
  606. )
  607. Draft7Validator = create(
  608. meta_schema=SPECIFICATIONS.contents(
  609. "http://json-schema.org/draft-07/schema#",
  610. ),
  611. validators={
  612. "$ref": _keywords.ref,
  613. "additionalItems": _legacy_keywords.additionalItems,
  614. "additionalProperties": _keywords.additionalProperties,
  615. "allOf": _keywords.allOf,
  616. "anyOf": _keywords.anyOf,
  617. "const": _keywords.const,
  618. "contains": _legacy_keywords.contains_draft6_draft7,
  619. "dependencies": _legacy_keywords.dependencies_draft4_draft6_draft7,
  620. "enum": _keywords.enum,
  621. "exclusiveMaximum": _keywords.exclusiveMaximum,
  622. "exclusiveMinimum": _keywords.exclusiveMinimum,
  623. "format": _keywords.format,
  624. "if": _keywords.if_,
  625. "items": _legacy_keywords.items_draft6_draft7_draft201909,
  626. "maxItems": _keywords.maxItems,
  627. "maxLength": _keywords.maxLength,
  628. "maxProperties": _keywords.maxProperties,
  629. "maximum": _keywords.maximum,
  630. "minItems": _keywords.minItems,
  631. "minLength": _keywords.minLength,
  632. "minProperties": _keywords.minProperties,
  633. "minimum": _keywords.minimum,
  634. "multipleOf": _keywords.multipleOf,
  635. "not": _keywords.not_,
  636. "oneOf": _keywords.oneOf,
  637. "pattern": _keywords.pattern,
  638. "patternProperties": _keywords.patternProperties,
  639. "properties": _keywords.properties,
  640. "propertyNames": _keywords.propertyNames,
  641. "required": _keywords.required,
  642. "type": _keywords.type,
  643. "uniqueItems": _keywords.uniqueItems,
  644. },
  645. type_checker=_types.draft7_type_checker,
  646. format_checker=_format.draft7_format_checker,
  647. version="draft7",
  648. id_of=referencing.jsonschema.DRAFT7.id_of,
  649. applicable_validators=_legacy_keywords.ignore_ref_siblings,
  650. )
  651. Draft201909Validator = create(
  652. meta_schema=SPECIFICATIONS.contents(
  653. "https://json-schema.org/draft/2019-09/schema",
  654. ),
  655. validators={
  656. "$recursiveRef": _legacy_keywords.recursiveRef,
  657. "$ref": _keywords.ref,
  658. "additionalItems": _legacy_keywords.additionalItems,
  659. "additionalProperties": _keywords.additionalProperties,
  660. "allOf": _keywords.allOf,
  661. "anyOf": _keywords.anyOf,
  662. "const": _keywords.const,
  663. "contains": _keywords.contains,
  664. "dependentRequired": _keywords.dependentRequired,
  665. "dependentSchemas": _keywords.dependentSchemas,
  666. "enum": _keywords.enum,
  667. "exclusiveMaximum": _keywords.exclusiveMaximum,
  668. "exclusiveMinimum": _keywords.exclusiveMinimum,
  669. "format": _keywords.format,
  670. "if": _keywords.if_,
  671. "items": _legacy_keywords.items_draft6_draft7_draft201909,
  672. "maxItems": _keywords.maxItems,
  673. "maxLength": _keywords.maxLength,
  674. "maxProperties": _keywords.maxProperties,
  675. "maximum": _keywords.maximum,
  676. "minItems": _keywords.minItems,
  677. "minLength": _keywords.minLength,
  678. "minProperties": _keywords.minProperties,
  679. "minimum": _keywords.minimum,
  680. "multipleOf": _keywords.multipleOf,
  681. "not": _keywords.not_,
  682. "oneOf": _keywords.oneOf,
  683. "pattern": _keywords.pattern,
  684. "patternProperties": _keywords.patternProperties,
  685. "properties": _keywords.properties,
  686. "propertyNames": _keywords.propertyNames,
  687. "required": _keywords.required,
  688. "type": _keywords.type,
  689. "unevaluatedItems": _legacy_keywords.unevaluatedItems_draft2019,
  690. "unevaluatedProperties": (
  691. _legacy_keywords.unevaluatedProperties_draft2019
  692. ),
  693. "uniqueItems": _keywords.uniqueItems,
  694. },
  695. type_checker=_types.draft201909_type_checker,
  696. format_checker=_format.draft201909_format_checker,
  697. version="draft2019-09",
  698. )
  699. Draft202012Validator = create(
  700. meta_schema=SPECIFICATIONS.contents(
  701. "https://json-schema.org/draft/2020-12/schema",
  702. ),
  703. validators={
  704. "$dynamicRef": _keywords.dynamicRef,
  705. "$ref": _keywords.ref,
  706. "additionalProperties": _keywords.additionalProperties,
  707. "allOf": _keywords.allOf,
  708. "anyOf": _keywords.anyOf,
  709. "const": _keywords.const,
  710. "contains": _keywords.contains,
  711. "dependentRequired": _keywords.dependentRequired,
  712. "dependentSchemas": _keywords.dependentSchemas,
  713. "enum": _keywords.enum,
  714. "exclusiveMaximum": _keywords.exclusiveMaximum,
  715. "exclusiveMinimum": _keywords.exclusiveMinimum,
  716. "format": _keywords.format,
  717. "if": _keywords.if_,
  718. "items": _keywords.items,
  719. "maxItems": _keywords.maxItems,
  720. "maxLength": _keywords.maxLength,
  721. "maxProperties": _keywords.maxProperties,
  722. "maximum": _keywords.maximum,
  723. "minItems": _keywords.minItems,
  724. "minLength": _keywords.minLength,
  725. "minProperties": _keywords.minProperties,
  726. "minimum": _keywords.minimum,
  727. "multipleOf": _keywords.multipleOf,
  728. "not": _keywords.not_,
  729. "oneOf": _keywords.oneOf,
  730. "pattern": _keywords.pattern,
  731. "patternProperties": _keywords.patternProperties,
  732. "prefixItems": _keywords.prefixItems,
  733. "properties": _keywords.properties,
  734. "propertyNames": _keywords.propertyNames,
  735. "required": _keywords.required,
  736. "type": _keywords.type,
  737. "unevaluatedItems": _keywords.unevaluatedItems,
  738. "unevaluatedProperties": _keywords.unevaluatedProperties,
  739. "uniqueItems": _keywords.uniqueItems,
  740. },
  741. type_checker=_types.draft202012_type_checker,
  742. format_checker=_format.draft202012_format_checker,
  743. version="draft2020-12",
  744. )
  745. _LATEST_VERSION = Draft202012Validator
  746. class _RefResolver:
  747. """
  748. Resolve JSON References.
  749. Arguments:
  750. base_uri (str):
  751. The URI of the referring document
  752. referrer:
  753. The actual referring document
  754. store (dict):
  755. A mapping from URIs to documents to cache
  756. cache_remote (bool):
  757. Whether remote refs should be cached after first resolution
  758. handlers (dict):
  759. A mapping from URI schemes to functions that should be used
  760. to retrieve them
  761. urljoin_cache (:func:`functools.lru_cache`):
  762. A cache that will be used for caching the results of joining
  763. the resolution scope to subscopes.
  764. remote_cache (:func:`functools.lru_cache`):
  765. A cache that will be used for caching the results of
  766. resolved remote URLs.
  767. Attributes:
  768. cache_remote (bool):
  769. Whether remote refs should be cached after first resolution
  770. .. deprecated:: v4.18.0
  771. ``RefResolver`` has been deprecated in favor of `referencing`.
  772. """
  773. _DEPRECATION_MESSAGE = (
  774. "jsonschema.RefResolver is deprecated as of v4.18.0, in favor of the "
  775. "https://github.com/python-jsonschema/referencing library, which "
  776. "provides more compliant referencing behavior as well as more "
  777. "flexible APIs for customization. A future release will remove "
  778. "RefResolver. Please file a feature request (on referencing) if you "
  779. "are missing an API for the kind of customization you need."
  780. )
  781. def __init__(
  782. self,
  783. base_uri,
  784. referrer,
  785. store=HashTrieMap(),
  786. cache_remote=True,
  787. handlers=(),
  788. urljoin_cache=None,
  789. remote_cache=None,
  790. ):
  791. if urljoin_cache is None:
  792. urljoin_cache = lru_cache(1024)(urljoin)
  793. if remote_cache is None:
  794. remote_cache = lru_cache(1024)(self.resolve_from_url)
  795. self.referrer = referrer
  796. self.cache_remote = cache_remote
  797. self.handlers = dict(handlers)
  798. self._scopes_stack = [base_uri]
  799. self.store = _utils.URIDict(
  800. (uri, each.contents) for uri, each in SPECIFICATIONS.items()
  801. )
  802. self.store.update(
  803. (id, each.META_SCHEMA) for id, each in _META_SCHEMAS.items()
  804. )
  805. self.store.update(store)
  806. self.store.update(
  807. (schema["$id"], schema)
  808. for schema in store.values()
  809. if isinstance(schema, Mapping) and "$id" in schema
  810. )
  811. self.store[base_uri] = referrer
  812. self._urljoin_cache = urljoin_cache
  813. self._remote_cache = remote_cache
  814. @classmethod
  815. def from_schema( # noqa: D417
  816. cls,
  817. schema,
  818. id_of=referencing.jsonschema.DRAFT202012.id_of,
  819. *args,
  820. **kwargs,
  821. ):
  822. """
  823. Construct a resolver from a JSON schema object.
  824. Arguments:
  825. schema:
  826. the referring schema
  827. Returns:
  828. `_RefResolver`
  829. """
  830. return cls(base_uri=id_of(schema) or "", referrer=schema, *args, **kwargs) # noqa: B026, E501
  831. def push_scope(self, scope):
  832. """
  833. Enter a given sub-scope.
  834. Treats further dereferences as being performed underneath the
  835. given scope.
  836. """
  837. self._scopes_stack.append(
  838. self._urljoin_cache(self.resolution_scope, scope),
  839. )
  840. def pop_scope(self):
  841. """
  842. Exit the most recent entered scope.
  843. Treats further dereferences as being performed underneath the
  844. original scope.
  845. Don't call this method more times than `push_scope` has been
  846. called.
  847. """
  848. try:
  849. self._scopes_stack.pop()
  850. except IndexError:
  851. raise exceptions._RefResolutionError(
  852. "Failed to pop the scope from an empty stack. "
  853. "`pop_scope()` should only be called once for every "
  854. "`push_scope()`",
  855. ) from None
  856. @property
  857. def resolution_scope(self):
  858. """
  859. Retrieve the current resolution scope.
  860. """
  861. return self._scopes_stack[-1]
  862. @property
  863. def base_uri(self):
  864. """
  865. Retrieve the current base URI, not including any fragment.
  866. """
  867. uri, _ = urldefrag(self.resolution_scope)
  868. return uri
  869. @contextlib.contextmanager
  870. def in_scope(self, scope):
  871. """
  872. Temporarily enter the given scope for the duration of the context.
  873. .. deprecated:: v4.0.0
  874. """
  875. warnings.warn(
  876. "jsonschema.RefResolver.in_scope is deprecated and will be "
  877. "removed in a future release.",
  878. DeprecationWarning,
  879. stacklevel=3,
  880. )
  881. self.push_scope(scope)
  882. try:
  883. yield
  884. finally:
  885. self.pop_scope()
  886. @contextlib.contextmanager
  887. def resolving(self, ref):
  888. """
  889. Resolve the given ``ref`` and enter its resolution scope.
  890. Exits the scope on exit of this context manager.
  891. Arguments:
  892. ref (str):
  893. The reference to resolve
  894. """
  895. url, resolved = self.resolve(ref)
  896. self.push_scope(url)
  897. try:
  898. yield resolved
  899. finally:
  900. self.pop_scope()
  901. def _find_in_referrer(self, key):
  902. return self._get_subschemas_cache()[key]
  903. @lru_cache # noqa: B019
  904. def _get_subschemas_cache(self):
  905. cache = {key: [] for key in _SUBSCHEMAS_KEYWORDS}
  906. for keyword, subschema in _search_schema(
  907. self.referrer, _match_subschema_keywords,
  908. ):
  909. cache[keyword].append(subschema)
  910. return cache
  911. @lru_cache # noqa: B019
  912. def _find_in_subschemas(self, url):
  913. subschemas = self._get_subschemas_cache()["$id"]
  914. if not subschemas:
  915. return None
  916. uri, fragment = urldefrag(url)
  917. for subschema in subschemas:
  918. id = subschema["$id"]
  919. if not isinstance(id, str):
  920. continue
  921. target_uri = self._urljoin_cache(self.resolution_scope, id)
  922. if target_uri.rstrip("/") == uri.rstrip("/"):
  923. if fragment:
  924. subschema = self.resolve_fragment(subschema, fragment)
  925. self.store[url] = subschema
  926. return url, subschema
  927. return None
  928. def resolve(self, ref):
  929. """
  930. Resolve the given reference.
  931. """
  932. url = self._urljoin_cache(self.resolution_scope, ref).rstrip("/")
  933. match = self._find_in_subschemas(url)
  934. if match is not None:
  935. return match
  936. return url, self._remote_cache(url)
  937. def resolve_from_url(self, url):
  938. """
  939. Resolve the given URL.
  940. """
  941. url, fragment = urldefrag(url)
  942. if not url:
  943. url = self.base_uri
  944. try:
  945. document = self.store[url]
  946. except KeyError:
  947. try:
  948. document = self.resolve_remote(url)
  949. except Exception as exc:
  950. raise exceptions._RefResolutionError(exc) from exc
  951. return self.resolve_fragment(document, fragment)
  952. def resolve_fragment(self, document, fragment):
  953. """
  954. Resolve a ``fragment`` within the referenced ``document``.
  955. Arguments:
  956. document:
  957. The referent document
  958. fragment (str):
  959. a URI fragment to resolve within it
  960. """
  961. fragment = fragment.lstrip("/")
  962. if not fragment:
  963. return document
  964. if document is self.referrer:
  965. find = self._find_in_referrer
  966. else:
  967. def find(key):
  968. yield from _search_schema(document, _match_keyword(key))
  969. for keyword in ["$anchor", "$dynamicAnchor"]:
  970. for subschema in find(keyword):
  971. if fragment == subschema[keyword]:
  972. return subschema
  973. for keyword in ["id", "$id"]:
  974. for subschema in find(keyword):
  975. if "#" + fragment == subschema[keyword]:
  976. return subschema
  977. # Resolve via path
  978. parts = unquote(fragment).split("/") if fragment else []
  979. for part in parts:
  980. part = part.replace("~1", "/").replace("~0", "~")
  981. if isinstance(document, Sequence):
  982. try: # noqa: SIM105
  983. part = int(part)
  984. except ValueError:
  985. pass
  986. try:
  987. document = document[part]
  988. except (TypeError, LookupError) as err:
  989. raise exceptions._RefResolutionError(
  990. f"Unresolvable JSON pointer: {fragment!r}",
  991. ) from err
  992. return document
  993. def resolve_remote(self, uri):
  994. """
  995. Resolve a remote ``uri``.
  996. If called directly, does not check the store first, but after
  997. retrieving the document at the specified URI it will be saved in
  998. the store if :attr:`cache_remote` is True.
  999. .. note::
  1000. If the requests_ library is present, ``jsonschema`` will use it to
  1001. request the remote ``uri``, so that the correct encoding is
  1002. detected and used.
  1003. If it isn't, or if the scheme of the ``uri`` is not ``http`` or
  1004. ``https``, UTF-8 is assumed.
  1005. Arguments:
  1006. uri (str):
  1007. The URI to resolve
  1008. Returns:
  1009. The retrieved document
  1010. .. _requests: https://pypi.org/project/requests/
  1011. """
  1012. try:
  1013. import requests
  1014. except ImportError:
  1015. requests = None
  1016. scheme = urlsplit(uri).scheme
  1017. if scheme in self.handlers:
  1018. result = self.handlers[scheme](uri)
  1019. elif scheme in ["http", "https"] and requests:
  1020. # Requests has support for detecting the correct encoding of
  1021. # json over http
  1022. result = requests.get(uri).json()
  1023. else:
  1024. # Otherwise, pass off to urllib and assume utf-8
  1025. with urlopen(uri) as url: # noqa: S310
  1026. result = json.loads(url.read().decode("utf-8"))
  1027. if self.cache_remote:
  1028. self.store[uri] = result
  1029. return result
  1030. _SUBSCHEMAS_KEYWORDS = ("$id", "id", "$anchor", "$dynamicAnchor")
  1031. def _match_keyword(keyword):
  1032. def matcher(value):
  1033. if keyword in value:
  1034. yield value
  1035. return matcher
  1036. def _match_subschema_keywords(value):
  1037. for keyword in _SUBSCHEMAS_KEYWORDS:
  1038. if keyword in value:
  1039. yield keyword, value
  1040. def _search_schema(schema, matcher):
  1041. """Breadth-first search routine."""
  1042. values = deque([schema])
  1043. while values:
  1044. value = values.pop()
  1045. if not isinstance(value, dict):
  1046. continue
  1047. yield from matcher(value)
  1048. values.extendleft(value.values())
  1049. def validate(instance, schema, cls=None, *args, **kwargs): # noqa: D417
  1050. """
  1051. Validate an instance under the given schema.
  1052. >>> validate([2, 3, 4], {"maxItems": 2})
  1053. Traceback (most recent call last):
  1054. ...
  1055. ValidationError: [2, 3, 4] is too long
  1056. :func:`~jsonschema.validators.validate` will first verify that the
  1057. provided schema is itself valid, since not doing so can lead to less
  1058. obvious error messages and fail in less obvious or consistent ways.
  1059. If you know you have a valid schema already, especially
  1060. if you intend to validate multiple instances with
  1061. the same schema, you likely would prefer using the
  1062. `jsonschema.protocols.Validator.validate` method directly on a
  1063. specific validator (e.g. ``Draft202012Validator.validate``).
  1064. Arguments:
  1065. instance:
  1066. The instance to validate
  1067. schema:
  1068. The schema to validate with
  1069. cls (jsonschema.protocols.Validator):
  1070. The class that will be used to validate the instance.
  1071. If the ``cls`` argument is not provided, two things will happen
  1072. in accordance with the specification. First, if the schema has a
  1073. :kw:`$schema` keyword containing a known meta-schema [#]_ then the
  1074. proper validator will be used. The specification recommends that
  1075. all schemas contain :kw:`$schema` properties for this reason. If no
  1076. :kw:`$schema` property is found, the default validator class is the
  1077. latest released draft.
  1078. Any other provided positional and keyword arguments will be passed
  1079. on when instantiating the ``cls``.
  1080. Raises:
  1081. `jsonschema.exceptions.ValidationError`:
  1082. if the instance is invalid
  1083. `jsonschema.exceptions.SchemaError`:
  1084. if the schema itself is invalid
  1085. .. rubric:: Footnotes
  1086. .. [#] known by a validator registered with
  1087. `jsonschema.validators.validates`
  1088. """
  1089. if cls is None:
  1090. cls = validator_for(schema)
  1091. cls.check_schema(schema)
  1092. validator = cls(schema, *args, **kwargs)
  1093. error = exceptions.best_match(validator.iter_errors(instance))
  1094. if error is not None:
  1095. raise error
  1096. def validator_for(
  1097. schema,
  1098. default: Validator | _utils.Unset = _UNSET,
  1099. ) -> type[Validator]:
  1100. """
  1101. Retrieve the validator class appropriate for validating the given schema.
  1102. Uses the :kw:`$schema` keyword that should be present in the given
  1103. schema to look up the appropriate validator class.
  1104. Arguments:
  1105. schema (collections.abc.Mapping or bool):
  1106. the schema to look at
  1107. default:
  1108. the default to return if the appropriate validator class
  1109. cannot be determined.
  1110. If unprovided, the default is to return the latest supported
  1111. draft.
  1112. Examples:
  1113. The :kw:`$schema` JSON Schema keyword will control which validator
  1114. class is returned:
  1115. >>> schema = {
  1116. ... "$schema": "https://json-schema.org/draft/2020-12/schema",
  1117. ... "type": "integer",
  1118. ... }
  1119. >>> jsonschema.validators.validator_for(schema)
  1120. <class 'jsonschema.validators.Draft202012Validator'>
  1121. Here, a draft 7 schema instead will return the draft 7 validator:
  1122. >>> schema = {
  1123. ... "$schema": "http://json-schema.org/draft-07/schema#",
  1124. ... "type": "integer",
  1125. ... }
  1126. >>> jsonschema.validators.validator_for(schema)
  1127. <class 'jsonschema.validators.Draft7Validator'>
  1128. Schemas with no ``$schema`` keyword will fallback to the default
  1129. argument:
  1130. >>> schema = {"type": "integer"}
  1131. >>> jsonschema.validators.validator_for(
  1132. ... schema, default=Draft7Validator,
  1133. ... )
  1134. <class 'jsonschema.validators.Draft7Validator'>
  1135. or if none is provided, to the latest version supported.
  1136. Always including the keyword when authoring schemas is highly
  1137. recommended.
  1138. """
  1139. DefaultValidator = _LATEST_VERSION if default is _UNSET else default
  1140. if schema is True or schema is False or "$schema" not in schema:
  1141. return DefaultValidator
  1142. if schema["$schema"] not in _META_SCHEMAS and default is _UNSET:
  1143. warn(
  1144. (
  1145. "The metaschema specified by $schema was not found. "
  1146. "Using the latest draft to validate, but this will raise "
  1147. "an error in the future."
  1148. ),
  1149. DeprecationWarning,
  1150. stacklevel=2,
  1151. )
  1152. return _META_SCHEMAS.get(schema["$schema"], DefaultValidator)