File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -303,8 +303,8 @@ instance. Each tree and child has a :attr:`~ErrorTree.errors` attribute, a
303303dict, that maps the failed validator to the corresponding validation error.
304304
305305
306- best_match
307- ----------
306+ best_match and by_relevance
307+ ---------------------------
308308
309309The :func: `best_match ` function is a simple but useful function for attempting
310310to 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']
You can’t perform that action at this time.
0 commit comments