forked from typestack/class-sanitizer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
189 lines (146 loc) · 4.02 KB
/
index.ts
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
import 'reflect-metadata';
import 'expect-more-jest';
import { SanitizerInterface } from '../src/SanitizerInterface';
describe('Sanitizer', () => {
beforeEach(() => {
// Because `class-sanitizer` stores metadata of all annotated classes in a
// single, global object, we make sure to get a fresh copy of the
// module for every test.
jest.resetModules();
});
test('Basic sanitization', async () => {
const {
Rtrim,
Ltrim,
ToInt,
ToBoolean,
NormalizeEmail,
ToLowerCase,
ToUpperCase,
sanitize,
} = await import('../src/index');
class A {
@Rtrim() text: string;
@NormalizeEmail() email: string;
@Ltrim() bio: string;
@ToInt() age: any;
@ToBoolean() isPremium: any;
@ToLowerCase() color1: string;
@ToUpperCase() color2: string;
}
const a = new A();
a.bio = ' abcdef';
a.text = 'test ';
a.email = '[email protected]';
a.age = '18';
a.isPremium = '1';
a.color1 = '#fFf';
a.color2 = '#FfF';
sanitize(a);
expect(a.bio).not.toStartWith(' ');
expect(a.text).not.toEndWith(' ');
expect(a.email).toBe('[email protected]');
expect(a.age).toBeNumber();
expect(a.age).toBe(18);
expect(a.isPremium).toBeBoolean();
expect(a.isPremium).toBe(true);
expect(a.color1).toMatch('#fff');
expect(a.color2).toMatch('#FFF');
});
test('Nested objects', async () => {
const {
Trim,
ToDate,
SanitizeNested,
sanitize,
} = await import('../src/index');
class Tag {
@Trim() name: string;
@ToDate() createdOn: string | Date;
}
class Post {
title: string;
@SanitizeNested() tags: Tag[];
}
const tag1 = new Tag();
tag1.name = 'ja';
const tag2 = new Tag();
tag2.name = 'node.js ';
tag2.createdOn = '2010-10-10';
const post1 = new Post();
post1.title = 'Hello world';
post1.tags = [tag1, tag2];
sanitize(post1);
expect(post1.tags[1]).toBe(tag2);
expect(tag2.name).not.toEndWith(' ');
expect(tag2.createdOn).toBeInstanceOf(Date);
});
test('Custom sanitizer', async () => {
const { Sanitize, SanitizerConstraint } = await import('../src/index');
@SanitizerConstraint()
class LetterReplacer implements SanitizerInterface {
sanitize(text: string): string {
return text.replace(/o/g, 'w');
}
}
const { sanitize } = await import('../src/index');
class Post {
@Sanitize(LetterReplacer) title: string;
}
const post1 = new Post();
post1.title = 'Hello world';
sanitize(post1);
expect(post1.title).toMatch('Hellw wwrld');
});
test('Inheritance', async () => {
const {
sanitize,
ToInt,
Trim,
Blacklist,
Rtrim,
} = await import('../src/index');
class BasePost {
@ToInt() rating: any;
}
class Post extends BasePost {
@Trim() title: string;
@Rtrim(['.'])
@Blacklist(/(1-9)/)
text: string;
}
const post1 = new Post();
post1.title = ' Hello world ';
post1.text = '1. this is a great (2) post about hello 3 world.';
post1.rating = '12.2';
sanitize(post1);
expect(post1.title).toMatch('Hello world');
expect(post1.text).toStartWith(
'. this is a great post about hello world',
);
expect(post1.text).not.toEndWith('.');
expect(post1.rating).toBe(12);
});
/* Test for https://github.com/typestack/class-sanitizer/issues/8 */
test(
'Two classes that both have a property with the same name are ' +
'not confused when performing sanitization',
async () => {
const { Trim, sanitize } = await import('../src/index');
class A {
text: string;
}
class B {
@Trim() text: string;
}
const a = new A();
const b = new B();
a.text = 'space at the end ';
b.text = 'space at the end ';
sanitize(a);
sanitize(b);
expect(a.text).toEndWith(' ');
expect(b.text).not.toEndWith(' ');
},
);
});