apeman-app-rest
Version:
apeman app to handle restful endpoint.
39 lines (34 loc) • 978 B
JavaScript
/**
* @function validateInclude
*/
const apemanerror = require('apemanerror')
const apemanmodel = require('apemanmodel')
const ApErrorCodes = apemanerror.ApErrorCodes
/** @lends validateInclude */
function validateInclude (model, include) {
if (!include) {
return null
}
const { associations } = model
let invalidInclude = include.filter((include) => !associations[ include ])
if (invalidInclude.length > 0) {
let type = apemanmodel.toType(model)
return validateInclude.includeError(type, invalidInclude, Object.keys(associations))
} else {
return null
}
}
Object.assign(validateInclude, {
includeError (forType, given, allowed) {
let code = ApErrorCodes.RESOURCE_INCLUDE_ERROR
return Object.assign(
apemanerror.newError(code, '$querystring/include', {
trigger: 'RestHandler',
scope: { for: forType, given, allowed }
}),
{ status: '400' }
)
}
})
module.exports = validateInclude