-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution-index.js
45 lines (41 loc) · 1.12 KB
/
solution-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const Validator = require("jsonschema").Validator;
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const v = new Validator();
const personSchema = {
id: "/SimplePerson",
type: "object",
properties: {
lastName: {
type: "string",
minLength: 2,
maxLength: 30,
pattern: "^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$"
},
firstName: {
type: "string",
minLength: 2,
maxLength: 30,
pattern: "^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$"
},
age: { type: "integer", minimum: 18 },
email: {
type: "email",
pattern: "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
}
},
required: ["firstName", "lastName", "email"]
};
v.addSchema(personSchema, "/SimplePerson");
app.post("/signup", (req, res) => {
let result = v.validate(req.body, personSchema);
if (Array.isArray(result.errors) && result.errors.length) {
res.status(400).send({ errors: result.errors });
}
res.json(req.body);
});
app.listen(8080, () => {
console.log("Server has started on port 3000");
});