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