-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
39 lines (34 loc) · 1.1 KB
/
schema.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
const BaseJoi = require('joi');
const sanitizeHTML = require('sanitize-html');
const extension = (joi) => {
return {
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);
module.exports.productSchema = Joi.object({
name: Joi.string().required().escapeHTML(),
img: Joi.string().required(),
price: Joi.number().min(0).required(),
desc: Joi.string().required().escapeHTML()
});
module.exports.reviewSchema = Joi.object({
rating: Joi.number().min(0).max(5),
comment: Joi.string().required().escapeHTML()
});