django-exceptions

0.2.0 • Public • Published
Django Exceptions

Handle and parse Django REST Framework validation messages with aplomb.

npm i django-exceptions --save

Travis   Coveralls   npm   License MIT

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>
    );
 
}

Package Sidebar

Install

npm i django-exceptions

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • wildhoney