-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.js
More file actions
42 lines (39 loc) · 1.61 KB
/
schemas.js
File metadata and controls
42 lines (39 loc) · 1.61 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
// server-side validation by JOI schema
const BaseJoi = require('joi'); // js validation tool
const sanitizeHtml = require('sanitize-html'); // sanitize html input
const extension = (joi) => ({
type: 'string',
base: joi.string(),
messages: {
'string.escapeHTML': '{{#label}} must not include HTML!'
},
rules: {
escapeHTML: { // define a extension on joi called escapeHTML()
validate(value, helpers) {
const clean = sanitizeHtml(value, {
allowedTags: [], //nothing allowed
allowedAttributes: {}, //nothing allowed
});
if (clean !== value) return helpers.error('string.escapeHTML', { value })
return clean;
}
}
}
});
const Joi = BaseJoi.extend(extension)
module.exports.campgoundSchema = Joi.object({
campground: Joi.object({
title: Joi.string().required().escapeHTML(),
price: Joi.number().required().min(0),
//image: Joi.string().required(),
location: Joi.string().required().escapeHTML(),
description: Joi.string().required().escapeHTML(),
}).required(),
deleteImages: Joi.array() //deleteImages is the same name with edit.ejs checkbox value, and it's not required
});
module.exports.reviewSchema = Joi.object({
review: Joi.object({
body: Joi.string().required().min(5).escapeHTML(), // min & max on string: for string length
rating: Joi.number().required().min(1).max(5) //min & max on number: for number range
}).required() //the whole review object is required if add a review
});