Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
lib/test: update code for mgl-validate v2.x+
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Blasnik committed May 4, 2016
1 parent cb955d5 commit 79cf6cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 26 additions & 9 deletions lib/plugins/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ function ValidatePlugin(rail, options) {
breakOnError: options.breakOnError === false ? false : true
});

var i, err;
var i;

if (options.schemas) {
for (i = 0; i < options.schemas.length; ++i) {
err = this.registry.addSchema(options.schemas[i]);

if (err) {
throw err;
}
this.registry.addSchema(options.schemas[i]);
}
}

Expand Down Expand Up @@ -105,24 +101,45 @@ ValidatePlugin.prototype._interceptResponse = function(call, options, response)


ValidatePlugin.prototype._validate = function(schema, data) {
var err, id;
var result, err, id;

if (typeof schema === 'string') {
return this.registry.test(schema, data);

} else if (typeof schema === 'object') {
id = schema.id;

err = this.registry.addSchema(schema);
err = this._addSchema(schema);

if (err) {
return err;
}

err = this.registry.test(id, data);
result = this.registry.test(id, data);

if (result) {
err = new Error('Validation failed');
err.validation = JSON.stringify(result);
err.validation = result;
}

// technically this could throw
this.registry.removeSchema(id);

return err;

} else {
return new Error('Invalid schema');
}
};


ValidatePlugin.prototype._addSchema = function(schema) {
try {
this.registry.addSchema(schema);
} catch (e) {
return e;
}

return;
};
2 changes: 1 addition & 1 deletion test/test-http-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ suite('http:validate', function() {
}
}).on('error', function(err) {
assert(err);
assert.strictEqual(err.message, 'Invalid schema id');
assert.strictEqual(err.message, 'Missing schema id');
done();
}).end();
});
Expand Down

0 comments on commit 79cf6cc

Please sign in to comment.