2022-08-11 17:42:21 +03:00
|
|
|
const debug = require('@tryghost/debug')('validators:handle');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-08-11 20:44:21 +03:00
|
|
|
const {sequence} = require('@tryghost/promise');
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
/**
|
2019-05-06 15:24:12 +03:00
|
|
|
* @description Shared input validation handler.
|
|
|
|
*
|
2018-10-05 01:50:45 +03:00
|
|
|
* The shared validation handler runs the request through all the validation steps.
|
|
|
|
*
|
2019-05-06 15:24:12 +03:00
|
|
|
* 1. Shared validation
|
|
|
|
* 2. API validation
|
|
|
|
*
|
|
|
|
* @param {Object} apiConfig - Docname + method of the ctrl
|
|
|
|
* @param {Object} apiValidators - Target API validators
|
|
|
|
* @param {Object} frame
|
2018-10-05 01:50:45 +03:00
|
|
|
*/
|
|
|
|
module.exports.input = (apiConfig, apiValidators, frame) => {
|
2019-10-15 17:07:38 +03:00
|
|
|
debug('input begin');
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
const tasks = [];
|
|
|
|
const sharedValidators = require('./input');
|
|
|
|
|
|
|
|
if (!apiValidators) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiConfig) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ##### SHARED ALL VALIDATION
|
|
|
|
|
|
|
|
tasks.push(function allShared() {
|
2018-10-12 00:05:49 +03:00
|
|
|
return sharedValidators.all.all(apiConfig, frame);
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
2018-10-12 00:05:49 +03:00
|
|
|
if (sharedValidators.all[apiConfig.method]) {
|
|
|
|
tasks.push(function allShared() {
|
|
|
|
return sharedValidators.all[apiConfig.method](apiConfig, frame);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
// ##### API VERSION VALIDATION
|
|
|
|
|
|
|
|
if (apiValidators.all) {
|
|
|
|
tasks.push(function allAPIVersion() {
|
|
|
|
return apiValidators.all[apiConfig.method](apiConfig, frame);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiValidators[apiConfig.docName]) {
|
|
|
|
if (apiValidators[apiConfig.docName].all) {
|
|
|
|
tasks.push(function docNameAll() {
|
|
|
|
return apiValidators[apiConfig.docName].all(apiConfig, frame);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiValidators[apiConfig.docName][apiConfig.method]) {
|
|
|
|
tasks.push(function docNameMethod() {
|
|
|
|
return apiValidators[apiConfig.docName][apiConfig.method](apiConfig, frame);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 17:07:38 +03:00
|
|
|
debug('input ready');
|
2018-10-05 01:50:45 +03:00
|
|
|
return sequence(tasks);
|
|
|
|
};
|