forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation.ts
More file actions
47 lines (43 loc) · 1.23 KB
/
validation.ts
File metadata and controls
47 lines (43 loc) · 1.23 KB
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
import { ObjectId } from 'mongodb';
// This is trivial, but makes it simple to refactor if we swap monogodb for
// bson, say.
/**
* Checks if a string is a valid MongoDB ObjectID.
* @param id A string to check.
* @returns A boolean indicating if the string is a valid MongoDB ObjectID.
*/
export const isObjectID = (id?: string): boolean =>
id ? ObjectId.isValid(id) : false;
// Refer : http://stackoverflow.com/a/430240/1932901
/**
* Sanitizes a input by removing HTML tags.
* @deprecated
* @param value A string to sanitize.
* @returns A string with HTML tags removed.
*/
export const trimTags = (value: string): string => {
const tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*';
const tagOrComment = new RegExp(
'<(?:' +
// Comment body.
'!--(?:(?:-*[^->])*--+|-?)' +
// Special "raw text" elements whose content should be elided.
'|script\\b' +
tagBody +
'>[\\s\\S]*?</script\\s*' +
'|style\\b' +
tagBody +
'>[\\s\\S]*?</style\\s*' +
// Regular name
'|/?[a-z]' +
tagBody +
')>',
'gi'
);
let rawValue;
do {
rawValue = value;
value = value.replace(tagOrComment, '');
} while (value !== rawValue);
return value.replace(/</g, '<');
};