django-exceptions
v0.2.0
Published
Handle and parse Django REST Framework validation messages with aplomb.
Downloads
14
Maintainers
Readme
Handle and parse Django REST Framework validation messages with aplomb.
npm i django-exceptions --save
Getting Started
Django Exceptions will attempt to flatten the validation messages into a single hierarchy, as otherwise the validation messages are infinitely nested, which makes it troublesome to render to HTML. Thus you are guanrateed to have a single array of all validation messages, with a field
and messages
key – both of which themselves are arrays.
import parse from 'django-exceptions';
const data = await fromApi();
parse(data).forEach(x => {
console.log('Field:', x.field);
console.log('Messages:', x.messages);
});
In React you might do something like the following to render the messages:
import parse from 'django-exceptions';
render() {
return (
<ul className="messages">
{parse(this.props.messages).map(x => (
<li>{x.field.join(' → ')}: {x.messages.join(', ')}</li>
))}
</ul>
);
}