1
+ import { validate , validateBySchema , registerSchema } from "../../src/index" ;
2
+ import { Post } from "./Post" ;
3
+
4
+ // 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
5
+ const postSchema = require ( __dirname + "/../../../../sample/sample5-schemas/post.json" ) ;
6
+
7
+ // register this schema
8
+ registerSchema ( postSchema ) ;
9
+
10
+ // Sample1. simple validation
11
+
12
+ let post1 = new Post ( ) ;
13
+ post1 . title = "Hello world" ; // should pass
14
+ post1 . text = "this is a great post about hello world" ; // should pass
15
+ post1 . rating = 10 ; // should pass
16
+ post1 . email = "info@google.com" ; // should pass
17
+ post1 . site = "google.com" ; // should pass
18
+ post1 . createDate = new Date ( ) ; // should pass
19
+ post1 . tags = [ "abcd1" , "abcd2" , "abcd3" , "abcd4" , "abcd4" ] ; // should pass
20
+
21
+ validateBySchema ( "post" , post1 ) . then ( result => {
22
+ console . log ( "1. should pass: " , result ) ; // should pass completely, e.g. return empty array
23
+ } ) ;
24
+
25
+ let post2 = new Post ( ) ;
26
+ post2 . title = "Hello" ; // should not pass
27
+ post2 . text = "this is a great post about hell world" ; // should not pass
28
+ post2 . rating = 11 ; // should not pass
29
+ post2 . email = "google.com" ; // should not pass
30
+ post2 . site = "googlecom" ; // should not pass
31
+ // should not pass because date property is missing
32
+
33
+ validateBySchema ( "post" , post2 ) . then ( result => {
34
+ console . log ( "2. should not pass: " , result ) ; // should not pass completely, must return array of ValidationError-s
35
+ } ) ;
36
+
37
+ // Sample2. using validation options to skip properties that are not defined
38
+
39
+ let post3 = new Post ( ) ;
40
+ post3 . title = "Hello" ; // should not pass
41
+ post3 . text = "this is a great post about hell world" ; // should not pass
42
+ post3 . rating = 11 ; // should not pass
43
+ post3 . email = "google.com" ; // should not pass
44
+ post3 . site = "googlecom" ; // should not pass
45
+
46
+ validateBySchema ( "post" , post3 , { skipMissingProperties : true } ) . then ( result => {
47
+ console . log ( "3. should not pass: " , result ) ; // should not pass, but returned ValidationError-s should not have error about date field
48
+ } ) ;
49
+
50
+ let post4 = new Post ( ) ;
51
+ post4 . title = "Hello world" ; // should pass
52
+ post4 . text = "this is a great post about hello world" ; // should pass
53
+ post4 . rating = 10 ; // should pass
54
+ post4 . email = "info@google.com" ; // should pass
55
+ post4 . site = "google.com" ; // should pass
56
+
57
+ validateBySchema ( "post" , post4 , { skipMissingProperties : true } ) . then ( result => {
58
+ console . log ( "4. should pass: " , result ) ; // should pass even if date is not set
59
+ } ) ;
60
+
61
+ // Sample3. using validation groups
62
+
63
+ let post5 = new Post ( ) ;
64
+ post5 . title = "Hello world" ; // should pass
65
+ post5 . text = "this is a great post about hello world" ; // should pass
66
+ post5 . rating = 10 ; // should pass
67
+ post5 . email = "info@google.com" ; // should pass
68
+ post5 . site = "google.com" ; // should pass
69
+
70
+ validateBySchema ( "post" , post5 , { skipMissingProperties : true } ) . then ( result => {
71
+ console . log ( "5. should pass: " , result ) ; // should pass even if date is not set
72
+ } ) ;
73
+
74
+ // Sample4. array validation
75
+
76
+ let post6 = new Post ( ) ;
77
+ post6 . title = "Hello world" ; // should pass
78
+ post6 . text = "this is a great post about hello world" ; // should pass
79
+ post6 . rating = 10 ; // should pass
80
+ post6 . email = "info@google.com" ; // should pass
81
+ post6 . site = "google.com" ; // should pass
82
+ post6 . createDate = new Date ( ) ; // should pass
83
+ post6 . tags = [ "abcd1" , "abcd2" , "abcd3" , "abcd4" , "abcd4" ] ;
84
+
85
+ validateBySchema ( "post" , post6 ) . then ( result => {
86
+ console . log ( "6. should pass: " , result ) ; // should pass completely, e.g. return empty array
87
+ } ) ;
88
+
89
+ let post7 = new Post ( ) ;
90
+ post7 . title = "Hello world" ; // should pass
91
+ post7 . text = "this is a great post about hello world" ; // should pass
92
+ post7 . rating = 10 ; // should pass
93
+ post7 . email = "info@google.com" ; // should pass
94
+ post7 . site = "google.com" ; // should pass
95
+ post7 . createDate = new Date ( ) ; // should pass
96
+ post7 . tags = [ "news" , "a" ] ;
97
+
98
+ validateBySchema ( "post" , post7 ) . then ( result => {
99
+ console . log ( "7. should not pass: " , result ) ; // should not pass
100
+ } ) ;
101
+
102
+ let post8 = new Post ( ) ;
103
+ post8 . title = "Hello world" ; // should pass
104
+ post8 . text = "this is a great post about hello world" ; // should pass
105
+ post8 . rating = 10 ; // should pass
106
+ post8 . email = "info@google.com" ; // should pass
107
+ post8 . site = "google.com" ; // should pass
108
+ post8 . createDate = new Date ( ) ; // should pass
109
+ post8 . tags = [ ] ;
110
+
111
+ validateBySchema ( "post" , post8 ) . then ( result => {
112
+ console . log ( "8. should not pass: " , result ) ; // should not pass
113
+ } ) ;
114
+
115
+ let post9 = new Post ( ) ;
116
+ post9 . title = "Hello world" ; // should pass
117
+ post9 . text = "this is a great post about hello world" ; // should pass
118
+ post9 . rating = 10 ; // should pass
119
+ post9 . email = "info@google.com" ; // should pass
120
+ post9 . site = "google.com" ; // should pass
121
+ post9 . createDate = new Date ( ) ; // should pass
122
+ post9 . tags = [ "abcd1" , "abcd2" , "abcd3" , "abcd4" , "abcd4" , "abcd4" ] ;
123
+
124
+ validateBySchema ( "post" , post9 ) . then ( result => {
125
+ console . log ( "9. should not pass: " , result ) ; // should not pass
126
+ } ) ;
127
+
128
+ let post10 = new Post ( ) ;
129
+ post10 . title = "Hello world" ; // should pass
130
+ post10 . text = "this is a great post about hello world" ; // should pass
131
+ post10 . rating = 10 ; // should pass
132
+ post10 . email = "info@google.com" ; // should pass
133
+ post10 . site = "google.com" ; // should pass
134
+ post10 . createDate = new Date ( ) ; // should pass
135
+ post10 . tags = [ "abcd1" , "abcd2" , "abcd3" , "abcd4" , "abcd4" ] ;
136
+
137
+ validateBySchema ( "post" , post10 ) . then ( result => {
138
+ console . log ( "10. should pass: " , result ) ; // should pass
139
+ } ) ;
140
+
141
+ let post11 = new Post ( ) ;
142
+ post11 . title = "Hello world" ; // should pass
143
+ post11 . text = "this is a great post about hello world" ; // should pass
144
+ post11 . rating = 10 ; // should pass
145
+ post11 . email = "info@google.com" ; // should pass
146
+ post11 . site = "google.com" ; // should pass
147
+ post11 . createDate = new Date ( ) ; // should pass
148
+ post11 . tags = null ;
149
+
150
+ validateBySchema ( "post" , post11 ) . then ( result => {
151
+ console . log ( "11. should not pass: " , result ) ; // should not pass
152
+ } ) ;
0 commit comments