How to use the tv4.errorCodes 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 pwa-builder / pwabuilder-lib / lib / manifestTools / validationRules / w3cManifestSchema.js View on Github external
var fs = require('fs'),
    path = require('path'),
    tv4 = require('tv4');

var validationConstants = require('../../constants').validation,
    utils = require('../../utils');
    
tv4.addFormat('uri', function (data) {
  if (!utils.isURL(data)) {
    return '\'' + data + '\' is not a valid URL.';
  }
  return null;
});

var errorCodeLookup = {};
for (var key in tv4.errorCodes) {
  errorCodeLookup[tv4.errorCodes[key]] = key.toLowerCase();
}

module.exports = function (manifestContent, callback) {
  var schemaFile = path.resolve(__dirname, '..', 'assets', 'web-manifest.json');
  var schema = JSON.parse(fs.readFileSync(schemaFile).toString());
  
  var extendedSchemaFile = path.resolve(__dirname, '..', 'assets', 'web-manifest-extended.json');
  var extendedSchema = JSON.parse(fs.readFileSync(extendedSchemaFile).toString());
  
  // merge the extended schema with the standard schema 
  for (var property in extendedSchema.properties) { schema.properties[property] = extendedSchema.properties[property]; }
  for (var definition in extendedSchema.definitions) { schema.definitions[definition] = extendedSchema.definitions[definition]; }
  
  var schemaValidation = tv4.validateMultiple(manifestContent, schema, true, true);
  var validationResults = [];
github pwa-builder / PWABuilder-CLI / manifoldjs-lib / src / manifestTools / validationRules / w3cManifestSchema.js View on Github external
path = require('path'),
    tv4 = require('tv4');

var validationConstants = require('../../constants').validation,
    utils = require('../../utils');
    
tv4.addFormat('uri', function (data) {
  if (!utils.isURL(data)) {
    return '\'' + data + '\' is not a valid URL.';
  }
  return null;
});

var errorCodeLookup = {};
for (var key in tv4.errorCodes) {
  errorCodeLookup[tv4.errorCodes[key]] = key.toLowerCase();
}

module.exports = function (manifestContent, callback) {
  var schemaFile = path.resolve(__dirname, '..', 'assets', 'web-manifest.json');
  var schema = JSON.parse(fs.readFileSync(schemaFile).toString());
  
  var extendedSchemaFile = path.resolve(__dirname, '..', 'assets', 'web-manifest-extended.json');
  var extendedSchema = JSON.parse(fs.readFileSync(extendedSchemaFile).toString());
  
  // merge the extended schema with the standard schema 
  for (var property in extendedSchema.properties) { schema.properties[property] = extendedSchema.properties[property]; }
  for (var definition in extendedSchema.definitions) { schema.definitions[definition] = extendedSchema.definitions[definition]; }
  
  var schemaValidation = tv4.validateMultiple(manifestContent, schema, true, true);
  var validationResults = [];
  schemaValidation.errors.forEach(function (err) {
github pwa-builder / PWABuilder-CLI / manifoldjs-lib / src / manifestTools / validationRules / w3cManifestSchema.js View on Github external
var fs = require('fs'),
    path = require('path'),
    tv4 = require('tv4');

var validationConstants = require('../../constants').validation,
    utils = require('../../utils');
    
tv4.addFormat('uri', function (data) {
  if (!utils.isURL(data)) {
    return '\'' + data + '\' is not a valid URL.';
  }
  return null;
});

var errorCodeLookup = {};
for (var key in tv4.errorCodes) {
  errorCodeLookup[tv4.errorCodes[key]] = key.toLowerCase();
}

module.exports = function (manifestContent, callback) {
  var schemaFile = path.resolve(__dirname, '..', 'assets', 'web-manifest.json');
  var schema = JSON.parse(fs.readFileSync(schemaFile).toString());
  
  var extendedSchemaFile = path.resolve(__dirname, '..', 'assets', 'web-manifest-extended.json');
  var extendedSchema = JSON.parse(fs.readFileSync(extendedSchemaFile).toString());
  
  // merge the extended schema with the standard schema 
  for (var property in extendedSchema.properties) { schema.properties[property] = extendedSchema.properties[property]; }
  for (var definition in extendedSchema.definitions) { schema.definitions[definition] = extendedSchema.definitions[definition]; }
  
  var schemaValidation = tv4.validateMultiple(manifestContent, schema, true, true);
  var validationResults = [];
github pwa-builder / pwabuilder-lib / lib / manifestTools / validationRules / w3cManifestSchema.js View on Github external
schemaValidation.errors.forEach(function (err) {
    var message = err.message;
    if (err.subErrors) {
      message = err.subErrors.reduce(function (previous, current) {
        return previous.message + (previous ? ' ' : '') + current.message + '.';
      });
    }
      
    var member = err.dataPath.split('/').pop();
    if (err.code !== tv4.errorCodes.UNKNOWN_PROPERTY || (member && member.indexOf('_') < 0)) {
      validationResults.push({
        'description': message,
        'platform': validationConstants.platforms.all,
        'level': (err.code === tv4.errorCodes.UNKNOWN_PROPERTY) ? validationConstants.levels.warning : validationConstants.levels.error,
        'member': err.dataPath,
        'code': 'w3c-schema-' + errorCodeLookup[err.code]
      });
    }
  });
github obniz / obniz / obniz / libs / wscommand / WSSchema.js View on Github external
tv4.defineKeyword('uniqueKeys', function(data, value, schema) {
  if (!Array.isArray(value)) {
    return null;
  }
  let targets = [];
  for (let key of value) {
    if (data[key] !== null && data[key] !== undefined) {
      targets.push(data[key]);
    }
  }
  let duplicated = targets.filter(function(x, i, self) {
    return self.indexOf(x) !== self.lastIndexOf(x);
  });
  if (duplicated.length > 0) {
    return {
      code: tv4.errorCodes.UNIQUE_KEYS,
      message: { uniqueKeys: value.join(',') },
    };
  }
  return null;
});