Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (parent.required === undefined) {
parent.required = [];
}
if (parent.optional === undefined) {
parent.optional = [];
}
if (Hoek.reach(joiObj, '_flags.presence') === 'required') {
parent.required.push(name);
}
if (Hoek.reach(joiObj, '_flags.presence') === 'optional') {
parent.optional.push(name);
}
}
}
property.default = Hoek.reach(joiObj, '_flags.default');
// allow for function calls
if (Utilities.isFunction(property.default)) {
property.default = property.default();
}
return property;
};
}
}
// add required/optional state only if present
if (parent && name) {
if (Hoek.reach(joiObj, '_flags.presence')) {
if (parent.required === undefined) {
parent.required = [];
}
if (parent.optional === undefined) {
parent.optional = [];
}
if (Hoek.reach(joiObj, '_flags.presence') === 'required') {
parent.required.push(name);
}
if (Hoek.reach(joiObj, '_flags.presence') === 'optional') {
parent.optional.push(name);
}
}
}
property.default = Hoek.reach(joiObj, '_flags.default');
// allow for function calls
if (Utilities.isFunction(property.default)) {
property.default = property.default();
}
return property;
};
};
Utilities.assignVendorExtensions(routeData, routeOptions);
routeData.path = Utilities.replaceInPath(routeData.path, ['endpoints'], this.settings.pathReplacements);
// user configured interface through route plugin options
if (Hoek.reach(routeOptions, 'validate.query')) {
routeData.queryParams = Utilities.toJoiObject(Hoek.reach(routeOptions, 'validate.query'));
}
if (Hoek.reach(routeOptions, 'validate.params')) {
routeData.pathParams = Utilities.toJoiObject(Hoek.reach(routeOptions, 'validate.params'));
}
if (Hoek.reach(routeOptions, 'validate.headers')) {
routeData.headerParams = Utilities.toJoiObject(Hoek.reach(routeOptions, 'validate.headers'));
}
if (Hoek.reach(routeOptions, 'validate.payload')) {
// has different structure, just pass straight through
routeData.payloadParams = Hoek.reach(routeOptions, 'validate.payload');
// if its a native javascript object convert it to JOI
if (!Utilities.isJoi(routeData.payloadParams)) {
routeData.payloadParams = Joi.object(routeData.payloadParams);
}
}
// swap out any custom validation function for Joi object/string
['queryParams', 'pathParams', 'headerParams', 'payloadParams'].forEach(function(property) {
// swap out any custom validation function for Joi object/string
if (Utilities.isFunction(routeData[property])) {
if (property !== 'pathParams') {
self.settings.log(
['validation', 'warning'],
'Using a Joi.function for a query, header or payload is not supported.'
routes.forEach(route => {
let routeOptions = Hoek.reach(route, 'settings.plugins.hapi-swagger') || {};
let routeData = {
path: route.path,
method: route.method.toUpperCase(),
description: route.settings.description,
notes: route.settings.notes,
tags: Hoek.reach(route, 'settings.tags'),
queryParams: Hoek.reach(route, 'settings.validate.query'),
pathParams: Hoek.reach(route, 'settings.validate.params'),
payloadParams: Hoek.reach(route, 'settings.validate.payload'),
responseSchema: Hoek.reach(route, 'settings.response.schema'),
responseStatus: Hoek.reach(route, 'settings.response.status'),
headerParams: Hoek.reach(route, 'settings.validate.headers'),
consumes: Hoek.reach(routeOptions, 'consumes') || null,
produces: Hoek.reach(routeOptions, 'produces') || null,
responses: Hoek.reach(routeOptions, 'responses') || null,
payloadType: Hoek.reach(routeOptions, 'payloadType') || null,
security: Hoek.reach(routeOptions, 'security') || null,
order: Hoek.reach(routeOptions, 'order') || null,
deprecated: Hoek.reach(routeOptions, 'deprecated') || null,
id: Hoek.reach(routeOptions, 'id') || null,
groups: route.group
};
Utilities.assignVendorExtensions(routeData, routeOptions);
routeData.path = Utilities.replaceInPath(routeData.path, ['endpoints'], this.settings.pathReplacements);
// user configured interface through route plugin options
if (Hoek.reach(routeOptions, 'validate.query')) {
routeData.queryParams = Utilities.toJoiObject(Hoek.reach(routeOptions, 'validate.query'));
}
}
else if (childType === 'object') {
target[childKey] = {};
schemaMapper(target[childKey], childSchemaRaw);
}
else if (childType === 'alternatives') {
const hasTrueCase = childSchemaDescription.alternatives.filter((option) => {
return option.ref && option.then;
}).length > 0;
const chosenAlternative = hasTrueCase ?
childSchemaDescription.alternatives[0].then :
childSchemaDescription.alternatives[0];
if (chosenAlternative.type === 'object' && Hoek.reach(chosenAlternative, 'flags.func')) {
target[childKey] = Hoek.clone(internals.joiDictionary.func);
}
else if (chosenAlternative.type === 'object') {
target[childKey] = {};
schemaMapper(target[childKey], Helpers.descriptionCompiler(chosenAlternative));
}
else {
const alternativeHasDefault = Hoek.reach(chosenAlternative, 'flags.default');
target[childKey] = (alternativeHasDefault && !ignoreDefaults) ?
Helpers.getDefault(chosenAlternative) :
Hoek.clone(internals.joiDictionary[chosenAlternative.type]);
}
}
else {
const childHasDefault = Hoek.reach(childSchemaDescription, 'flags.default');
target[childKey] = (childHasDefault && !ignoreDefaults) ?
routes.forEach(route => {
let routeOptions = Hoek.reach(route, 'settings.plugins.hapi-swagger') || {};
let routeData = {
path: route.path,
method: route.method.toUpperCase(),
description: route.settings.description,
notes: route.settings.notes,
tags: Hoek.reach(route, 'settings.tags'),
queryParams: Hoek.reach(route, 'settings.validate.query'),
pathParams: Hoek.reach(route, 'settings.validate.params'),
payloadParams: Hoek.reach(route, 'settings.validate.payload'),
responseSchema: Hoek.reach(route, 'settings.response.schema'),
responseStatus: Hoek.reach(route, 'settings.response.status'),
headerParams: Hoek.reach(route, 'settings.validate.headers'),
consumes: Hoek.reach(routeOptions, 'consumes') || null,
produces: Hoek.reach(routeOptions, 'produces') || null,
responses: Hoek.reach(routeOptions, 'responses') || null,
payloadType: Hoek.reach(routeOptions, 'payloadType') || null,
security: Hoek.reach(routeOptions, 'security') || null,
order: Hoek.reach(routeOptions, 'order') || null,
deprecated: Hoek.reach(routeOptions, 'deprecated') || null,
id: Hoek.reach(routeOptions, 'id') || null,
groups: route.group
};
Utilities.assignVendorExtensions(routeData, routeOptions);
if (chosenAlternative.type === 'object' && Hoek.reach(chosenAlternative, 'flags.func')) {
target[childKey] = Hoek.clone(internals.joiDictionary.func);
}
else if (chosenAlternative.type === 'object') {
target[childKey] = {};
schemaMapper(target[childKey], Helpers.descriptionCompiler(chosenAlternative));
}
else {
const alternativeHasDefault = Hoek.reach(chosenAlternative, 'flags.default');
target[childKey] = (alternativeHasDefault && !ignoreDefaults) ?
Helpers.getDefault(chosenAlternative) :
Hoek.clone(internals.joiDictionary[chosenAlternative.type]);
}
}
else {
const childHasDefault = Hoek.reach(childSchemaDescription, 'flags.default');
target[childKey] = (childHasDefault && !ignoreDefaults) ?
Helpers.getDefault(childSchemaDescription) :
Hoek.clone(internals.joiDictionary[childType]);
}
});
}
internals.extractConfig = function (request) {
return Object.assign({}, Hoek.reach(request, 'route.settings.plugins.good'), Hoek.reach(request, 'plugins.good'));
};
if (!arrayIsSparse) {
if (schemaDescription.orderedItems) {
for (let i = 0; i < schemaDescription.orderedItems.length; ++i) {
const itemRawSchema = Hoek.reach(this._schema, '_inner.ordereds')[i];
const itemType = internals.getType(itemRawSchema);
const Item = new Examples[itemType](itemRawSchema, childOptions);
arrayResult.push(Item.generate());
}
}
if (schemaDescription.items) {
for (let i = 0; i < schemaDescription.items.length; ++i) {
const itemIsForbidden = schemaDescription.items[i].flags && schemaDescription.items[i].flags.presence === 'forbidden';
if (!itemIsForbidden) {
const itemRawSchema = Hoek.reach(this._schema, '_inner.items')[i];
const itemType = internals.getType(itemRawSchema);
const Item = new Examples[itemType](itemRawSchema, childOptions);
arrayResult.push(Item.generate());
}
}
}
const itemsToAdd = schemaDescription.items ? schemaDescription.items : [
{
type: 'string'
},
{
type: 'number'
}
];
childrenKeys.forEach((childKey) => {
const childSchemaDescription = schemaDescription.children[childKey];
const flagsPresence = Hoek.reach(childSchemaDescription, 'flags.presence');
const flagsStrip = Hoek.reach(childSchemaDescription, 'flags.strip');
const childIsRequired = flagsPresence === 'required';
const childIsOptional = (flagsPresence === 'optional') || (parentPresence === 'optional' && !childIsRequired);
const childIsForbidden = flagsPresence === 'forbidden' || flagsStrip;
if (childIsForbidden || (childIsOptional && !(Hoek.reach(config, 'includeOptional')))) {
return;
}
const childType = childSchemaDescription.type;
const childSchemaRaw = schema._inner.children.filter((child) => {
return child.key === childKey;
})[0].schema;
if (childType === 'object' && Hoek.reach(childSchemaDescription, 'flags.func')) {