Skip to content

Commit 4b54b27

Browse files
authored
feat: simplify validation logic/imports (#265)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent 4014da2 commit 4b54b27

File tree

6 files changed

+87
-105
lines changed

6 files changed

+87
-105
lines changed

src/event/validation.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { ErrorObject } from "ajv";
2+
3+
/**
4+
* An Error class that will be thrown when a CloudEvent
5+
* cannot be properly validated against a specification.
6+
*/
7+
export class ValidationError extends TypeError {
8+
errors?: string[] | ErrorObject[] | null;
9+
10+
constructor(message: string, errors?: string[] | ErrorObject[] | null) {
11+
super(message);
12+
this.errors = errors ? errors : [];
13+
}
14+
}
15+
16+
export const isString = (v: unknown): boolean => typeof v === "string";
17+
export const isObject = (v: unknown): boolean => typeof v === "object";
18+
export const isDefined = (v: unknown): boolean => v && typeof v !== "undefined";
19+
20+
export const isBoolean = (v: unknown): boolean => typeof v === "boolean";
21+
export const isInteger = (v: unknown): boolean => Number.isInteger(v as number);
22+
export const isDate = (v: unknown): boolean => v instanceof Date;
23+
export const isBinary = (v: unknown): boolean => v instanceof Uint32Array;
24+
25+
export const isStringOrThrow = (v: unknown, t: Error): boolean =>
26+
isString(v)
27+
? true
28+
: (() => {
29+
throw t;
30+
})();
31+
32+
export const isDefinedOrThrow = (v: unknown, t: Error): boolean =>
33+
isDefined(v)
34+
? true
35+
: (() => {
36+
throw t;
37+
})();
38+
39+
export const isStringOrObjectOrThrow = (v: unknown, t: Error): boolean =>
40+
isString(v)
41+
? true
42+
: isObject(v)
43+
? true
44+
: (() => {
45+
throw t;
46+
})();
47+
48+
export const equalsOrThrow = (v1: unknown, v2: unknown, t: Error): boolean =>
49+
v1 === v2
50+
? true
51+
: (() => {
52+
throw t;
53+
})();
54+
55+
export const isBase64 = (value: unknown): boolean =>
56+
Buffer.from(value as string, "base64").toString("base64") === value;
57+
58+
export const isBuffer = (value: unknown): boolean => value instanceof Buffer;
59+
60+
export const asBuffer = (value: string | Buffer | Uint32Array): Buffer =>
61+
isBinary(value)
62+
? Buffer.from(value as string)
63+
: isBuffer(value)
64+
? (value as Buffer)
65+
: (() => {
66+
throw new TypeError("is not buffer or a valid binary");
67+
})();
68+
69+
export const asBase64 = (value: string | Buffer | Uint32Array): string => asBuffer(value).toString("base64");
70+
71+
export const clone = (o: Record<string, unknown>): Record<string, unknown> => JSON.parse(JSON.stringify(o));
72+
73+
export const isJsonContentType = (contentType: string): "" | RegExpMatchArray | null =>
74+
contentType && contentType.match(/(json)/i);
75+
76+
export const asData = (data: unknown, contentType: string): string => {
77+
// pattern matching alike
78+
const maybeJson =
79+
isString(data) && !isBase64(data) && isJsonContentType(contentType) ? JSON.parse(data as string) : data;
80+
81+
return isBinary(maybeJson) ? asBase64(maybeJson) : maybeJson;
82+
};
83+
84+
export const isValidType = (v: boolean | number | string | Date | Uint32Array): boolean =>
85+
isBoolean(v) || isInteger(v) || isString(v) || isDate(v) || isBinary(v);

src/event/validation/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/event/validation/is.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/event/validation/validation_error.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/integration/http_binding_1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import nock from "nock";
55

66
import { CloudEvent, Version } from "../../src";
77
import { emitBinary, emitStructured } from "../../src/transport/http";
8-
import { asBase64 } from "../../src/event/validation/is";
8+
import { asBase64 } from "../../src/event/validation";
99
import { AxiosResponse } from "axios";
1010

1111
const type = "com.github.pull.create";

test/integration/utilities_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "mocha";
22
import { expect } from "chai";
3-
import { isStringOrThrow, equalsOrThrow, isBase64, asData } from "../../src/event/validation/is";
3+
import { isStringOrThrow, equalsOrThrow, isBase64, asData } from "../../src/event/validation";
44

55
describe("Utilities", () => {
66
describe("isStringOrThrow", () => {

0 commit comments

Comments
 (0)