cast.js
1.67 KB
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
'use strict';
// Load modules
const Hoek = require('hoek');
const Ref = require('./ref');
// Type modules are delay-loaded to prevent circular dependencies
// Declare internals
const internals = {
any: null,
date: require('./types/date'),
string: require('./types/string'),
number: require('./types/number'),
boolean: require('./types/boolean'),
alt: null,
object: null
};
exports.schema = function (config) {
internals.any = internals.any || new (require('./types/any'))();
internals.alt = internals.alt || require('./types/alternatives');
internals.object = internals.object || require('./types/object');
if (config !== undefined && config !== null && typeof config === 'object') {
if (config.isJoi) {
return config;
}
if (Array.isArray(config)) {
return internals.alt.try(config);
}
if (config instanceof RegExp) {
return internals.string.regex(config);
}
if (config instanceof Date) {
return internals.date.valid(config);
}
return internals.object.keys(config);
}
if (typeof config === 'string') {
return internals.string.valid(config);
}
if (typeof config === 'number') {
return internals.number.valid(config);
}
if (typeof config === 'boolean') {
return internals.boolean.valid(config);
}
if (Ref.isRef(config)) {
return internals.any.valid(config);
}
Hoek.assert(config === null, 'Invalid schema content:', config);
return internals.any.valid(null);
};
exports.ref = function (id) {
return Ref.isRef(id) ? id : Ref.create(id);
};