arvo-event-handler
Version:
A complete set of orthogonal event handler and orchestration primitives for Arvo based applications, featuring declarative state machines (XState), imperative resumables for agentic workflows, contract-based routing, OpenTelemetry observability, and in-me
108 lines (107 loc) • 4.88 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateInputEvent = validateInputEvent;
var arvo_core_1 = require("arvo-core");
/**
* Validates an event against provided contracts.
*
* Performs comprehensive validation including:
* - Dataschema parsing and resolution
* - Contract resolution (self vs service)
* - URI and version compatibility checks
* - Schema-based data validation
*/
function validateInputEvent(_a) {
var _b;
var event = _a.event, selfContract = _a.selfContract, serviceContracts = _a.serviceContracts, span = _a.span;
var resolvedContract = null;
var contractType;
var parsedEventDataSchema = arvo_core_1.EventDataschemaUtil.parse(event);
if (!parsedEventDataSchema) {
var errorMessage = "Event dataschema resolution failed: Unable to parse dataschema='".concat(event.dataschema, "' for event(id='").concat(event.id, "', type='").concat(event.type, "'). This makes the event opaque and does not allow contract resolution");
(0, arvo_core_1.logToSpan)({
level: 'WARNING',
message: errorMessage,
}, span);
return {
type: 'INVALID',
error: new Error(errorMessage),
};
}
var selfType = selfContract instanceof arvo_core_1.VersionedArvoContract ? selfContract.accepts.type : selfContract.type;
if (event.type === selfType) {
contractType = 'self';
resolvedContract =
selfContract instanceof arvo_core_1.VersionedArvoContract
? selfContract
: selfContract.version(parsedEventDataSchema.version);
}
else {
contractType = 'service';
// Search through service contracts for matching event type
for (var _i = 0, _c = Object.values(serviceContracts); _i < _c.length; _i++) {
var contract = _c[_i];
if (resolvedContract)
break;
// Check both emitted events and system error
for (var _d = 0, _e = __spreadArray(__spreadArray([], contract.emitList, true), [contract.systemError], false); _d < _e.length; _d++) {
var emitType = _e[_d];
if (resolvedContract)
break;
if (event.type === emitType.type) {
resolvedContract = contract;
}
}
}
}
if (!resolvedContract) {
var errorMessage = "Contract resolution failed: No matching contract found for event (id='".concat(event.id, "', type='").concat(event.type, "')");
(0, arvo_core_1.logToSpan)({
level: 'WARNING',
message: errorMessage,
}, span);
return {
type: 'CONTRACT_UNRESOLVED',
};
}
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Dataschema resolved: ".concat(event.dataschema, " matches contract(uri='").concat(resolvedContract.uri, "', version='").concat(resolvedContract.version, "')"),
}, span);
if (parsedEventDataSchema.uri !== resolvedContract.uri) {
return {
type: 'INVALID',
error: new Error("Contract URI mismatch: ".concat(contractType, " Contract(uri='").concat(resolvedContract.uri, "', type='").concat(resolvedContract.accepts.type, "') does not match Event(dataschema='").concat(event.dataschema, "', type='").concat(event.type, "')")),
};
}
if (!(0, arvo_core_1.isWildCardArvoSematicVersion)(parsedEventDataSchema.version) &&
parsedEventDataSchema.version !== resolvedContract.version) {
return {
type: 'INVALID',
error: new Error("Contract version mismatch: ".concat(contractType, " Contract(version='").concat(resolvedContract.version, "', type='").concat(resolvedContract.accepts.type, "', uri=").concat(resolvedContract.uri, ") does not match Event(dataschema='").concat(event.dataschema, "', type='").concat(event.type, "')")),
};
}
var validationSchema = contractType === 'self'
? resolvedContract.accepts.schema
: ((_b = resolvedContract.emits[event.type]) !== null && _b !== void 0 ? _b : resolvedContract.systemError.schema);
var validationResult = validationSchema.safeParse(event.data);
if (!validationResult.success) {
return {
type: 'INVALID_DATA',
error: validationResult.error,
};
}
return {
type: 'VALID',
contractType: contractType,
};
}