-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoiSchemas.js
More file actions
64 lines (49 loc) · 1.66 KB
/
joiSchemas.js
File metadata and controls
64 lines (49 loc) · 1.66 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const baseJoi = require("joi");
const sanitizeHtml = require("sanitize-html");
const extension = joi => ({
type: "string",
base: joi.string(),
messages: {
"string.escapeHTML": "{{#label}} must not include HTML!"
},
rules: {
escapeHTML: {
validate(value, helpers) {
const clean = sanitizeHtml(value, {
allowedTags: [],
allowedAttributes: {}
});
if (clean !== value) {
return helpers.error("string.escapeHTML", { value });
}
return clean;
}
}
}
});
const Joi = baseJoi.extend(extension);
const userSchema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required().escapeHTML(),
email: Joi.string().email({ minDomainSegments: 2 }).min(10).max(140).required().escapeHTML(),
password: Joi.string().pattern(new RegExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{8,}$/)).required()
});
const starportSchema = Joi.object({
starport: Joi.object({
title: Joi.string().min(5).max(60).required().escapeHTML(),
price: Joi.number().required().min(0),
location: Joi.string().min(5).max(90).required().escapeHTML(),
description: Joi.string().min(5).max(280).required().escapeHTML()
}).required(),
deleteImages: Joi.array()
});
const reviewSchema = Joi.object({
review: Joi.object({
rating: Joi.number().min(0).max(5).required(),
body: Joi.string().min(5).max(280).required().escapeHTML()
}).required()
});
module.exports = {
starportSchema,
reviewSchema,
userSchema
};