65 lines
1.2 KiB
JavaScript
65 lines
1.2 KiB
JavaScript
'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 = {};
|
|
|
|
|
|
exports.schema = function (Joi, config) {
|
|
|
|
if (config !== undefined && config !== null && typeof config === 'object') {
|
|
|
|
if (config.isJoi) {
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
if (Ref.isRef(config)) {
|
|
return Joi.valid(config);
|
|
}
|
|
|
|
Hoek.assert(config === null, 'Invalid schema content:', config);
|
|
|
|
return Joi.valid(null);
|
|
};
|
|
|
|
|
|
exports.ref = function (id) {
|
|
|
|
return Ref.isRef(id) ? id : Ref.create(id);
|
|
};
|