Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function downloadExternalDeploymentTemplateSchemas(initialDownloadUris, failedSchemaUris, done) {
if (_.isFunction(failedSchemaUris)) {
done = failedSchemaUris;
failedSchemaUris = [];
}
var urlsToDownload = initialDownloadUris.concat(_.difference(tv4.getMissingUris(), failedSchemaUris));
if (urlsToDownload.length === 0) {
return done();
}
async.each(urlsToDownload, function (uri, next) {
request.get(uri, function (err, response, body) {
// If download fails, just skip, don't fail everything
// Will result in less validation, but should be fine otherwise
if (!err && response.statusCode === 200) {
tv4.addSchema(uri, JSON.parse(body.trim()));
} else {
failedSchemaUris.push(uri);
}
next();
});
}, function () {
async function addMissingSchemas(missingUris: string[], loadSchema: (uri: string) => Promise) {
while (missingUris && missingUris.length > 0) {
var missingUri = missingUris.pop();
if (missingUri && missingUri.length > 0) {
const schema = await loadSchema(missingUri);
tv4.addSchema(missingUri, schema);
missingUris = tv4.getMissingUris();
}
}
}
function validate(json, schema, schemaFolderPath) {
let result;
if (json === null || json === undefined) {
logError(getErrorMessage("Cannot validate a", json, "json object."));
result = { valid: false, errors: [{ message: "Invalid JSON" }], missingSchemas: [] };
}
else if (!schema) {
logError(getErrorMessage("Cannot use a", schema, "schema for validation."));
result = { valid: false, errors: [{ message: "Invalid schema" }], missingSchemas: [] };
}
else {
tv4.addSchema(json);
tv4.addSchema(schema);
addMissingSchemas(tv4.getMissingUris(), schemaFolderPath);
result = convertTv4ValidationResult(tv4.validateMultiple(json, schema));
while (result.missingSchemas && result.missingSchemas.length > 0) {
addMissingSchemas(result.missingSchemas, schemaFolderPath);
result = convertTv4ValidationResult(tv4.validateMultiple(json, schema));
}
}
return result;
}
async function validate(json: any, schema: any, loadSchema: (uri: string) => Promise): Promise {
if (json === null || json === undefined) {
logError(getErrorMessage("Cannot validate a", json, "json object."));
return { valid: false, errors: [{ message: "Invalid JSON", dataPath: '', subErrors: [], schemaPath: '' }], missingSchemas: [] };
}
if (!schema) {
logError(getErrorMessage("Cannot use a", schema, "schema for validation."));
return { valid: false, errors: [{ message: "Invalid schema", dataPath: '', subErrors: [], schemaPath: '' }], missingSchemas: [] };
}
tv4.addSchema(json);
tv4.addSchema(schema);
await addMissingSchemas(tv4.getMissingUris(), loadSchema);
let result = convertTv4ValidationResult(tv4.validateMultiple(json, schema));
while (result.missingSchemas && result.missingSchemas.length > 0) {
await addMissingSchemas(result.missingSchemas, loadSchema);
result = convertTv4ValidationResult(tv4.validateMultiple(json, schema));
}
return result;
}
function addMissingSchemas(missingUris, schemaFolderPath) {
while (missingUris && missingUris.length > 0) {
var missingUri = missingUris.pop();
if (missingUri && missingUri.length > 0) {
tv4.addSchema(missingUri, utilities.readJSONPath(missingUri, schemaFolderPath));
missingUris = tv4.getMissingUris();
}
}
}