An open API service indexing awesome lists of open source software.

https://github.com/wildhoney/djangoexceptions

Handle and parse Django REST Framework validation messages with aplomb.
https://github.com/wildhoney/djangoexceptions

api django error errors framework message messages rest validation validator validators

Last synced: about 1 year ago
JSON representation

Handle and parse Django REST Framework validation messages with aplomb.

Awesome Lists containing this project

README

          

Django Exceptions

> Handle and parse [Django REST Framework validation messages](https://docs.djangoproject.com/en/1.11/ref/forms/validation/) with aplomb.

`npm i django-exceptions --save`

![Travis](http://img.shields.io/travis/Wildhoney/DjangoExceptions.svg?style=flat-square)
 
![Coveralls](https://img.shields.io/coveralls/Wildhoney/DjangoExceptions.svg?style=flat-square)
 
![npm](http://img.shields.io/npm/v/django-exceptions.svg?style=flat-square)
 
![License MIT](http://img.shields.io/badge/license-mit-lightgrey.svg?style=flat-square)

# 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.

```javascript
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](https://github.com/facebook/react) you *might* do something like the following to render the messages:

```javascript
import parse from 'django-exceptions';

render() {

return (

    {parse(this.props.messages).map(x => (

  • {x.field.join(' → ')}: {x.messages.join(', ')}

  • ))}


);

}
```