Class: ClosureCompiler

ClosureCompiler

new ClosureCompiler(){ClosureCompiler}

The ClosureCompiler class that wraps the Google closure compiler

Returns:
ClosureCompiler instance.

Methods

compile(ast, options, callback)

compile a AST into a validator function

Name Type Description
ast object

AST of validation nodes

options object

compiler options

callback function

returns the validation function

Example
// A simple example of compiling a schema

assert = require('assert'),
  StringNode = require('vitesse').StringNode,
  Compiler = require('vitesse').Compiler,
  ObjectNode = require('vitesse').ObjectNode,
var string = new StringNode(null, null, {typeCheck:true});
var schema = new ObjectNode(null, null, {typeCheck:true})
  .addChild('brand', string);

var compiler = new ClosureCompiler({});
// Compile the AST
compiler.compile(schema, function(err, func) {
  assert.equal(null, err);
  // Validate {}
  var results = func.validate([]);
  assert.equal(1, results.length);
  assert.equal('field is not an object', results[0].message);
  assert.deepEqual(['object'], results[0].path);
  assert.ok(results[0].rule === schema);

  // Validate {ca:1}
  var results = func.validate({brand:100});
  assert.equal(1, results.length);
  assert.equal('field is not a string', results[0].message);
  assert.deepEqual(['object', 'brand'], results[0].path);
  assert.ok(results[0].rule === string);

  // Validate {ca:'test'}
  var results = func.validate({brand:'test'});
  assert.equal(0, results.length);
});
comments powered by Disqus