-
Notifications
You must be signed in to change notification settings - Fork 815
/
Copy pathapp.ts
152 lines (124 loc) · 5.59 KB
/
app.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
import { validate, registerSchema } from '../../src/index';
import { Post } from './Post';
// load schema. we load it a bit tricky way because we output source code into separate directory, so our json resource left in another directory
const postSchema = require(__dirname + '/../../../../sample/sample5-schemas/post.json');
// register this schema
registerSchema(postSchema);
// Sample1. simple validation
let post1 = new Post();
post1.title = 'Hello world'; // should pass
post1.text = 'this is a great post about hello world'; // should pass
post1.rating = 10; // should pass
post1.email = 'info@google.com'; // should pass
post1.site = 'google.com'; // should pass
post1.createDate = new Date(); // should pass
post1.tags = ['abcd1', 'abcd2', 'abcd3', 'abcd4', 'abcd4']; // should pass
validate('post', post1).then(result => {
console.log('1. should pass: ', result); // should pass completely, e.g. return empty array
});
let post2 = new Post();
post2.title = 'Hello'; // should not pass
post2.text = 'this is a great post about hell world'; // should not pass
post2.rating = 11; // should not pass
post2.email = 'google.com'; // should not pass
post2.site = 'googlecom'; // should not pass
// should not pass because date property is missing
validate('post', post2).then(result => {
console.log('2. should not pass: ', result); // should not pass completely, must return array of ValidationError-s
});
// Sample2. using validation options to skip properties that are not defined
let post3 = new Post();
post3.title = 'Hello'; // should not pass
post3.text = 'this is a great post about hell world'; // should not pass
post3.rating = 11; // should not pass
post3.email = 'google.com'; // should not pass
post3.site = 'googlecom'; // should not pass
validate('post', post3, { skipMissingProperties: true }).then(result => {
console.log('3. should not pass: ', result); // should not pass, but returned ValidationError-s should not have error about date field
});
let post4 = new Post();
post4.title = 'Hello world'; // should pass
post4.text = 'this is a great post about hello world'; // should pass
post4.rating = 10; // should pass
post4.email = 'info@google.com'; // should pass
post4.site = 'google.com'; // should pass
validate('post', post4, { skipMissingProperties: true }).then(result => {
console.log('4. should pass: ', result); // should pass even if date is not set
});
// Sample3. using validation groups
let post5 = new Post();
post5.title = 'Hello world'; // should pass
post5.text = 'this is a great post about hello world'; // should pass
post5.rating = 10; // should pass
post5.email = 'info@google.com'; // should pass
post5.site = 'google.com'; // should pass
validate('post', post5, { skipMissingProperties: true }).then(result => {
console.log('5. should pass: ', result); // should pass even if date is not set
});
// Sample4. array validation
let post6 = new Post();
post6.title = 'Hello world'; // should pass
post6.text = 'this is a great post about hello world'; // should pass
post6.rating = 10; // should pass
post6.email = 'info@google.com'; // should pass
post6.site = 'google.com'; // should pass
post6.createDate = new Date(); // should pass
post6.tags = ['abcd1', 'abcd2', 'abcd3', 'abcd4', 'abcd4'];
validate('post', post6).then(result => {
console.log('6. should pass: ', result); // should pass completely, e.g. return empty array
});
let post7 = new Post();
post7.title = 'Hello world'; // should pass
post7.text = 'this is a great post about hello world'; // should pass
post7.rating = 10; // should pass
post7.email = 'info@google.com'; // should pass
post7.site = 'google.com'; // should pass
post7.createDate = new Date(); // should pass
post7.tags = ['news', 'a'];
validate('post', post7).then(result => {
console.log('7. should not pass: ', result); // should not pass
});
let post8 = new Post();
post8.title = 'Hello world'; // should pass
post8.text = 'this is a great post about hello world'; // should pass
post8.rating = 10; // should pass
post8.email = 'info@google.com'; // should pass
post8.site = 'google.com'; // should pass
post8.createDate = new Date(); // should pass
post8.tags = [];
validate('post', post8).then(result => {
console.log('8. should not pass: ', result); // should not pass
});
let post9 = new Post();
post9.title = 'Hello world'; // should pass
post9.text = 'this is a great post about hello world'; // should pass
post9.rating = 10; // should pass
post9.email = 'info@google.com'; // should pass
post9.site = 'google.com'; // should pass
post9.createDate = new Date(); // should pass
post9.tags = ['a', 'abcd1', 'abcd2', 'abcd3', 'abcd4', 'abcd4', 'abcd4'];
validate('post', post9).then(result => {
console.log('9. should not pass: ', result); // should not pass
});
let post10 = new Post();
post10.title = 'Hello world'; // should pass
post10.text = 'this is a great post about hello world'; // should pass
post10.rating = 10; // should pass
post10.email = 'info@google.com'; // should pass
post10.site = 'google.com'; // should pass
post10.createDate = new Date(); // should pass
post10.tags = ['abcd1', 'abcd2', 'abcd3', 'abcd4', 'abcd4'];
validate('post', post10).then(result => {
console.log('10. should pass: ', result); // should pass
});
let post11 = new Post();
post11.title = 'Hello world'; // should pass
post11.text = 'this is a great post about hello world'; // should pass
post11.rating = 10; // should pass
post11.email = 'info@google.com'; // should pass
post11.site = 'google.com'; // should pass
post11.createDate = new Date(); // should pass
post11.tags = null;
validate('post', post11).then(result => {
console.log('11. should not pass: ', result); // should not pass
});