summary refs log tree commit diff
path: root/src/util/instanceOf.ts
blob: d83dc39cc63db5c4dbd4d7effe37152f44efca0d (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
// different version of lambert-server instanceOf with discord error format

import { NextFunction, Request, Response } from "express";
import { Tuple } from "lambert-server";

const OPTIONAL_PREFIX = "$";

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

class FieldError extends Error {
	constructor(public code: string, public message: string) {
		super(message);
	}
}

export function instanceOf(
	type: any,
	value: any,
	{ path = "", optional = false, errors = {} }: { path?: string; optional?: boolean; errors?: any } = {}
): Boolean {
	try {
		if (!type) return true; // no type was specified

		if (value == null) {
			if (optional) return true;
			throw new FieldError("BASE_TYPE_REQUIRED", `This field is required`);
		}

		switch (type) {
			case String:
				if (typeof value === "string") return true;
				throw new FieldError("BASE_TYPE_STRING", `This field must be a string`);
			case Number:
				value = Number(value);
				if (typeof value === "number" && !isNaN(value)) return true;
				throw new FieldError("BASE_TYPE_NUMBER", `This field must be a number`);
			case BigInt:
				try {
					value = BigInt(value);
					if (typeof value === "bigint") return true;
				} catch (error) {}
				throw new FieldError("BASE_TYPE_BIGINT", `This field must be a bigint`);
			case Boolean:
				if (value == "true") value = true;
				if (value == "false") value = false;
				if (typeof value === "boolean") return true;
				throw new FieldError("BASE_TYPE_BOOLEAN", `This field must be a boolean`);
		}

		if (typeof type === "object") {
			if (type?.constructor?.name != "Object") {
				if (type instanceof Tuple) {
					if ((<Tuple>type).types.some((x) => instanceOf(x, value, { path, optional, errors }))) return true;
					throw new FieldError("BASE_TYPE_CHOICES", `This field must be one of (${type.types})`);
				}
				if (value instanceof type) return true;
				throw new FieldError("BASE_TYPE_CLASS", `This field must be an instance of ${type}`);
			}
			if (typeof value !== "object") throw new FieldError("BASE_TYPE_OBJECT", `This field must be a object`);

			if (Array.isArray(type)) {
				if (!Array.isArray(value)) throw new FieldError("BASE_TYPE_ARRAY", `This field must be an array`);
				if (!type.length) return true; // type array didn't specify any type

				return (
					value.every((val, i) => {
						errors[i] = {};
						return (
							instanceOf(type[0], val, { path: `${path}[${i}]`, optional, errors: errors[i] }) === true
						);
					}) || errors
				);
			}

			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", `Unkown 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[key] = {};

					return (
						instanceOf(type[key], value[newKey], {
							path: `${path}.${newKey}`,
							optional: OPTIONAL,
							errors: errors[key],
						}) === true
					);
				}) || errors
			);
		} else if (typeof type === "number" || typeof type === "string" || typeof type === "boolean") {
			if (value === type) return true;
			throw new FieldError("BASE_TYPE_CONSTANT", `This field must be ${value}`);
		} else if (typeof type === "bigint") {
			if (BigInt(value) === type) return true;
			throw new FieldError("BASE_TYPE_CONSTANT", `This field must be ${value}`);
		}

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