|
| 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); |
0 commit comments