Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env node
var tv4 = require('tv4'),
debug = require('debug')('sockethub:schemas'),
fs = require('fs');
var base = process.env.PWD;
var schemaSHAS = require('../src/sockethub-activity-stream');
var schemaSHAO = require('../src/sockethub-activity-object');
// load sockethub-activity-stream schema and register it with tv4
tv4.addSchema(schemaSHAS.id, schemaSHAS);
// load sockethub-activity-object schema and register it with tv4
tv4.addSchema(schemaSHAO.id, schemaSHAO);
var fd = fs.openSync(base + '/schemas/activity-stream.js', 'w+');
fs.writeSync(fd, 'module.exports = ' + JSON.stringify(schemaSHAS, null, "\t") + ';');
fd = fs.openSync(base + '/schemas/activity-stream.json', 'w+');
fs.writeSync(fd, JSON.stringify(schemaSHAS, null, "\t"));
fd = fs.openSync(base + '/schemas/activity-object.js', 'w+');
fs.writeSync(fd, 'module.exports = ' + JSON.stringify(schemaSHAO, null, "\t") + ';');
fd = fs.openSync(base + '/schemas/activity-object.json', 'w+');
fs.writeSync(fd, JSON.stringify(schemaSHAO, null, "\t"));
debug('updated sockethub activity stream schemas');
// validate schema property
if (! tv4.validate(p.schema, schemas.platform)) {
throw new Error(
`${platformName} platform schema failed to validate: ${tv4.error.message}`);
} else if (typeof p.config !== 'object') {
throw new Error(
`${platformName} platform must have a config property that is an object.`);
} else if (p.schema.credentials) {
// register the platforms credentials schema
types.push('credentials');
tv4.addSchema(`http://sockethub.org/schemas/v0/context/${platformName}/credentials`,
p.schema.credentials);
}
tv4.addSchema(`http://sockethub.org/schemas/v0/context/${platformName}/messages`,
p.schema.messages);
if (platformListsSupportedTypes(p)) {
types = [...types, ...p.schema.messages.properties['@type'].enum];
}
platforms.set(platformName, {
id: platformName,
moduleName: moduleName,
version: packageJson.version,
'@types': types.join(', ')
});
}
}
}
#!/usr/bin/env node
var tv4 = require('tv4'),
debug = require('debug')('sockethub:schemas'),
fs = require('fs');
var base = process.env.PWD;
var schemaSHAS = require('../src/sockethub-activity-stream');
var schemaSHAO = require('../src/sockethub-activity-object');
// load sockethub-activity-stream schema and register it with tv4
tv4.addSchema(schemaSHAS.id, schemaSHAS);
// load sockethub-activity-object schema and register it with tv4
tv4.addSchema(schemaSHAO.id, schemaSHAO);
var fd = fs.openSync(base + '/schemas/activity-stream.js', 'w+');
fs.writeSync(fd, 'module.exports = ' + JSON.stringify(schemaSHAS, null, "\t") + ';');
fd = fs.openSync(base + '/schemas/activity-stream.json', 'w+');
fs.writeSync(fd, JSON.stringify(schemaSHAS, null, "\t"));
fd = fs.openSync(base + '/schemas/activity-object.js', 'w+');
fs.writeSync(fd, 'module.exports = ' + JSON.stringify(schemaSHAO, null, "\t") + ';');
fd = fs.openSync(base + '/schemas/activity-object.json', 'w+');
fs.writeSync(fd, JSON.stringify(schemaSHAO, null, "\t"));
debug('updated sockethub activity stream schemas');
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 buildSchemas () {
var schemas = {};
var specs = _.filter(fs.readdirSync(path.join(__dirname, '..', 'specs')),
(p) => p.endsWith('.schema.json'));
for (let i = 0; i < specs.length; i += 1) {
let spec = specs[i];
let schema = require(path.join('..', 'specs', spec));
let schemaName = spec.slice(0, -12);
schemas[schemaName] = new Schema(schema);
tv4.addSchema(schemaName, schema);
}
return schemas;
}
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;
}
var authRequest = require('../auth/authrequest');
var utils = require('../utils');
var geocoder = require('node-geocoder')('google', 'http');
var async = require('async');
var auth = require('../auth/auth');
var tv4 = require('tv4');
var schema = require('../schema/schema');
var config = require('../config');
var fiwareHeaders = {
'fiware-service': config.fiwareService
};
tv4.addSchema('restaurant', schema.restaurant);
tv4.addSchema('reservation', schema.reservation);
tv4.addSchema('review', schema.review);
/**
* Restaurants
*/
/**
* Function used to POST at /api/orion/restaurant and generate a Restaurant
*
* @param {Object} req - Request received
* @param {Object} res - Response
*/
exports.createRestaurant = function(req, res) {
var elementToOrion = req.body;
var validSchema = tv4.validate(elementToOrion, 'restaurant');
if (validSchema) {
var address = elementToOrion.address.streetAddress + ' ' +
request(profile.schema, function(E, R, B) {
if(E) {
RJ('Failed loading schema from ' + profile.schema);
return null;
}
try {
tv4.addSchema("definitions.json", JSON.parse(body));
RS(tv4.validateMultiple(json, JSON.parse(B)));
} catch(E) {
RJ('Failed parsing schema json from ' + profile.schema);
}
});
var tv4 = require('tv4'),
assert = require('assert'),
fs = require('fs'),
thing = require('core-util-is'),
path = require('path');
var schemaPath, baseSchemaPath, baseSchema;
schemaPath = path.join(__dirname, 'swagger-spec/schemas/v2.0');
baseSchemaPath = path.join(schemaPath, 'schema.json');
assert.ok(fs.existsSync(schemaPath));
assert.ok(fs.existsSync(baseSchemaPath));
baseSchema = require(baseSchemaPath);
tv4.addSchema(baseSchema);
module.exports = {
/**
* Validate against an optional schema, defaulting to base api schema.
* @param data
* @param schema
* @returns {*}
*/
validate: function validate(data, schema) {
var results;
schema && assert.ok(thing.isObject(schema));
results = tv4.validateResult(data, schema || baseSchema, true);
return results;