-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator-example.nools
129 lines (110 loc) · 3.36 KB
/
validator-example.nools
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
define User {
constructor: function(options){
this.id = options.id;
this.firstName = options.firstName;
this.lastName = options.lastName;
this.dob = options.dob;
this.email = options.email;
},
toString : function(){
return `User(id: ${this.id}, firstName: ${this.firstName}, lastName: ${this.lastName}, dob: ${this.dob ? this.dob.toISOString() : null}, email: ${this.email})`;
}
}
define ValidationError {
constructor: function(user, err){
this.user = user;
this.error = err;
}
}
function isEmailAddress(email){
return /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/.test(email);
}
rule 'Check First Name Required' {
when {
u : User !u.firstName;
}
then {
emit('validation-error', new ValidationError(u, 'First name is required'));
}
}
rule 'Check First Name No Numbers' {
when {
u : User (isString(u.firstName) and u.firstName like /[0-9]/);
}
then {
emit('validation-error', new ValidationError(u, 'First name is must not contain numbers'));
}
}
rule 'Check First Name Length' {
when {
u : User (isString(u.firstName) and u.firstName.length gt 10);
}
then {
emit('validation-error', new ValidationError(u, 'First name cannot be longer than 10 characters'));
}
}
rule 'Check Last Name Required' {
when {
u : User !u.lastName;
}
then {
emit('validation-error', new ValidationError(u, 'Last name is required'));
}
}
rule 'Check Last Name No Numbers' {
when {
u : User (isString(u.lastName) and u.lastName like /[0-9]/);
}
then {
emit('validation-error', new ValidationError(u, 'Last name is must not contain numbers'));
}
}
rule 'Check Last Name Length' {
when {
u : User (isString(u.lastName) and u.lastName.length gte 10);
}
then {
emit('validation-error', new ValidationError(u, 'Last name cannot be longer than 10 characters'));
}
}
rule 'Check Date Of Birth Required' {
when {
u : User isDate(u.dob) == false;
}
then {
emit('validation-error', new ValidationError(u, 'Date of birth is required'));
}
}
rule 'Check Date Of Birth Age To Young' {
when {
u : User isDate(u.dob) and u.dob gte yearsAgo(18);
}
then {
emit('validation-error', new ValidationError(u, 'Date Of Birth must be >= 18 years ago'));
}
}
rule 'Check Dob Age To Old' {
when {
u : User isDate(u.dob) and u.dob lte yearsAgo(100);
}
then {
emit('validation-error', new ValidationError(u, 'You cannot be older than 100 to use this app'));
}
}
rule 'Check Email Address' {
when {
u : User !isEmailAddress(u.email);
}
then {
emit('validation-error', new ValidationError(u, 'Invalid email address'));
}
}
rule 'Check Email Unique' {
when {
m1 : User isString(m1.email) {id:m1Id, email:m1Email};
m2 : User isString(m2.email) and m1Id neq m2.id and m2.email eq m1Email;
}
then {
emit('validation-error', new ValidationError(m2, 'Email is not unqiue'));
}
}