express-validate-decorator
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -37,3 +37,3 @@ "use strict"; | ||
.status(400) | ||
.send("Query Error " + queryResult.error.message); | ||
.json({ message: "Query Error " + queryResult.error.message }); | ||
} | ||
@@ -44,3 +44,5 @@ } | ||
if (bodyResult.error) | ||
return res.status(400).send("Body Error " + bodyResult.error.message); | ||
return res | ||
.status(400) | ||
.json({ message: "Body Error " + bodyResult.error.message }); | ||
} | ||
@@ -52,3 +54,3 @@ if (paramsSchema) { | ||
.status(400) | ||
.send("Params Error " + paramsResult.error.message); | ||
.json({ message: "Params Error " + paramsResult.error.message }); | ||
} | ||
@@ -55,0 +57,0 @@ return original.apply(this, [req, res, next]); |
{ | ||
"name": "express-validate-decorator", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "./dist/validate.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/validate.d.ts", |
@@ -1,3 +0,53 @@ | ||
# express-validate-decorator | ||
# 🦈 Express Validate Decorator | ||
coming soon | ||
Validate express routes with Joi. You can validate query and body. | ||
## Example | ||
Create a class for using decorators. | ||
When query or body not validated by schema this method will return 400 with Joi validation error message. | ||
```js | ||
import { validate, number, string, schema } from "express-validate-decorator"; | ||
import express from "express" | ||
const app = express(); | ||
const port = 3000; | ||
class UserService { | ||
constructor() { | ||
//validator respects your scope you can use bind safely | ||
this.getUser = this.getUser.bind(this); | ||
} | ||
//create validator schema | ||
@validate({ | ||
query: { | ||
page: number.required(), | ||
count: number.required() | ||
}, | ||
// you can add your custom schema 😱 | ||
body: schema({ | ||
a: Joi.number().min(1).max(10).integer(), | ||
b: 'some string' | ||
}); | ||
}) | ||
getUser(req, res){ | ||
//use body or request without if conditions or null checks | ||
res.send('Hello World!') | ||
} | ||
} | ||
const userService = new UserService() | ||
app.get('/',userService.getUser) | ||
app.listen(port, () => { | ||
console.log(`Example app listening at http://localhost:${port}`) | ||
}) | ||
``` | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11523
94
54