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,
|
CloudEventV1Attributes,
|
||||||
CloudEventV1OptionalAttributes,
|
CloudEventV1OptionalAttributes,
|
||||||
} from "./interfaces";
|
} from "./interfaces";
|
||||||
import { validateV1, validateV03 } from "./spec";
|
import { validateCloudEvent } from "./spec";
|
||||||
import { ValidationError, isBinary, asBase64, isValidType } from "./validation";
|
import { ValidationError, isBinary, asBase64, isValidType } from "./validation";
|
||||||
import CONSTANTS from "../constants";
|
import CONSTANTS from "../constants";
|
||||||
import { isString } from "util";
|
import { isString } from "util";
|
||||||
|
@ -174,12 +174,7 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
|
||||||
*/
|
*/
|
||||||
public validate(): boolean {
|
public validate(): boolean {
|
||||||
try {
|
try {
|
||||||
if (this.specversion === Version.V1) {
|
return validateCloudEvent(this);
|
||||||
return validateV1(this);
|
|
||||||
} else if (this.specversion === Version.V03) {
|
|
||||||
return validateV03(this);
|
|
||||||
}
|
|
||||||
throw new ValidationError("invalid payload");
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof ValidationError) {
|
if (e instanceof ValidationError) {
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -3,24 +3,26 @@ import { ValidationError, isBase64 } from "./validation";
|
||||||
|
|
||||||
import { CloudEventV1, CloudEventV03 } from "./interfaces";
|
import { CloudEventV1, CloudEventV03 } from "./interfaces";
|
||||||
import { schemaV03, schemaV1 } from "./schemas";
|
import { schemaV03, schemaV1 } from "./schemas";
|
||||||
|
import { Version } from "./cloudevent";
|
||||||
import CONSTANTS from "../constants";
|
import CONSTANTS from "../constants";
|
||||||
|
|
||||||
const ajv = new Ajv({ extendRefs: true });
|
const ajv = new Ajv({ extendRefs: true });
|
||||||
const isValidAgainstSchemaV1 = ajv.compile(schemaV1);
|
const isValidAgainstSchemaV1 = ajv.compile(schemaV1);
|
||||||
const isValidAgainstSchemaV03 = ajv.compile(schemaV03);
|
const isValidAgainstSchemaV03 = ajv.compile(schemaV03);
|
||||||
|
|
||||||
export function validateV1(event: CloudEventV1): boolean {
|
export function validateCloudEvent(event: CloudEventV03 | CloudEventV1): boolean {
|
||||||
if (!isValidAgainstSchemaV1(event)) {
|
if (event.specversion === Version.V1) {
|
||||||
throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors);
|
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 true;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
export function validateV03(event: CloudEventV03): boolean {
|
|
||||||
if (!isValidAgainstSchemaV03(event)) {
|
|
||||||
throw new ValidationError("invalid payload", isValidAgainstSchemaV03.errors);
|
|
||||||
}
|
|
||||||
return checkDataContentEncoding(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkDataContentEncoding(event: CloudEventV03): boolean {
|
function checkDataContentEncoding(event: CloudEventV03): boolean {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { CloudEvent, Version } from "../..";
|
import { CloudEvent, Version } from "../..";
|
||||||
import { CloudEventV1, CloudEventV03 } from "../../event/interfaces";
|
import { CloudEventV1, CloudEventV03 } from "../../event/interfaces";
|
||||||
import { validateV1, validateV03 } from "../../event/spec";
|
import { validateCloudEvent } from "../../event/spec";
|
||||||
import { Headers, validate } from "./headers";
|
import { Headers, validate } from "./headers";
|
||||||
import { v03binaryParsers, v1binaryParsers } from "./versions";
|
import { v03binaryParsers, v1binaryParsers } from "./versions";
|
||||||
import { parserByContentType, MappedParser } from "../../parsers";
|
import { parserByContentType, MappedParser } from "../../parsers";
|
||||||
|
@ -88,7 +88,7 @@ export class BinaryHTTPReceiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
const cloudevent = new CloudEvent({ ...eventObj, data: parsedPayload } as CloudEventV1 | CloudEventV03);
|
const cloudevent = new CloudEvent({ ...eventObj, data: parsedPayload } as CloudEventV1 | CloudEventV03);
|
||||||
this.version === Version.V1 ? validateV1(cloudevent) : validateV03(cloudevent);
|
validateCloudEvent(cloudevent);
|
||||||
return cloudevent;
|
return cloudevent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { parserByContentType } from "../../parsers";
|
||||||
import { v1structuredParsers, v03structuredParsers } from "./versions";
|
import { v1structuredParsers, v03structuredParsers } from "./versions";
|
||||||
import { isString, isBase64, ValidationError, isStringOrObjectOrThrow } from "../../event/validation";
|
import { isString, isBase64, ValidationError, isStringOrObjectOrThrow } from "../../event/validation";
|
||||||
import { CloudEventV1, CloudEventV03 } from "../../event/interfaces";
|
import { CloudEventV1, CloudEventV03 } from "../../event/interfaces";
|
||||||
import { validateV1, validateV03 } from "../../event/spec";
|
import { validateCloudEvent } from "../../event/spec";
|
||||||
import CONSTANTS from "../../constants";
|
import CONSTANTS from "../../constants";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -85,7 +85,7 @@ export class StructuredHTTPReceiver {
|
||||||
const cloudevent = new CloudEvent(eventObj as CloudEventV1 | CloudEventV03);
|
const cloudevent = new CloudEvent(eventObj as CloudEventV1 | CloudEventV03);
|
||||||
|
|
||||||
// Validates the event
|
// Validates the event
|
||||||
this.version === Version.V1 ? validateV1(cloudevent) : validateV03(cloudevent);
|
validateCloudEvent(cloudevent);
|
||||||
return cloudevent;
|
return cloudevent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue