wildduck/lib/schemas.js

116 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
const EJSON = require('mongodb-extended-json');
2020-07-20 01:51:06 +08:00
const Joi = require('joi');
feat(apidocs): Autogenerate OpenAPI docs ZMS-100 (#552) * api.js added endpoint for generating openapi docs. added new info to one route in mailboxes.js and messages.js files so that the api docs generation can be done at all * try to first generate json representation of the api docs * add initial Joi Object parsing * api.js make generation dynamic. messages.js add schemas from separate file. messages-schemas.js used for messages endpoint schemas * add additions to schemas. Add new schemas to messages.js and also add response object there. Add response object parsing functionality to api.js * add initial openapi doc yml file generation * remove manual yaml parsing with js-yaml JSON -> YAML parsing * fix replaceWithRefs and parseComponentsDecoupled functions, refactor, remove unnecessary comments and logs * add support for another endpoint * move big code from api.js to tools * fix array type representation, fix response objects, add necessary data and changes to endpoints * redo include logic into exclude login * fix api generation in tools.js to accomodate new naming of objects * fix messages.js, add structuredClone check in tools.js * fix structured clone definition * add one endpoint in messages.js to the api generation * messages.js add one more endpoint to API generation * add response to prev commit. Add new endpoint to API generation. Archive message and archive messages * finish with post endpoints in messages.js * added general request and response schemas. Also added req and res schemas for messages * add multiple GET endpoints to API generation and changed them to new design. Use general schemas made earlier * fix incorrect import of successRes * fix mailboxes.js * refactor general-schemas.js. Fix searchSchema in messages.js. Mailboxes.js fix response * tools.js rename methodObj in API generation to operationObj * tools.js api generation remove string fallbacks * messages.js finish with GET endpoints, addition to API doc generation * for openApi doc generation use JSON now instead of YAML
2023-11-10 23:55:16 +08:00
const sessSchema = Joi.string().max(255).label('Session identifier').description('Session identifier for the logs');
const sessIPSchema = Joi.string()
.ip({
version: ['ipv4', 'ipv6'],
cidr: 'forbidden'
})
feat(apidocs): Autogenerate OpenAPI docs ZMS-100 (#552) * api.js added endpoint for generating openapi docs. added new info to one route in mailboxes.js and messages.js files so that the api docs generation can be done at all * try to first generate json representation of the api docs * add initial Joi Object parsing * api.js make generation dynamic. messages.js add schemas from separate file. messages-schemas.js used for messages endpoint schemas * add additions to schemas. Add new schemas to messages.js and also add response object there. Add response object parsing functionality to api.js * add initial openapi doc yml file generation * remove manual yaml parsing with js-yaml JSON -> YAML parsing * fix replaceWithRefs and parseComponentsDecoupled functions, refactor, remove unnecessary comments and logs * add support for another endpoint * move big code from api.js to tools * fix array type representation, fix response objects, add necessary data and changes to endpoints * redo include logic into exclude login * fix api generation in tools.js to accomodate new naming of objects * fix messages.js, add structuredClone check in tools.js * fix structured clone definition * add one endpoint in messages.js to the api generation * messages.js add one more endpoint to API generation * add response to prev commit. Add new endpoint to API generation. Archive message and archive messages * finish with post endpoints in messages.js * added general request and response schemas. Also added req and res schemas for messages * add multiple GET endpoints to API generation and changed them to new design. Use general schemas made earlier * fix incorrect import of successRes * fix mailboxes.js * refactor general-schemas.js. Fix searchSchema in messages.js. Mailboxes.js fix response * tools.js rename methodObj in API generation to operationObj * tools.js api generation remove string fallbacks * messages.js finish with GET endpoints, addition to API doc generation * for openApi doc generation use JSON now instead of YAML
2023-11-10 23:55:16 +08:00
.label('Client IP')
.description('IP address for the logs ');
2020-07-20 01:51:06 +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
*/
2021-01-04 20:20:48 +08:00
const mongoCursorValidator = () => (value, helpers) => {
value = value.toString();
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');
}
2021-01-04 20:20:48 +08:00
return value; // Everything is OK
};
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);
const pageLimitSchema = Joi.number().default(20).min(1).max(250).label('Page size');
feat(apidocs): Autogenerate OpenAPI docs ZMS-100 (#552) * api.js added endpoint for generating openapi docs. added new info to one route in mailboxes.js and messages.js files so that the api docs generation can be done at all * try to first generate json representation of the api docs * add initial Joi Object parsing * api.js make generation dynamic. messages.js add schemas from separate file. messages-schemas.js used for messages endpoint schemas * add additions to schemas. Add new schemas to messages.js and also add response object there. Add response object parsing functionality to api.js * add initial openapi doc yml file generation * remove manual yaml parsing with js-yaml JSON -> YAML parsing * fix replaceWithRefs and parseComponentsDecoupled functions, refactor, remove unnecessary comments and logs * add support for another endpoint * move big code from api.js to tools * fix array type representation, fix response objects, add necessary data and changes to endpoints * redo include logic into exclude login * fix api generation in tools.js to accomodate new naming of objects * fix messages.js, add structuredClone check in tools.js * fix structured clone definition * add one endpoint in messages.js to the api generation * messages.js add one more endpoint to API generation * add response to prev commit. Add new endpoint to API generation. Archive message and archive messages * finish with post endpoints in messages.js * added general request and response schemas. Also added req and res schemas for messages * add multiple GET endpoints to API generation and changed them to new design. Use general schemas made earlier * fix incorrect import of successRes * fix mailboxes.js * refactor general-schemas.js. Fix searchSchema in messages.js. Mailboxes.js fix response * tools.js rename methodObj in API generation to operationObj * tools.js api generation remove string fallbacks * messages.js finish with GET endpoints, addition to API doc generation * for openApi doc generation use JSON now instead of YAML
2023-11-10 23:55:16 +08:00
const pageNrSchema = Joi.number().default(1).label('Page number').description('Current page number. Informational only, page numbers start from 1');
const nextPageCursorSchema = mongoCursorSchema
.label('Next page cursor')
.description('Cursor value for next page, retrieved from nextCursor response value')
.example('eyIkb2lkIjoiNWRmMWZkMmQ3NzkyNTExOGI2MDdjNjg0In0');
feat(apidocs): Autogenerate OpenAPI docs ZMS-100 (#552) * api.js added endpoint for generating openapi docs. added new info to one route in mailboxes.js and messages.js files so that the api docs generation can be done at all * try to first generate json representation of the api docs * add initial Joi Object parsing * api.js make generation dynamic. messages.js add schemas from separate file. messages-schemas.js used for messages endpoint schemas * add additions to schemas. Add new schemas to messages.js and also add response object there. Add response object parsing functionality to api.js * add initial openapi doc yml file generation * remove manual yaml parsing with js-yaml JSON -> YAML parsing * fix replaceWithRefs and parseComponentsDecoupled functions, refactor, remove unnecessary comments and logs * add support for another endpoint * move big code from api.js to tools * fix array type representation, fix response objects, add necessary data and changes to endpoints * redo include logic into exclude login * fix api generation in tools.js to accomodate new naming of objects * fix messages.js, add structuredClone check in tools.js * fix structured clone definition * add one endpoint in messages.js to the api generation * messages.js add one more endpoint to API generation * add response to prev commit. Add new endpoint to API generation. Archive message and archive messages * finish with post endpoints in messages.js * added general request and response schemas. Also added req and res schemas for messages * add multiple GET endpoints to API generation and changed them to new design. Use general schemas made earlier * fix incorrect import of successRes * fix mailboxes.js * refactor general-schemas.js. Fix searchSchema in messages.js. Mailboxes.js fix response * tools.js rename methodObj in API generation to operationObj * tools.js api generation remove string fallbacks * messages.js finish with GET endpoints, addition to API doc generation * for openApi doc generation use JSON now instead of YAML
2023-11-10 23:55:16 +08:00
const previousPageCursorSchema = mongoCursorSchema
.label('Previous page cursor')
.description('Cursor value for previous page, retrieved from previousCursor response value')
.example('TMIjjIy23ZGM2kk0lIixygWomEknQDWdmzMNIkbNeO0NNjR');
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
};