|
2 | 2 | Handling Validation Errors |
3 | 3 | ========================== |
4 | 4 |
|
5 | | -.. currentmodule:: jsonschema |
| 5 | +.. currentmodule:: jsonschema.exceptions |
6 | 6 |
|
7 | 7 | When an invalid instance is encountered, a :exc:`ValidationError` will be |
8 | 8 | raised or returned, depending on which method or function is used. |
@@ -194,7 +194,7 @@ If you want to programmatically be able to query which properties or validators |
194 | 194 | failed when validating a given instance, you probably will want to do so using |
195 | 195 | :class:`ErrorTree` objects. |
196 | 196 |
|
197 | | -.. autoclass:: ErrorTree |
| 197 | +.. autoclass:: jsonschema.validators.ErrorTree |
198 | 198 | :members: |
199 | 199 | :special-members: |
200 | 200 | :exclude-members: __dict__,__weakref__ |
@@ -301,3 +301,87 @@ To summarize, each tree contains child trees that can be accessed by indexing |
301 | 301 | the tree to get the corresponding child tree for a given index into the |
302 | 302 | instance. Each tree and child has a :attr:`~ErrorTree.errors` attribute, a |
303 | 303 | dict, that maps the failed validator to the corresponding validation error. |
| 304 | + |
| 305 | + |
| 306 | +best_match and by_relevance |
| 307 | +--------------------------- |
| 308 | + |
| 309 | +The :func:`best_match` function is a simple but useful function for attempting |
| 310 | +to guess the most relevant error in a given bunch. |
| 311 | + |
| 312 | +.. autofunction:: best_match |
| 313 | + |
| 314 | + Try to find an error that appears to be the best match among given errors. |
| 315 | + |
| 316 | + In general, errors that are higher up in the instance (i.e. for which |
| 317 | + :attr:`ValidationError.path` is shorter) are considered better matches, |
| 318 | + since they indicate "more" is wrong with the instance. |
| 319 | + |
| 320 | +.. doctest:: |
| 321 | + |
| 322 | + >>> from jsonschema import Draft4Validator |
| 323 | + >>> from jsonschema.exceptions import best_match |
| 324 | + |
| 325 | + >>> schema = { |
| 326 | + ... "type": "array", |
| 327 | + ... "minItems": 3, |
| 328 | + ... } |
| 329 | + >>> print(best_match(Draft4Validator(schema).iter_errors(11)).message) |
| 330 | + 11 is not of type 'array' |
| 331 | + |
| 332 | + If the resulting match is either :validator:`oneOf` or :validator:`anyOf`, |
| 333 | + the *opposite* assumption is made -- i.e. the deepest error is picked, |
| 334 | + since these validators only need to match once, and any other errors may |
| 335 | + not be relevant. |
| 336 | + |
| 337 | + :argument iterable errors: the errors to select from. Do not provide a |
| 338 | + mixture of errors from different validation attempts (i.e. from |
| 339 | + different instances or schemas), since it won't produce sensical |
| 340 | + output. |
| 341 | + :argument callable key: the key to use when sorting errors. See |
| 342 | + :func:`by_relevance` for more details (the default is to sort with the |
| 343 | + defaults of that function). |
| 344 | + :returns: the best matching error, or ``None`` if the iterable was empty |
| 345 | + |
| 346 | + .. note:: |
| 347 | + |
| 348 | + This function is a heuristic. Its return value may change for a given |
| 349 | + set of inputs from version to version if better heuristics are added. |
| 350 | + |
| 351 | + |
| 352 | +.. autofunction:: by_relevance |
| 353 | + |
| 354 | + Create a key function that can be used to sort errors by relevance. |
| 355 | + |
| 356 | + If you want to sort a bunch of errors entirely, you can use this function |
| 357 | + to do so. Using the return value of this function as a key to e.g. |
| 358 | + :func:`sorted` or :func:`max` will cause more relevant errors to be |
| 359 | + considered greater than less relevant ones. |
| 360 | + |
| 361 | +.. doctest:: |
| 362 | + |
| 363 | + >>> schema = { |
| 364 | + ... "properties": { |
| 365 | + ... "name": {"type": "string"}, |
| 366 | + ... "phones": { |
| 367 | + ... "properties": { |
| 368 | + ... "home": {"type": "string"} |
| 369 | + ... }, |
| 370 | + ... }, |
| 371 | + ... }, |
| 372 | + ... } |
| 373 | + >>> instance = {"name": 123, "phones": {"home": [123]}} |
| 374 | + >>> errors = Draft4Validator(schema).iter_errors(instance) |
| 375 | + >>> [ |
| 376 | + ... e.path[-1] |
| 377 | + ... for e in sorted(errors, key=exceptions.by_relevance()) |
| 378 | + ... ] |
| 379 | + ['home', 'name'] |
| 380 | + |
| 381 | + :argument set weak: a collection of validators to consider to be "weak". If |
| 382 | + there are two errors at the same level of the instance and one is in |
| 383 | + the set of weak validators, the other error will take priority. By |
| 384 | + default, :validator:`anyOf` and :validator:`oneOf` are considered weak |
| 385 | + validators and will be superceded by other same-level validation |
| 386 | + errors. |
| 387 | + :argument set strong a collection of validators to consider to be "strong". |
0 commit comments