How to use the tv4.addSchema function in tv4

To help you get started, we’ve selected a few tv4 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sockethub / sockethub / packages / sockethub-schemas / bin / build.js View on Github external
#!/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');
github sockethub / sockethub / src / bootstrap / platforms.js View on Github external
// 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(', ')
        });
      }
    }
  }
github sockethub / sockethub / packages / sockethub-schemas / bin / build.js View on Github external
#!/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');
github Azure / azure-resource-manager-schemas / tools / schemaTestsRunner.ts View on Github external
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();
        }
    }
}
github dapphub / dapple / lib / schemas.js View on Github external
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;
}
github Azure / azure-resource-manager-schemas / tools / validateJSON.js View on Github external
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;
}
github FIWARE / tutorials.TourGuide-App / server / routes / orion.js View on Github external
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 + ' ' +
github openknowledge-archive / datapackage-validate-js / index.js View on Github external
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);
          }
        });
github krakenjs / swaggerize-routes / lib / schema / index.js View on Github external
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;