2020-06-25 16:19:20 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const EJSON = require('mongodb-extended-json');
|
2020-07-20 01:51:06 +08:00
|
|
|
const Joi = require('joi');
|
2020-06-25 16:19:20 +08:00
|
|
|
|
|
|
|
const sessSchema = Joi.string().max(255).label('Session identifier');
|
|
|
|
const sessIPSchema = Joi.string()
|
|
|
|
.ip({
|
|
|
|
version: ['ipv4', 'ipv6'],
|
|
|
|
cidr: 'forbidden'
|
|
|
|
})
|
|
|
|
.label('Client IP');
|
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
/*
|
2020-06-25 16:19:20 +08:00
|
|
|
const tagSchema = Joi.string().max();
|
|
|
|
const tagStringValidator = () => {
|
|
|
|
return value => {
|
|
|
|
let tagSeen = new Set();
|
|
|
|
let tags = ((value && value.toString()) || '')
|
|
|
|
.split(',')
|
|
|
|
.map(tag => tag.toLowerCase().trim())
|
|
|
|
.filter(tag => {
|
|
|
|
if (tag && !tagSeen.has(tag)) {
|
|
|
|
tagSeen.add(tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return tags;
|
|
|
|
};
|
|
|
|
};
|
2020-07-20 01:51:06 +08:00
|
|
|
*/
|
2020-06-25 16:19:20 +08:00
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
const mongoCursorValidator = () => (value, helpers) => {
|
|
|
|
value = value.toString();
|
2020-06-25 16:19:20 +08:00
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
if (/[^a-zA-Z0-9\-_]/.test(value)) {
|
|
|
|
return helpers.error('any.invalid');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
EJSON.parse(Buffer.from(value, 'base64'));
|
|
|
|
} catch (E) {
|
|
|
|
return helpers.error('any.invalid');
|
|
|
|
}
|
2020-06-25 16:19:20 +08:00
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
return value; // Everything is OK
|
2020-06-25 16:19:20 +08:00
|
|
|
};
|
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
const metaDataValidator = () => (value, helpers) => {
|
|
|
|
let parsed;
|
2020-09-28 18:45:41 +08:00
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
if (typeof value === 'object') {
|
|
|
|
try {
|
|
|
|
parsed = value;
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
} catch (err) {
|
|
|
|
return helpers.error('any.invalid');
|
2020-09-28 18:45:41 +08:00
|
|
|
}
|
2021-01-04 20:20:48 +08:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
parsed = JSON.parse(value);
|
|
|
|
} catch (err) {
|
|
|
|
return helpers.error('any.invalid');
|
2020-09-28 18:45:41 +08:00
|
|
|
}
|
2021-01-04 20:20:48 +08:00
|
|
|
}
|
2020-09-28 18:45:41 +08:00
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
const { error: strError, value: strValue } = Joi.string()
|
|
|
|
.trim()
|
|
|
|
.max(1024 * 1024)
|
|
|
|
.validate(value);
|
|
|
|
if (strError) {
|
|
|
|
throw strError;
|
|
|
|
}
|
2020-09-28 18:45:41 +08:00
|
|
|
|
2021-01-04 20:20:48 +08:00
|
|
|
const { error: objError } = Joi.object().validate(parsed);
|
|
|
|
if (objError) {
|
|
|
|
throw objError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strValue;
|
2020-09-28 18:45:41 +08:00
|
|
|
};
|
|
|
|
|
2020-07-22 02:52:40 +08:00
|
|
|
const mongoCursorSchema = Joi.string().trim().empty('').custom(mongoCursorValidator({}), 'Cursor validation').max(1024);
|
2020-06-25 16:19:20 +08:00
|
|
|
const pageLimitSchema = Joi.number().default(20).min(1).max(250).label('Page size');
|
|
|
|
const pageNrSchema = Joi.number().default(1).label('Page number');
|
|
|
|
const nextPageCursorSchema = mongoCursorSchema.label('Next page cursor');
|
|
|
|
const previousPageCursorSchema = mongoCursorSchema.label('Previous page cursor');
|
2020-07-20 01:51:06 +08:00
|
|
|
const booleanSchema = Joi.boolean().empty('').truthy('Y', 'true', 'yes', 'on', '1', 1).falsy('N', 'false', 'no', 'off', '0', 0);
|
2020-09-28 18:45:41 +08:00
|
|
|
const metaDataSchema = Joi.any().custom(metaDataValidator({}), 'metadata validation');
|
2020-07-20 01:51:06 +08:00
|
|
|
|
2023-08-26 03:55:02 +08:00
|
|
|
const usernameSchema = Joi.string()
|
|
|
|
.lowercase()
|
|
|
|
.regex(/^[a-z0-9-]+(?:[._=:][a-z0-9-]+)*(?:@[a-z0-9-]+(?:[._=:][a-z0-9-]+)*)?$/, 'username')
|
|
|
|
.min(1)
|
|
|
|
.max(128);
|
|
|
|
|
2020-09-28 18:45:41 +08:00
|
|
|
module.exports = {
|
|
|
|
sessSchema,
|
|
|
|
sessIPSchema,
|
|
|
|
pageNrSchema,
|
|
|
|
nextPageCursorSchema,
|
|
|
|
previousPageCursorSchema,
|
|
|
|
pageLimitSchema,
|
|
|
|
booleanSchema,
|
2023-08-26 03:55:02 +08:00
|
|
|
metaDataSchema,
|
|
|
|
usernameSchema
|
2020-09-28 18:45:41 +08:00
|
|
|
};
|