Skip to content

Commit b7fd5cd

Browse files
committed
Updating for exercise
1 parent 628b032 commit b7fd5cd

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const Joi = require('joi');
2+
3+
const schema = Joi.object()
4+
.keys({
5+
// Requires a given string value
6+
username: Joi.string()
7+
.alphanum()
8+
.min(3)
9+
.max(30)
10+
.required(),
11+
// Define password complexity requirements through regex (consider more complex regex)
12+
password: Joi.string()
13+
.regex(/^[a-zA-Z0-9]{3,30}$/)
14+
.required(),
15+
// Force passwords to match
16+
password_confirmation: Joi.any()
17+
.equal(Joi.ref('password'))
18+
.required(),
19+
// Accept different Joi types. Optional, unconstrainted string or number
20+
access_token: [Joi.string(), Joi.number()],
21+
// Required birthyear to be an int between range
22+
birthyear: Joi.number()
23+
.integer()
24+
.min(1900)
25+
.max(2013)
26+
.required(),
27+
// Validate email address from example.com (remember spoofing considerations)
28+
email: Joi.string()
29+
.email()
30+
.regex(/example\.com$/),
31+
marketing_opt_out: Joi.boolean(),
32+
csrf_token: Joi.string()
33+
.guid({
34+
version: 'uuidv4',
35+
})
36+
.required(),
37+
sex: Joi.string()
38+
.equal(['M', 'F', 'MALE', 'FEMALE', 'DECLINE'])
39+
.required(),
40+
time: Joi.date()
41+
.timestamp('javascript'),
42+
roles: Joi.object()
43+
.keys(),
44+
})
45+
// email must be accompanied by marketing_opt_out
46+
.with('email', 'marketing_opt_out');
47+
48+
const result = Joi.validate({
49+
username: 'Ronald',
50+
password: 'McDonald',
51+
password_confirmation: 'McDonald',
52+
birthyear: 2010,
53+
54+
marketing_opt_out: true,
55+
csrf_token: '6d4d8c14-ef12-45d9-ab3c-5dddf941fb76',
56+
sex: 'F',
57+
time: 1534942475121,
58+
roles: {},
59+
}, schema);
60+
61+
// If result.error === null, payload is valid
62+
console.log(`The validation error is: ${result.error}`);

0 commit comments

Comments
 (0)