summary refs log tree commit diff
path: root/api/src/util/instanceOf.ts
blob: 4d9034e5b1a3eb888fa7808f384080a8c7081ed0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// different version of lambert-server instanceOf with discord error format

import { NextFunction, Request, Response } from "express";
import { Tuple } from "lambert-server";
import "missing-native-js-functions";

export const OPTIONAL_PREFIX = "$";
export const EMAIL_REGEX =
	/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

export function check(schema: any) {
	return (req: Request, res: Response, next: NextFunction) => {
		try {
			const result = instanceOf(schema, req.body, { path: "body", req, ref: { obj: null, key: "" } });
			if (result === true) return next();
			throw result;
		} catch (error) {
			return res.status(400).json({ code: 50035, message: "Invalid Form Body", success: false, errors: error });
		}
	};
}

export function FieldErrors(fields: Record<string, { code?: string; message: string }>) {
	return new FieldError(
		50035,
		"Invalid Form Body",
		fields.map(({ message, code }) => ({
			_errors: [
				{
					message,
					code: code || "BASE_TYPE_INVALID"
				}
			]
		}))
	);
}

// TODO: implement Image data type: Data URI scheme that supports JPG, GIF, and PNG formats. An example Data URI format is: data:image/jpeg;base64,BASE64_ENCODED_JPEG_IMAGE_DATA
// Ensure you use the proper content type (image/jpeg, image/png, image/gif) that matches the image data being provided.

export class FieldError extends Error {
	constructor(public code: string | number, public message: string, public errors?: any) {
		super(message);
	}
}

export class Email {
	constructor(public email: string) {}
	check() {
		return !!this.email.match(EMAIL_REGEX);
	}
}

export class Length {
	constructor(public type: any, public min: number, public max: number) {}

	check(value: string) {
		if (typeof value === "string" || Array.isArray(value)) return value.length >= this.min && value.length <= this.max;
		if (typeof value === "number" || typeof value === "bigint") return value >= this.min && value <= this.max;
		return false;
	}
}

export function instanceOf(
	type: any,
	value: any,
	{
		path = "",
		optional = false,
		errors = {},
		req,
		ref
	}: { path?: string; optional?: boolean; errors?: any; req: Request; ref?: { key: string | number; obj: any } }
): Boolean {
	if (!ref) ref = { obj: null, key: "" };
	if (!path) path = "body";
	if (!type) return true; // no type was specified

	try {
		if (value == null) {
			if (optional) return true;
			throw new FieldError("BASE_TYPE_REQUIRED", req.t("common:field.BASE_TYPE_REQUIRED"));
		}

		switch (type) {
			case String:
				value = `${value}`;
				ref.obj[ref.key] = value;
				if (typeof value === "string") return true;
				throw new FieldError("BASE_TYPE_STRING", req.t("common:field.BASE_TYPE_STRING"));
			case Number:
				value = Number(value);
				ref.obj[ref.key] = value;
				if (typeof value === "number" && !isNaN(value)) return true;
				throw new FieldError("BASE_TYPE_NUMBER", req.t("common:field.BASE_TYPE_NUMBER"));
			case BigInt:
				try {
					value = BigInt(value);
					ref.obj[ref.key] = value;
					if (typeof value === "bigint") return true;
				} catch (error) {}
				throw new FieldError("BASE_TYPE_BIGINT", req.t("common:field.BASE_TYPE_BIGINT"));
			case Boolean:
				if (value == "true") value = true;
				if (value == "false") value = false;
				ref.obj[ref.key] = value;
				if (typeof value === "boolean") return true;
				throw new FieldError("BASE_TYPE_BOOLEAN", req.t("common:field.BASE_TYPE_BOOLEAN"));

			case Email:
				if (new Email(value).check()) return true;
				throw new FieldError("EMAIL_TYPE_INVALID_EMAIL", req.t("common:field.EMAIL_TYPE_INVALID_EMAIL"));
			case Date:
				value = new Date(value);
				ref.obj[ref.key] = value;
				// value.getTime() can be < 0, if it is before 1970
				if (!isNaN(value)) return true;
				throw new FieldError("DATE_TYPE_PARSE", req.t("common:field.DATE_TYPE_PARSE"));
		}

		if (typeof type === "object") {
			if (Array.isArray(type)) {
				if (!Array.isArray(value)) throw new FieldError("BASE_TYPE_ARRAY", req.t("common:field.BASE_TYPE_ARRAY"));
				if (!type.length) return true; // type array didn't specify any type

				return (
					value.every((val, i) => {
						errors[i] = {};

						if (
							instanceOf(type[0], val, {
								path: `${path}[${i}]`,
								optional,
								errors: errors[i],
								req,
								ref: { key: i, obj: value }
							}) === true
						) {
							delete errors[i];
							return true;
						}

						return false;
					}) || errors
				);
			} else if (type?.constructor?.name != "Object") {
				if (type instanceof Tuple) {
					if ((<Tuple>type).types.some((x) => instanceOf(x, value, { path, optional, errors, req, ref }))) return true;
					throw new FieldError("BASE_TYPE_CHOICES", req.t("common:field.BASE_TYPE_CHOICES", { types: type.types }));
				} else if (type instanceof Length) {
					let length = <Length>type;
					if (instanceOf(length.type, value, { path, optional, req, ref, errors }) !== true) return errors;
					let val = ref.obj[ref.key];
					if ((<Length>type).check(val)) return true;
					throw new FieldError(
						"BASE_TYPE_BAD_LENGTH",
						req.t("common:field.BASE_TYPE_BAD_LENGTH", {
							length: `${type.min} - ${type.max}`
						})
					);
				}
				try {
					if (value instanceof type) return true;
				} catch (error) {
					throw new FieldError("BASE_TYPE_CLASS", req.t("common:field.BASE_TYPE_CLASS", { type }));
				}
			}

			if (typeof value !== "object") throw new FieldError("BASE_TYPE_OBJECT", req.t("common:field.BASE_TYPE_OBJECT"));

			const diff = Object.keys(value).missing(
				Object.keys(type).map((x) => (x.startsWith(OPTIONAL_PREFIX) ? x.slice(OPTIONAL_PREFIX.length) : x))
			);

			if (diff.length) throw new FieldError("UNKOWN_FIELD", req.t("common:field.UNKOWN_FIELD", { key: diff }));

			return (
				Object.keys(type).every((key) => {
					let newKey = key;
					const OPTIONAL = key.startsWith(OPTIONAL_PREFIX);
					if (OPTIONAL) newKey = newKey.slice(OPTIONAL_PREFIX.length);
					errors[newKey] = {};

					if (
						instanceOf(type[key], value[newKey], {
							path: `${path}.${newKey}`,
							optional: OPTIONAL,
							errors: errors[newKey],
							req,
							ref: { key: newKey, obj: value }
						}) === true
					) {
						delete errors[newKey];
						return true;
					}

					return false;
				}) || errors
			);
		} else if (typeof type === "number" || typeof type === "string" || typeof type === "boolean") {
			if (value === type) return true;
			throw new FieldError("BASE_TYPE_CONSTANT", req.t("common:field.BASE_TYPE_CONSTANT", { value: type }));
		} else if (typeof type === "bigint") {
			if (BigInt(value) === type) return true;
			throw new FieldError("BASE_TYPE_CONSTANT", req.t("common:field.BASE_TYPE_CONSTANT", { value: type }));
		}

		return type == value;
	} catch (error) {
		let e = error as FieldError;
		errors._errors = [{ message: e.message, code: e.code }];
		return errors;
	}
}