Skip to content

Commit 4f9447b

Browse files
committed
And add by_relevance docs.
1 parent 6b8e1f4 commit 4f9447b

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

docs/errors.rst

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ instance. Each tree and child has a :attr:`~ErrorTree.errors` attribute, a
303303
dict, that maps the failed validator to the corresponding validation error.
304304

305305

306-
best_match
307-
----------
306+
best_match and by_relevance
307+
---------------------------
308308

309309
The :func:`best_match` function is a simple but useful function for attempting
310310
to guess the most relevant error in a given bunch.
@@ -349,4 +349,31 @@ to guess the most relevant error in a given bunch.
349349
set of inputs from version to version if better heuristics are added.
350350

351351

352-
.. autofunction:: best_match
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']

0 commit comments

Comments
 (0)