alecthomas / alecthomas/voluptuous

Improvement: Schema build to report what custom validator failed

Abierto
#277 4 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Python
Estrellas
1.9k
Forks
237
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Thank you very much for this library 😃 Mozilla uses it to validate structured dictionaries in several projects. For instance [this](https://dxr.mozilla.org/mozilla-central/rev/45692c884fdd5136a64fb2f8a61a0c8183b69331/taskcluster/taskgraph/transforms/task.py#32 ) and [that](https://github.com/mozilla/releasetasks/blob/1b46ce601bebf9d4ca4ffd06a76fdadc1366af4f/releasetasks/test/firefox/__init__.py#L29) ones.

I noticed an improvement we could make when custom validators are failing. As of now, schema builders don't tell what function failed. This cost me about half a day to understand that `task_provisionerId_test()` (in link 2) was causing the error:

``` py
def validate_with_humanized_errors(data, schema, max_sub_error_length=MAX_VALIDATION_ERROR_ITEM_LENGTH):
try:
return schema(data)
except (Invalid, MultipleInvalid) as e:
> raise Error(humanize_error(data, e, max_sub_error_length))
E Error: not a valid value for dictionary value @ data['tasks'][10]['task']. Got {'priority': 'high', 'provisionerId': 'aws-provisioner-v1', 'expires': '3017-04-10T17:55:13.477520+02:00', 'metadata': {'source': 'https://github.com/mozilla/releasetasks', 'name': 'firefox email release-drivers foo-None', 'description': 'Sends email to release-drivers telling updates are ready on foo-None\n', 'owner': 'release@mozilla.com'}, 'routes': ['index.releases.v1.foo.abcdef123456.firefox.42_0b2.build3.email', 'index.releases.v1.foo.latest.firefox.latest.email'], 'payload': {'maxRunTi...
```
(`task` being a fairly-sized dict). The bug was definitely on our side. Nonetheless, I think a simpler error would have been:
```py
Error: not a valid value for dictionary value @ data['tasks'][10]['task']. task_provisionerId_test() is not True. Got: ...
```

Long story short, I made a [reduced test case](https://github.com/alecthomas/voluptuous/compare/master...JohanLorenzo:report_failing_validator?expand=1) which fits in a few lines.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.