Permalink
Browse files

Rewrite the ‘addValidator’ test suite into Mocha

  • Loading branch information...
1 parent ba55cc0 commit 4366cce2ac39aa3a3840bf609cd72e0d222e1931 @Baggz committed Mar 7, 2012
Showing with 100 additions and 0 deletions.
  1. +100 −0 tests/json/addValidator.js
@@ -0,0 +1,100 @@
+if (typeof module !== 'undefined' && module.exports) {
+ var expect = require('expect.js');
+ var Amanda = require('../../dist/latest.js');
+}
+
+/**
+ * AddValidator
+ * --------------------
+ */
+suite('JSON/addValidator', function() {
+
+ var count = 0;
+
+ /**
+ * Validator
+ */
+ var Validator = new Amanda('json');
+
+ /**
+ * MyValidator
+ */
+ Validator.addValidator('myValidator', function(property, propertyValue, validator, propertyValidators, callback) {
+
+ var usernames = [
+ 'Baggz',
+ 'Amanda',
+ 'Administrator',
+ 'Admin'
+ ];
+
+ if (validator && usernames.indexOf(propertyValue) !== -1) {
+ return callback('Oops! This username - ' + propertyValue + ' - is taken.');
+ } else {
+ return callback();
+ }
+
+ });
+
+ /**
+ * Schema
+ */
+ var schema = {
+ reqired: true,
+ myValidator: true
+ };
+
+ /**
+ * Schema2
+ */
+ var schema2 = {
+ type: 'object',
+ properties: {
+ name: {
+ required: true,
+ type: 'string',
+ format: 'alphanumeric',
+ myValidator: true
+ },
+ username: {
+ required: true,
+ type: 'string',
+ format: 'alphanumeric',
+ }
+ }
+ };
+
+ test('should return an error', function() {
+ [
+ 'Baggz',
+ 'Amanda',
+ 'Administrator',
+ 'Admin'
+ ].forEach(function(instance) {
+ Validator.validate(instance, schema, function(error) {
+ expect(error).to.be.ok();
+ });
+ });
+ });
+
+ test('should return an error', function() {
+ [
+ 'Baggz',
+ 'Amanda',
+ 'Administrator',
+ 'Admin'
+ ].forEach(function(input) {
+
+ Validator.validate({
+ name: input,
+ username: 'Test'
+ }, schema2, {
+ singleError: false
+ }, function(error) {
+ expect(error).to.be.ok();
+ });
+
+ });
+ });
+
+});

0 comments on commit 4366cce

Please sign in to comment.