{"size": 1191, "ext": "js", "max_stars_repo_path": "api/lib/schema-validator.js", "max_stars_repo_name": "geertweening/ripple-rest", "max_stars_repo_stars_event_min_datetime": "2015-01-06T18:24:42.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-17T00:20:41.000Z", "max_issues_repo_path": "api/lib/schema-validator.js", "max_issues_repo_name": "nick-nie/ripple-rest", "max_issues_repo_issues_event_min_datetime": "2015-01-02T15:56:47.000Z", "max_issues_repo_issues_event_max_datetime": "2017-06-29T07:33:50.000Z", "max_forks_repo_path": "api/lib/schema-validator.js", "max_forks_repo_name": "nick-nie/ripple-rest", "max_forks_repo_forks_event_min_datetime": "2015-01-02T18:36:29.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-01T23:18:27.000Z"}
{"max_stars_count": 66, "max_issues_count": 150, "max_forks_count": 71, "avg_line_length": 25.8913043478, "max_line_length": 79, "alphanum_fraction": 0.6557514694}
'use strict';
var fs = require('fs');
var path = require('path');
var JaySchema = require('jayschema');
var formatJaySchemaErrors = require('jayschema-error-messages');

var baseDir = path.join(__dirname, './schemas');

module.exports = (function() {
  var validator = new JaySchema();
  var validate = validator.validate;

  // If schema is valid, return true. Otherwise
  // return array of validation errors
  validator.validate = function() {
    var errors = validate.apply(validator, arguments);
    return {
      err: errors,
      errors: formatJaySchemaErrors(errors),
      isValid: errors.length === 0
    };
  };

  validator.isValid = function() {
    return validator.validate.apply(validator, arguments).isValid;
  };

  // Load Schemas
  fs.readdirSync(baseDir).filter(function(fileName) {
    return /^[\w\s]+\.json$/.test(fileName);
  })
  .map(function(fileName) {
    try {
      return JSON.parse(fs.readFileSync(path.join(baseDir, fileName), 'utf8'));
    } catch (e) {
      throw new Error('Failed to parse schema: ' + fileName);
    }
  })
  .forEach(function(schema) {
    schema.id = schema.title;
    validator.register(schema);
  });

  return validator;
})();
