Skip to content

Simplify/TS Validation Files #265

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 2 commits into from
Jul 23, 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
85 changes: 85 additions & 0 deletions src/event/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ErrorObject } from "ajv";

/**
* An Error class that will be thrown when a CloudEvent
* cannot be properly validated against a specification.
*/
export class ValidationError extends TypeError {
errors?: string[] | ErrorObject[] | null;

constructor(message: string, errors?: string[] | ErrorObject[] | null) {
super(message);
this.errors = errors ? errors : [];
}
}

export const isString = (v: unknown): boolean => typeof v === "string";
export const isObject = (v: unknown): boolean => typeof v === "object";
export const isDefined = (v: unknown): boolean => v && typeof v !== "undefined";

export const isBoolean = (v: unknown): boolean => typeof v === "boolean";
export const isInteger = (v: unknown): boolean => Number.isInteger(v as number);
export const isDate = (v: unknown): boolean => v instanceof Date;
export const isBinary = (v: unknown): boolean => v instanceof Uint32Array;

export const isStringOrThrow = (v: unknown, t: Error): boolean =>
isString(v)
? true
: (() => {
throw t;
})();

export const isDefinedOrThrow = (v: unknown, t: Error): boolean =>
isDefined(v)
? true
: (() => {
throw t;
})();

export const isStringOrObjectOrThrow = (v: unknown, t: Error): boolean =>
isString(v)
? true
: isObject(v)
? true
: (() => {
throw t;
})();

export const equalsOrThrow = (v1: unknown, v2: unknown, t: Error): boolean =>
v1 === v2
? true
: (() => {
throw t;
})();

export const isBase64 = (value: unknown): boolean =>
Buffer.from(value as string, "base64").toString("base64") === value;

export const isBuffer = (value: unknown): boolean => value instanceof Buffer;

export const asBuffer = (value: string | Buffer | Uint32Array): Buffer =>
isBinary(value)
? Buffer.from(value as string)
: isBuffer(value)
? (value as Buffer)
: (() => {
throw new TypeError("is not buffer or a valid binary");
})();

export const asBase64 = (value: string | Buffer | Uint32Array): string => asBuffer(value).toString("base64");

export const clone = (o: Record<string, unknown>): Record<string, unknown> => JSON.parse(JSON.stringify(o));

export const isJsonContentType = (contentType: string): "" | RegExpMatchArray | null =>
contentType && contentType.match(/(json)/i);

export const asData = (data: unknown, contentType: string): string => {
// pattern matching alike
const maybeJson =
isString(data) && !isBase64(data) && isJsonContentType(contentType) ? JSON.parse(data as string) : data;

return isBinary(maybeJson) ? asBase64(maybeJson) : maybeJson;
};

export const isValidType = (v: boolean | number | string | Date | Uint32Array): boolean =>
isBoolean(v) || isInteger(v) || isString(v) || isDate(v) || isBinary(v);
2 changes: 0 additions & 2 deletions src/event/validation/index.ts

This file was deleted.

87 changes: 0 additions & 87 deletions src/event/validation/is.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/event/validation/validation_error.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/integration/http_binding_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import nock from "nock";

import { CloudEvent, Version } from "../../src";
import { emitBinary, emitStructured } from "../../src/transport/http";
import { asBase64 } from "../../src/event/validation/is";
import { asBase64 } from "../../src/event/validation";
import { AxiosResponse } from "axios";

const type = "com.github.pull.create";
Expand Down
2 changes: 1 addition & 1 deletion test/integration/utilities_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "mocha";
import { expect } from "chai";
import { isStringOrThrow, equalsOrThrow, isBase64, asData } from "../../src/event/validation/is";
import { isStringOrThrow, equalsOrThrow, isBase64, asData } from "../../src/event/validation";

describe("Utilities", () => {
describe("isStringOrThrow", () => {
Expand Down