diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index 0cab0dad..2cb571a7 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -8,7 +8,7 @@ import { CloudEventV1Attributes, CloudEventV1OptionalAttributes, } from "./interfaces"; -import { validateV1, validateV03 } from "./spec"; +import { validateCloudEvent } from "./spec"; import { ValidationError, isBinary, asBase64, isValidType } from "./validation"; import CONSTANTS from "../constants"; import { isString } from "util"; @@ -174,12 +174,7 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 { */ public validate(): boolean { try { - if (this.specversion === Version.V1) { - return validateV1(this); - } else if (this.specversion === Version.V03) { - return validateV03(this); - } - throw new ValidationError("invalid payload"); + return validateCloudEvent(this); } catch (e) { if (e instanceof ValidationError) { throw e; diff --git a/src/event/spec.ts b/src/event/spec.ts index afa75681..537b6432 100644 --- a/src/event/spec.ts +++ b/src/event/spec.ts @@ -3,24 +3,26 @@ import { ValidationError, isBase64 } from "./validation"; import { CloudEventV1, CloudEventV03 } from "./interfaces"; import { schemaV03, schemaV1 } from "./schemas"; +import { Version } from "./cloudevent"; import CONSTANTS from "../constants"; const ajv = new Ajv({ extendRefs: true }); const isValidAgainstSchemaV1 = ajv.compile(schemaV1); const isValidAgainstSchemaV03 = ajv.compile(schemaV03); -export function validateV1(event: CloudEventV1): boolean { - if (!isValidAgainstSchemaV1(event)) { - throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors); - } - return true; -} - -export function validateV03(event: CloudEventV03): boolean { - if (!isValidAgainstSchemaV03(event)) { - throw new ValidationError("invalid payload", isValidAgainstSchemaV03.errors); +export function validateCloudEvent(event: CloudEventV03 | CloudEventV1): boolean { + if (event.specversion === Version.V1) { + if (!isValidAgainstSchemaV1(event)) { + throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors); + } + return true; + } else if (event.specversion === Version.V03) { + if (!isValidAgainstSchemaV03(event)) { + throw new ValidationError("invalid payload", isValidAgainstSchemaV03.errors); + } + return checkDataContentEncoding(event); } - return checkDataContentEncoding(event); + return false; } function checkDataContentEncoding(event: CloudEventV03): boolean { diff --git a/src/transport/http/binary_receiver.ts b/src/transport/http/binary_receiver.ts index 9be2def0..aa027f2b 100644 --- a/src/transport/http/binary_receiver.ts +++ b/src/transport/http/binary_receiver.ts @@ -1,6 +1,6 @@ import { CloudEvent, Version } from "../.."; import { CloudEventV1, CloudEventV03 } from "../../event/interfaces"; -import { validateV1, validateV03 } from "../../event/spec"; +import { validateCloudEvent } from "../../event/spec"; import { Headers, validate } from "./headers"; import { v03binaryParsers, v1binaryParsers } from "./versions"; import { parserByContentType, MappedParser } from "../../parsers"; @@ -88,7 +88,7 @@ export class BinaryHTTPReceiver { } const cloudevent = new CloudEvent({ ...eventObj, data: parsedPayload } as CloudEventV1 | CloudEventV03); - this.version === Version.V1 ? validateV1(cloudevent) : validateV03(cloudevent); + validateCloudEvent(cloudevent); return cloudevent; } } diff --git a/src/transport/http/structured_receiver.ts b/src/transport/http/structured_receiver.ts index 2450371e..ef9b3a34 100644 --- a/src/transport/http/structured_receiver.ts +++ b/src/transport/http/structured_receiver.ts @@ -5,7 +5,7 @@ import { parserByContentType } from "../../parsers"; import { v1structuredParsers, v03structuredParsers } from "./versions"; import { isString, isBase64, ValidationError, isStringOrObjectOrThrow } from "../../event/validation"; import { CloudEventV1, CloudEventV03 } from "../../event/interfaces"; -import { validateV1, validateV03 } from "../../event/spec"; +import { validateCloudEvent } from "../../event/spec"; import CONSTANTS from "../../constants"; /** @@ -85,7 +85,7 @@ export class StructuredHTTPReceiver { const cloudevent = new CloudEvent(eventObj as CloudEventV1 | CloudEventV03); // Validates the event - this.version === Version.V1 ? validateV1(cloudevent) : validateV03(cloudevent); + validateCloudEvent(cloudevent); return cloudevent; } }