summaryrefslogtreecommitdiff
path: root/src/api/validator.ts
blob: 3f1678e35dd752a041d61d2c5b1fce833a81837e (plain)
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
import * as mongo from 'mongodb';
import hasDuplicates from '../common/has-duplicates';

type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object';

type Validator<T> = ((x: T) => boolean | string) | ((x: T) => boolean | string)[];

function validate(value: any, type: 'id', isRequired?: boolean): [mongo.ObjectID, string];
function validate(value: any, type: 'string', isRequired?: boolean, validator?: Validator<string>): [string, string];
function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator<number>): [number, string];
function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string];
function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator<any[]>): [any[], string];
function validate(value: any, type: 'set', isRequired?: boolean, validator?: Validator<any[]>): [any[], string];
function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator<any>): [any, string];
function validate<T>(value: any, type: Type, isRequired?: boolean, validator?: Validator<T>): [T, string] {
	if (value === undefined || value === null) {
		if (isRequired) {
			return [null, 'is-required']
		} else {
			return [null, null]
		}
	}

	switch (type) {
		case 'id':
			if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) {
				return [null, 'incorrect-id'];
			}
			break;

		case 'string':
			if (typeof value != 'string') {
				return [null, 'must-be-a-string'];
			}
			break;

		case 'number':
			if (!Number.isFinite(value)) {
				return [null, 'must-be-a-number'];
			}
			break;

		case 'boolean':
			if (typeof value != 'boolean') {
				return [null, 'must-be-a-boolean'];
			}
			break;

		case 'array':
			if (!Array.isArray(value)) {
				return [null, 'must-be-an-array'];
			}
			break;

		case 'set':
			if (!Array.isArray(value)) {
				return [null, 'must-be-an-array'];
			} else if (hasDuplicates(value)) {
				return [null, 'duplicated-contents'];
			}
			break;

		case 'object':
			if (typeof value != 'object') {
				return [null, 'must-be-an-object'];
			}
			break;
	}

	if (type == 'id') value = new mongo.ObjectID(value);

	if (validator) {
		const validators = Array.isArray(validator) ? validator : [validator];
		for (let i = 0; i < validators.length; i++) {
			const result = validators[i](value);
			if (result === false) {
				return [null, 'invalid-format'];
			} else if (typeof result == 'string') {
				return [null, result];
			}
		}
	}

	return [value, null];
}

export default validate;