feat: simplify validation logic/imports (#265)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
This commit is contained in:
parent
4014da26f5
commit
4b54b272a5
|
@ -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);
|
|
@ -1,2 +0,0 @@
|
||||||
export * from "./is";
|
|
||||||
export * from "./validation_error";
|
|
|
@ -1,87 +0,0 @@
|
||||||
const isString = (v: unknown): boolean => typeof v === "string";
|
|
||||||
const isObject = (v: unknown): boolean => typeof v === "object";
|
|
||||||
const isDefined = (v: unknown): boolean => v && typeof v !== "undefined";
|
|
||||||
|
|
||||||
const isBoolean = (v: unknown): boolean => typeof v === "boolean";
|
|
||||||
const isInteger = (v: unknown): boolean => Number.isInteger(v as number);
|
|
||||||
const isDate = (v: unknown): boolean => v instanceof Date;
|
|
||||||
const isBinary = (v: unknown): boolean => v instanceof Uint32Array;
|
|
||||||
|
|
||||||
const isStringOrThrow = (v: unknown, t: Error): boolean =>
|
|
||||||
isString(v)
|
|
||||||
? true
|
|
||||||
: (() => {
|
|
||||||
throw t;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const isDefinedOrThrow = (v: unknown, t: Error): boolean =>
|
|
||||||
isDefined(v)
|
|
||||||
? true
|
|
||||||
: (() => {
|
|
||||||
throw t;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const isStringOrObjectOrThrow = (v: unknown, t: Error): boolean =>
|
|
||||||
isString(v)
|
|
||||||
? true
|
|
||||||
: isObject(v)
|
|
||||||
? true
|
|
||||||
: (() => {
|
|
||||||
throw t;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const equalsOrThrow = (v1: unknown, v2: unknown, t: Error): boolean =>
|
|
||||||
v1 === v2
|
|
||||||
? true
|
|
||||||
: (() => {
|
|
||||||
throw t;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const isBase64 = (value: unknown): boolean => Buffer.from(value as string, "base64").toString("base64") === value;
|
|
||||||
|
|
||||||
const isBuffer = (value: unknown): boolean => value instanceof Buffer;
|
|
||||||
|
|
||||||
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");
|
|
||||||
})();
|
|
||||||
|
|
||||||
const asBase64 = (value: string | Buffer | Uint32Array): string => asBuffer(value).toString("base64");
|
|
||||||
|
|
||||||
const clone = (o: Record<string, unknown>): Record<string, unknown> => JSON.parse(JSON.stringify(o));
|
|
||||||
|
|
||||||
const isJsonContentType = (contentType: string) => contentType && contentType.match(/(json)/i);
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
const isValidType = (v: boolean | number | string | Date | Uint32Array): boolean =>
|
|
||||||
isBoolean(v) || isInteger(v) || isString(v) || isDate(v) || isBinary(v);
|
|
||||||
|
|
||||||
export {
|
|
||||||
isString,
|
|
||||||
isStringOrThrow,
|
|
||||||
isObject,
|
|
||||||
isDefined,
|
|
||||||
isBoolean,
|
|
||||||
isInteger,
|
|
||||||
isDate,
|
|
||||||
isBinary,
|
|
||||||
isDefinedOrThrow,
|
|
||||||
isStringOrObjectOrThrow,
|
|
||||||
isValidType,
|
|
||||||
equalsOrThrow,
|
|
||||||
isBase64,
|
|
||||||
clone,
|
|
||||||
asData,
|
|
||||||
asBase64,
|
|
||||||
};
|
|
|
@ -1,14 +0,0 @@
|
||||||
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 : [];
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,7 @@ import nock from "nock";
|
||||||
|
|
||||||
import { CloudEvent, Version } from "../../src";
|
import { CloudEvent, Version } from "../../src";
|
||||||
import { emitBinary, emitStructured } from "../../src/transport/http";
|
import { emitBinary, emitStructured } from "../../src/transport/http";
|
||||||
import { asBase64 } from "../../src/event/validation/is";
|
import { asBase64 } from "../../src/event/validation";
|
||||||
import { AxiosResponse } from "axios";
|
import { AxiosResponse } from "axios";
|
||||||
|
|
||||||
const type = "com.github.pull.create";
|
const type = "com.github.pull.create";
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import "mocha";
|
import "mocha";
|
||||||
import { expect } from "chai";
|
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("Utilities", () => {
|
||||||
describe("isStringOrThrow", () => {
|
describe("isStringOrThrow", () => {
|
||||||
|
|
Loading…
Reference in New Issue