Mailspring/packages/nylas-api/serialization.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-06-19 18:02:32 +08:00
const Joi = require('joi');
function replacer(key, value) {
// force remove any disallowed keys here
return value;
}
function jsonSchema(modelName) {
const models = ['Message', 'Thread', 'File', 'Error', 'SyncbackRequest', 'Account', 'Contact']
2016-06-29 04:55:00 +08:00
if (models.includes(modelName)) {
return Joi.object();
}
2016-06-19 18:02:32 +08:00
if (modelName === 'Account') {
// Ben: Disabled temporarily because folks keep changing the keys and it's hard
// to keep in sync. Might need to consider another approach to these.
// return Joi.object().keys({
// id: Joi.number(),
// object: Joi.string(),
// email_address: Joi.string(),
// provider: Joi.string(),
// organization_unit: Joi.string(),
// connection_settings: Joi.object(),
// sync_policy: Joi.object(),
// sync_error: Joi.object().allow(null),
// first_sync_completion: Joi.number().allow(null),
// last_sync_completions: Joi.array(),
// })
2016-06-23 05:17:45 +08:00
}
if (modelName === 'Folder') {
return Joi.object().keys({
id: Joi.number(),
object: Joi.string(),
account_id: Joi.string(),
name: Joi.string().allow(null),
display_name: Joi.string(),
})
}
if (modelName === 'Label') {
2016-06-23 05:17:45 +08:00
return Joi.object().keys({
id: Joi.number(),
object: Joi.string(),
account_id: Joi.string(),
2016-06-23 05:17:45 +08:00
name: Joi.string().allow(null),
display_name: Joi.string(),
2016-06-19 18:02:32 +08:00
})
}
return null;
}
function jsonStringify(models) {
return JSON.stringify(models, replacer, 2);
}
module.exports = {
jsonSchema,
jsonStringify,
}