refactor: validate cloudevent version agnostic (#311)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
This commit is contained in:
parent
81623ac443
commit
8ac3eb0c69
|
@ -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;
|
||||
|
|
|
@ -3,25 +3,27 @@ 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 {
|
||||
export function validateCloudEvent(event: CloudEventV03 | CloudEventV1): boolean {
|
||||
if (event.specversion === Version.V1) {
|
||||
if (!isValidAgainstSchemaV1(event)) {
|
||||
throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function validateV03(event: CloudEventV03): boolean {
|
||||
} else if (event.specversion === Version.V03) {
|
||||
if (!isValidAgainstSchemaV03(event)) {
|
||||
throw new ValidationError("invalid payload", isValidAgainstSchemaV03.errors);
|
||||
}
|
||||
return checkDataContentEncoding(event);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkDataContentEncoding(event: CloudEventV03): boolean {
|
||||
if (event.datacontentencoding) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue