Ghost/ghost/api-framework/lib/validators/handle.js
Daniel Lockyer 82c612bad9 Rolled out API framework JSDoc typing to more places
- this updates a bunch of places where we're just using Object to cheat
  the system
- doing this means editor autocomplete and basic type checking is better
  because we now have proper types in place
- functionality should not change, these are just comments
2024-05-08 09:28:31 +02:00

68 lines
1.9 KiB
JavaScript

const debug = require('@tryghost/debug')('validators:handle');
const errors = require('@tryghost/errors');
const {sequence} = require('@tryghost/promise');
/**
* @description Shared input validation handler.
*
* The shared validation handler runs the request through all the validation steps.
*
* 1. Shared validation
* 2. API validation
*
* @param {Object} apiConfig - Docname + method of the ctrl
* @param {Object} apiValidators - Target API validators
* @param {import('@tryghost/api-framework').Frame} frame
*/
module.exports.input = (apiConfig, apiValidators, frame) => {
debug('input begin');
const tasks = [];
const sharedValidators = require('./input');
if (!apiValidators) {
return Promise.reject(new errors.IncorrectUsageError());
}
if (!apiConfig) {
return Promise.reject(new errors.IncorrectUsageError());
}
// ##### SHARED ALL VALIDATION
tasks.push(function allShared() {
return sharedValidators.all.all(apiConfig, frame);
});
if (sharedValidators.all[apiConfig.method]) {
tasks.push(function allShared() {
return sharedValidators.all[apiConfig.method](apiConfig, frame);
});
}
// ##### 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);
});
}
}
debug('input ready');
return sequence(tasks);
};