Skip to content

Commit

Permalink
Merge pull request #23 from Jyrno42/validationerror-haserror-bug
Browse files Browse the repository at this point in the history
Fix ValidationError.hasError
  • Loading branch information
Jyrno42 authored Aug 31, 2017
2 parents 95cccb6 + 670cc27 commit 31d891f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tg-resources",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.6",
"description": "Abstractions on-top of `superagent` (or other Ajax libaries) for communication with REST.",
"main": "./dist/index.js",
"jsnext:main": "./es/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/validationError.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class ValidationError extends ParentValidationErrorInterface {
}

hasError() {
return this._errKeys.length > 0;
return this.nonFieldErrors !== null || this._errKeys.length > 0;
}

asString(glue = '; ') {
Expand Down
6 changes: 6 additions & 0 deletions test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,10 @@ app.post('/error400', (req, res) => {
});
});

app.get('/error400_nonField', (req, res) => {
res.status(400).json({
'non_field_errors': ['Sup dog']
});
});

export default () => app.listen(port);
25 changes: 25 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,30 @@ export default {
done();
}).catch(done);
},

'statusValidationError is handled properly - nonField only': (done) => {
const res = new Resource('/error400_nonField', {
apiRoot: 'http://127.0.0.1:3000',
});

res.fetch(null).then(() => {
done(new Error('Expected request to fail'));
}, (err) => {
// the error must RequestValidationError
expect(err).to.be.an.instanceof(RequestValidationError);

// statusCode must be correct
expect(err.statusCode).to.equal(400);

// hasError must be true
expect(err.hasError()).to.be.equal(true);

// errors should be correct
expect(err.errors.toString()).to.equal('Sup dog');

// all good
done();
}).catch(done);
},
},
};

0 comments on commit 31d891f

Please sign in to comment.