-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cast.js
executable file
·147 lines (101 loc) · 3.23 KB
/
cast.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
const Hoek = require('@hapi/hoek');
const Common = require('./common');
const Ref = require('./ref');
const internals = {};
exports.schema = function (Joi, config, options = {}) {
if (options.appendPath) {
return internals.appendPath(Joi, config);
}
if (config !== undefined &&
config !== null &&
typeof config === 'object') {
if (Ref.isRef(config)) {
return Joi.valid(config);
}
if (Common.isSchema(config)) {
return config;
}
if (Array.isArray(config)) {
return Joi.alternatives().try(config);
}
if (config instanceof RegExp) {
return Joi.string().regex(config);
}
if (config instanceof Date) {
return Joi.date().valid(config);
}
Hoek.assert(Object.getPrototypeOf(config) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
return Joi.object().keys(config);
}
if (typeof config === 'string') {
return Joi.string().valid(config);
}
if (typeof config === 'number') {
return Joi.number().valid(config);
}
if (typeof config === 'boolean') {
return Joi.boolean().valid(config);
}
Hoek.assert(config === null, 'Invalid schema content:', config);
return Joi.valid(null);
};
internals.appendPath = function (Joi, config) {
try {
return exports.schema(Joi, config);
}
catch (err) {
if (err.path !== undefined) {
err.message = `${err.message}${err.message[err.message.length - 1] === ' ' ? '' : ' '}(${err.path})`;
}
throw err;
}
};
exports.ref = function (id, options) {
return Ref.isRef(id) ? id : new Ref(id, options);
};
exports.compile = function (root, schema, options = {}) {
// Compiled by any supported version
const any = schema && schema[Common.symbols.any];
if (any) {
Hoek.assert(options.legacy || any.version === Common.version, 'Cannot mix different versions of joi schemas');
return schema;
}
// Uncompiled root
if (typeof schema !== 'object' ||
!options.legacy) {
return internals.appendPath(root, schema); // Will error if schema contains other versions
}
// Scan schema for compiled parts
const compiler = internals.walk(schema);
if (!compiler) {
return internals.appendPath(root, schema);
}
return compiler.compile(compiler.root, schema);
};
internals.walk = function (schema) {
if (typeof schema !== 'object') {
return null;
}
if (Array.isArray(schema)) {
for (const item of schema) {
const compiler = internals.walk(item);
if (compiler) {
return compiler;
}
}
return null;
}
const any = schema[Common.symbols.any];
if (any) {
return { root: schema[any.root], compile: any.compile };
}
Hoek.assert(Object.getPrototypeOf(schema) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
for (const key in schema) {
const compiler = internals.walk(schema[key]);
if (compiler) {
return compiler;
}
}
return null;
};