UNPKG

n8n

Version:

n8n Workflow Automation Tool

84 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validCredentialsPropertiesForUpdate = exports.validCredentialTypeForUpdate = exports.validCredentialsProperties = exports.validCredentialType = void 0; const di_1 = require("@n8n/di"); const jsonschema_1 = require("jsonschema"); const credential_types_1 = require("../../../../credential-types"); const credentials_helper_1 = require("../../../../credentials-helper"); const credentials_service_1 = require("./credentials.service"); function validateCredentialData(credentialType, data, res) { const properties = di_1.Container.get(credentials_helper_1.CredentialsHelper) .getCredentialsProperties(credentialType) .filter((property) => property.type !== 'hidden'); const schema = (0, credentials_service_1.toJsonSchema)(properties); const { valid, errors } = (0, jsonschema_1.validate)(data, schema, { nestedErrors: true }); if (!valid) { return res.status(400).json({ message: errors.map((error) => `request.body.data ${error.message}`).join(','), }); } } const validCredentialType = (req, res, next) => { try { di_1.Container.get(credential_types_1.CredentialTypes).getByName(req.body.type); } catch { return res.status(400).json({ message: 'req.body.type is not a known type' }); } return next(); }; exports.validCredentialType = validCredentialType; const validCredentialsProperties = (req, res, next) => { const { type, data } = req.body; const validationResult = validateCredentialData(type, data, res); if (validationResult) { return validationResult; } return next(); }; exports.validCredentialsProperties = validCredentialsProperties; const validCredentialTypeForUpdate = (req, res, next) => { const { type } = req.body; if (type !== undefined) { try { di_1.Container.get(credential_types_1.CredentialTypes).getByName(type); } catch { return res.status(400).json({ message: 'req.body.type is not a known type' }); } } return next(); }; exports.validCredentialTypeForUpdate = validCredentialTypeForUpdate; const validCredentialsPropertiesForUpdate = async (req, res, next) => { let { type } = req.body; const { data } = req.body; const { id: credentialId } = req.params; if (data !== undefined) { if (type === undefined) { const existingCredential = await (0, credentials_service_1.getCredential)(credentialId); if (!existingCredential) { return res.status(404).json({ message: 'Credential not found' }); } type = existingCredential.type; } const validationResult = validateCredentialData(type, data, res); if (validationResult) { return validationResult; } } if (type !== undefined && data === undefined) { const existingCredential = await (0, credentials_service_1.getCredential)(credentialId); if (!existingCredential) { return res.status(404).json({ message: 'Credential not found' }); } if (existingCredential.type !== type) { return res.status(400).json({ message: 'req.body.data is required when changing credential type. The existing data cannot be used with the new type.', }); } } return next(); }; exports.validCredentialsPropertiesForUpdate = validCredentialsPropertiesForUpdate; //# sourceMappingURL=credentials.middleware.js.map