Skip to content

Commit 92bc088

Browse files
committed
Merge branch 'development'
2 parents 39e3e18 + 03c4ac6 commit 92bc088

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

hapijoi/abortearly.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env node
2+
const Joi = require("joi");
3+
4+
const schema = Joi.object().keys({
5+
username: Joi.string().min(2).max(30).required(),
6+
password: Joi.string().regex(/^[\w]{8,30}$/),
7+
registered: Joi.number().integer().min(2012).max(2019),
8+
married: Joi.boolean().required()
9+
});
10+
11+
let username = "Roger Brown";
12+
let password = "s#cret12";
13+
let registered = "2011";
14+
let married = false;
15+
16+
let data = { username, password, registered, married };
17+
let options = { abortEarly: false };
18+
19+
const { error, value } = schema.validate(data, options);
20+
21+
if (error) {
22+
console.log(error.details);
23+
} else {
24+
console.log(value);
25+
}

hapijoi/arrays.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node
2+
const Joi = require("joi");
3+
4+
const schema = Joi.array().min(2).max(10);
5+
6+
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ];
7+
8+
const { error, value } = schema.validate(data);
9+
10+
if (error) {
11+
console.log(error.details);
12+
} else {
13+
console.log(value);
14+
}

hapijoi/casting.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
const Joi = require("joi");
3+
4+
const schema = Joi.object().keys({
5+
timestamp: Joi.date().timestamp(),
6+
val: Joi.number()
7+
});
8+
9+
let val = "23543";
10+
let timestamp = 1559761841;
11+
12+
let data = { val, timestamp };
13+
14+
const { error, value } = schema.validate(data);
15+
16+
if (error) {
17+
console.log(error.details);
18+
} else {
19+
console.log(value);
20+
}

hapijoi/dates.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
const Joi = require("@hapi/joi");
3+
4+
const schema = Joi.object().keys({
5+
6+
timestamp: Joi.date().timestamp(),
7+
isodate: Joi.date().iso(),
8+
registered: Joi.date().greater("2018-01-01")
9+
});
10+
11+
let timestamp = 1559761841;
12+
let isodate = "1970-01-19T01:16:01.841Z";
13+
let registered = "2019-02-12";
14+
15+
let data = { timestamp, isodate, registered };
16+
17+
const { error, value } = schema.validate(data);
18+
19+
if (error) {
20+
console.log(error.details);
21+
} else {
22+
console.log(value);
23+
}

hapijoi/functions.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
const Joi = require("joi");
3+
4+
const schema = Joi.func().arity(2);
5+
6+
function add2int(x, y) {
7+
return x + y;
8+
}
9+
10+
const { error, value } = schema.validate(add2int);
11+
if (error) {
12+
console.log(error.details);
13+
} else {
14+
console.log(value);
15+
}

hapijoi/numbers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
const Joi = require("joi");
3+
4+
const schema = Joi.object().keys({
5+
age: Joi.number().min(18).max(129),
6+
price: Joi.number().positive(),
7+
experience: Joi.number().greater(5)
8+
});
9+
10+
let age = 35;
11+
let price = -124.3;
12+
let experience = 6;
13+
14+
let data = { age, price, experience };
15+
16+
const { error, value } = schema.validate(data);
17+
18+
if (error) {
19+
console.log(error.details);
20+
} else {
21+
console.log(value);
22+
}

0 commit comments

Comments
 (0)