/* jshint node: true */

'use strict';

/**
 * Shim to ease transition from the previous JS validator.
 *
 * This API is identical to the previous one except for the following
 * differences:
 *
 * + The validator's `schema` property isn't available.
 * + Error messages might be worded differently.
 * + Namespaced type names are now correctly supported.
 *
 */

var protocols = require('../../lib/protocols'),
    schemas = require('../../lib/schemas'),
    util = require('util');

function Validator(schema, namespace, namedTypes) {
  var opts = {namespace: namespace, registry: namedTypes};
  this._type = schemas.createType(schema, opts);
  this.rawSchema = schema;
}

Validator.prototype.validate = function (obj) {
  return this._type.isValid(obj, {errorHook: errorHook});
};

Validator.validate = function (schema, obj) {
  var validator = new Validator(schema);
  return validator.validate(obj);
};

function ProtocolValidator(protocol) {
  this._protocol = new protocols.Protocol(protocol);
}

ProtocolValidator.prototype.validate = function (typeName, obj) {
  var type = this._protocol.getTypes()[typeName];
  if (!type) {
    throw new Error(util.format('unknown type: %s', typeName));
  }
  return type.isValid(obj, {errorHook: errorHook});
};

ProtocolValidator.validate = function (protocol, typeName, obj) {
  var validator = new ProtocolValidator(protocol);
  return validator.validate(typeName, obj);
};

// Helpers.

/**
 * Hook used to get information about errors.
 *
 * @param path {Array} Components of path to mismatch.
 * @param obj {...} Mismatched value.
 * @param type {Type} Expected type.
 *
 * This hook will throw an error on the first mismatch encounter, consistent
 * with the behavior of the previous validator.
 *
 */
function errorHook(path, obj, type) {
  var name = type.getSchema(true);
  throw new Error(util.format('invalid %s@%s: %j', name, path.join(), obj));
}


module.exports = {
  Validator: Validator,
  ProtocolValidator: ProtocolValidator
};

@@ -302,186 +302,712 @@
 * + 
-Namespaced type names are now correctly supported.%0A *%0A */%0A%0Avar protocols = require('../../lib/protocols'),%0A    schemas = require('../../lib/schemas'),%0A    util = require('util');
+All types are now implemented (e.g. bytes).%0A * + Namespaces are now correctly handled.%0A * + Very large %60long%60s will not pass validation (safer since they might suffer%0A *   precision loss).%0A * + Extra fields in records are ignored (more efficient and leaves more%0A *   flexibility to clients).%0A *%0A * See %60test.js%60 in this folder for more information.%0A *%0A */%0A%0Avar protocols = require('../../lib/protocols'),%0A    schemas = require('../../lib/schemas'),%0A    util = require('util');%0A%0A%0Avar WARNING = 'Validator API is now deprecated in favor of the Type API';%0A%0A/* jshint -W021 */%0AValidator = util.deprecate(Validator, WARNING);%0AProtocolValidator = util.deprecate(ProtocolValidator, WARNING);%0A/* jshint +W021 */%0A
 %0A%0Afu
@@ -1429,16 +1429,17 @@
 j);%0A%7D;%0A%0A
+%0A
 function

Add deprecation warning to shimmed functions.