Skip to content

refactor: validate cloudevent version agnostic #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/event/cloudevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 13 additions & 11 deletions src/event/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/transport/http/binary_receiver.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/transport/http/structured_receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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;
}
}