Skip to content

Commit d68435f

Browse files
committed
Added support for validators
Updated README Added sample validator
1 parent ed209be commit d68435f

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,38 @@ async handle(error, options) {
163163
}
164164
```
165165

166+
#### Validator
167+
> Sample Validator (see examples)
168+
```` javascript
169+
const {formatters} = use('Validator');
170+
const JsonApi = use('JsonApi');
171+
class UserValidator {
172+
get rules(){
173+
return {
174+
'username': 'required|accepted|max:255',
175+
'contact_number': 'required|accepted|max:50',
176+
'email': 'required|email|unique:companies,email|max:100'
177+
}
178+
}
179+
180+
get formatter() {
181+
return formatters.JsonApi;
182+
}
183+
184+
async fails({errors}) {
185+
for (const error of errors) {
186+
const jsonError = JsonApi.JSE.UnprocessableResourceObject.invoke();
187+
jsonError.detail = error.detail;
188+
jsonError.source = error.source;
189+
JsonApi.pushError(jsonError);
190+
}
191+
return this.ctx.response
192+
.status(JsonApi.getJsonErrorStatus())
193+
.send(JsonApi.getJsonError());
194+
}
195+
}
196+
````
197+
166198
#### Serializer Library:
167199
> [Serializer functions](https://github.com/danivek/json-api-serializer/blob/master/lib/JSONAPISerializer.js)
168200
``` javascript
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const {formatters} = use('Validator');
4+
const JsonApi = use('JsonApi');
5+
6+
class CompanyValidator {
7+
get rules() {
8+
if (this.ctx.request.method() === 'PATCH') {
9+
return {
10+
'name': 'max:255',
11+
'contact_number': 'max:50',
12+
'email': 'email|unique:companies,email|max:100',
13+
'web_address': 'max:255',
14+
'address_line_1': 'max:255',
15+
'address_line_2': 'max:255',
16+
'city': 'max:100',
17+
'postal_code': 'max:50',
18+
'country': 'max:3'
19+
}
20+
} else {
21+
return {
22+
'name': 'required|accepted|max:255',
23+
'contact_number': 'required|accepted|max:50',
24+
'email': 'required|email|unique:companies,email|max:100',
25+
'web_address': 'max:255',
26+
'address_line_1': 'max:255',
27+
'address_line_2': 'max:255',
28+
'city': 'max:100',
29+
'postal_code': 'required|accepted|max:50',
30+
'country': 'required|accepted|max:3'
31+
}
32+
}
33+
}
34+
35+
get sanitizationRules() {
36+
return {
37+
'email': 'normalize_email'
38+
}
39+
}
40+
41+
get formatter() {
42+
return formatters.JsonApi;
43+
}
44+
45+
get validateAll() {
46+
return true;
47+
}
48+
49+
async fails({errors}) {
50+
for (const error of errors) {
51+
const jsonError = JsonApi.JSE.UnprocessableResourceObject.invoke();
52+
jsonError.detail = error.detail;
53+
jsonError.source = error.source;
54+
JsonApi.pushError(jsonError);
55+
}
56+
return this.ctx.response
57+
.status(JsonApi.getJsonErrorStatus())
58+
.send(JsonApi.getJsonError());
59+
}
60+
}
61+
62+
module.exports = CompanyValidator;

src/Service/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,21 @@ class JsonApi {
5555
return jsonApiError.toJSON();
5656
}
5757

58+
getJsonError() {
59+
return this.setCommon(
60+
{
61+
jsonapi: {
62+
version: '1.0'
63+
},
64+
errors: this.jsonApiErrors
65+
}
66+
);
67+
}
68+
5869
async handleError(error, {request, response}) {
5970
const jsonApiError = this.parseError(error);
6071
this.pushError(error);
61-
await response.status(jsonApiError.status).send({"errors": this.jsonApiErrors});
72+
await response.status(jsonApiError.status).send(this.getJsonError());
6273
this.jsonApiErrors = [];
6374
}
6475

@@ -83,8 +94,7 @@ class JsonApi {
8394
}
8495
return (status400 > status500) ? 400 : 500;
8596
} else {
86-
const jsonApiError = this.jsonApiErrors.pop();
87-
return jsonApiError.status;
97+
return this.jsonApiErrors[0].status;
8898
}
8999
}
90100
}

0 commit comments

Comments
 (0)