How to use the tv4.freshApi 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 postmanlabs / postman-collection-transformer / tests / schema / example-validity-spec.js View on Github external
it('must be a valid V1 collection: ' + sampleName + '.json', function (done) {
                var validator = tv4.freshApi(),
                    result;
                validator.addSchema(schema);
                // Some of the converter functions assign "undefined" value to some properties,
                // It is necessary to get rid of them (otherwise schema validation sees an "undefined" and fails).
                // Converting to and parsing from JSON does this.
                result = validator.validate(sample, schema);
                if (!result) {
                    console.log(JSON.stringify(validator.error, null, 4)); // Helps debug on CI
                }
                if (validator.missing.length) {
                    console.log(validator.missing);
                    result = false;
                }
                expect(result).to.be(true);
                done();
            });
github postmanlabs / postman-collection-transformer / tests / unit / converter-v2-to-v1-spec.js View on Github external
it('must create a valid V1 collection from ' + sampleName + '.json with synchronous API', function (done) {
                var validator = tv4.freshApi(),
                    result,
                    converted;
                validator.addSchema(schema);
                converted = converter.convert(sample);

                // Some of the converter functions assign "undefined" value to some properties,
                // It is necessary to get rid of them (otherwise schema validation sees an "undefined" and fails).
                // Converting to and parsing from JSON does this.
                converted = JSON.parse(JSON.stringify(converted));

                result = validator.validate(converted, schema);
                if (!result) {
                    console.log(JSON.stringify(validator.error, null, 4)); // Helps debug on CI
                }
                if (validator.missing.length) {
                    console.log(validator.missing);
github json-schema-faker / json-schema-faker / tests / schema / validator.js View on Github external
}

  const errors = validator.getLastErrors();

  if (errors || !valid) {
    fail.push(errors.map(e => {
      if (e.code === 'PARENT_SCHEMA_VALIDATION_FAILED') {
        return e.inner.map(x => `[z-schema] ${x.message}`).join('\n');
      }

      return `[z-schema] ${e.message}`;
    }).join('\n') || `[z-schema] Invalid schema ${JSON.stringify(sample)}`);
  }

  // tv4
  const api = tv4.freshApi();

  api.banUnknown = false;
  api.cyclicCheck = false;

  Object.keys(fixed).forEach(k => {
    api.addSchema(k, fixed[k]);
  });

  const result = api.validateResult(sample, clone(schema), api.cyclicCheck, api.banUnknown);

  if (result.missing.length) {
    fail.push(`[tv4] Missing ${result.missing.join(', ')}`);
  }

  if (result.error) {
    fail.push(`[tv4] ${result.error}`);
github noobaa / noobaa-core / src / util / rest_api.js View on Github external
// module targets: nodejs & browserify
'use strict';

var http = require('http');
var https = require('https');
var querystring = require('querystring');
var _ = require('lodash');
var Q = require('q');
var assert = require('assert');
var util = require('util');
var URL = require('url');
var Cookie = require('cookie-jar');
var tv4 = require('tv4').freshApi();
var ice_api = require('./ice_api');
var ice_lib = require('./ice_lib');
var buf = require('./buffer_utils');
var dbg = require('noobaa-util/debug_module')(__filename);
var config = require('../../config.js');

dbg.set_level(config.dbg_log_level);

// TODO temporary impl for dbg.error until we add it to debug_module
if (!dbg.error) {
    dbg.error = function() {
        var args = _.toArray(arguments);
        args.shift('ERROR:');
        dbg.log0.apply(dbg, args);
    };
}
github timbeadle / grunt-tv4 / tasks / tv4.js View on Github external
/*
 * grunt-tv4
 * https://github.com/timbeadle/grunt-tv4
 *
 * Copyright (c) 2013 Bart van der Schoor
 * Licensed under the MIT license.
 */

'use strict';

var taskTv4 = require('tv4').freshApi();
var reporter = require('tv4-reporter');
var ministyle = require('ministyle');
var miniwrite = require('miniwrite');
// eslint-disable-next-line node/no-unpublished-require
var loader = require('../lib/loader');
// eslint-disable-next-line node/no-unpublished-require
var runnerModule = require('../lib/runner');

//var util = require('util');

module.exports = function (grunt) {

	var out = miniwrite.grunt(grunt);
	var style = ministyle.grunt();
	var report = reporter.getReporter(out, style);
	var runner = runnerModule.getRunner(taskTv4, loader.getLoaders(), out, style);
github tobie / specref / test / biblio.js View on Github external
test("Validate References with JSON schema.", function() {
        var validator = tv4.freshApi();
        validator.addFormat(formats);
        var result = validator.validateResult(json, require("../schemas/raw-reference.json"));
        assert(result.valid, schemaMsg(result.error, json));
    });
});
github OpenTMI / opentmi / app / tools / validators.js View on Github external
return (value) => {
    const validator = tv4.freshApi();
    const input = {};
    input[key] = value;
    const result = validator.validateResult(input, schema);
    return result.valid;
  };
}
github manifoldco / torus-cli / lib / util / schema.js View on Github external
'use strict';

var path = require('path');
var tv4 = require('tv4');

var validation = require('./validation');

const schema = exports;

var validator = tv4.freshApi();
validator.addFormat({
  slug: function (input) {
    if (validation.slug(input)) {
      return null;
    }

    return 'Invalid slug';
  }
});

schema.validator = validator;

schema._cache = {};

schema._getPath = function (schemaPath) {
  return path.resolve(path.join(__dirname, '../../schema', schemaPath+'.json'));
github adrai / node-cqrs-domain / lib / validator.js View on Github external
function getValidator (options, schema) {
  options = options || {};
  options.schemas = options.schemas || {};
  options.formats = options.formats || {};

  if (!schema || !_.isObject(schema)) {
    var err = new Error('Please pass a valid schema!');
    debug(err);
    throw err;
  }

  var tv4 = tv4Module.freshApi();

  _.each(options.schemas, function (v, k) {
    tv4.addSchema(k, v);
  });

  tv4.addFormat(options.formats);

  return function (data) {
    var validation = tv4.validateMultiple(data, schema);

    if (validation.missing.length > 0) {
      var missingString = validation.missing[0];

      for (var m = 1, lenM = validation.missing.length; m < lenM; m++) {
        missingString += ', ' + validation.missing[m];
      }
github nickclaw / vlad / src / vlad.js View on Github external
var util = require('./util'),
    error = require('./errors'),
    validator = require('tv4').freshApi(),
    property = require('./property'),
    Property = property.Property,
    each = require('lodash/each');

module.exports = vlad;
module.exports.sync = vlad;
module.exports.promise = promiseWrapper;
module.exports.callback = callbackWrapper;
module.exports.middleware = middlewareWrapper;

/**
 * vlad validator factory
 * Returns a function that can be used to validate objects
 * @param {Object} schema
 * @return {Function}
 */