Patches applied: - enforceRelayTTL — auto-deletes expired relay nodes (>7d) - updateNode — tenant enforcement (block updates on NULL-namespace nodes) - buildCanonicalMetadata — reject nested objects, sanitize values - createNode — source-based and title-based deduplication - getContext — NPE-safe count queries, stale node detection - repair_log integration on updateNode - bridge-validation.js — auto-fill defaults for tenant/namespace Origin: github.com/bradwmorris/ra-h_os@70e95e7
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
// RA-H OS Bridge Schema Validation Middleware
|
|
function validateNodePayload(payload) {
|
|
const errors = [];
|
|
const required = ['tenant', 'namespace', 'type', 'visibility', 'state', 'captured_by'];
|
|
const defaults = {
|
|
tenant: 'syslogsolution',
|
|
namespace: 'syslogsolution',
|
|
type: 'note',
|
|
visibility: 'shared',
|
|
state: 'not_processed',
|
|
captured_by: 'agent'
|
|
};
|
|
|
|
for (const field of required) {
|
|
payload.metadata = payload.metadata || {};
|
|
if (!payload.metadata[field]) {
|
|
if (defaults[field]) payload.metadata[field] = defaults[field];
|
|
else errors.push('Missing required field: ' + field);
|
|
}
|
|
}
|
|
|
|
// Also ensure source_metadata exists for compatibility
|
|
if (!payload.metadata.source_metadata) {
|
|
payload.metadata.source_metadata = {};
|
|
}
|
|
|
|
return errors;
|
|
}
|
|
|
|
function validateEdgePayload(payload) {
|
|
const errors = [];
|
|
if (!payload.explanation) {
|
|
if (payload.context?.explanation) payload.explanation = payload.context.explanation;
|
|
else errors.push('Missing required field: explanation');
|
|
}
|
|
return errors;
|
|
}
|
|
|
|
function validationMiddleware(operation, payload) {
|
|
let errors = [];
|
|
if (['createNode', 'updateNode'].includes(operation)) errors = validateNodePayload(payload);
|
|
else if (operation === 'createEdge') errors = validateEdgePayload(payload);
|
|
|
|
if (errors.length > 0) {
|
|
console.error('[BRIDGE_VALIDATION] ' + operation + ' rejected:', { errors: errors, timestamp: new Date().toISOString() });
|
|
return { success: false, errors: errors };
|
|
}
|
|
return { success: true, payload: payload };
|
|
}
|
|
module.exports = { validationMiddleware };
|