Skip to content

Commit

Permalink
README.md changes, and options merging
Browse files Browse the repository at this point in the history
  • Loading branch information
pocesar committed Apr 24, 2015
1 parent c0d244d commit 3aac2b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 26 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![Build Status](https://travis-ci.org/pocesar/angular-async-validator.svg?branch=master)](https://travis-ci.org/pocesar/angular-async-validator) [![Coverage Status](https://coveralls.io/repos/pocesar/angular-async-validator/badge.svg)](https://coveralls.io/r/pocesar/angular-async-validator)

[![NPM](https://nodei.co/npm/angular-async-validator.png)](https://nodei.co/npm/angular-async-validator/)

Angular Async Validator
=====

Expand All @@ -10,7 +12,11 @@ Provides no validation functions out-of-the-box. You may reuse the ones from Ang

Code was based off [ui-validate](http://angular-ui.github.io/ui-utils/#/validate) initially, but it's too simple and lagging behind still using $parsers and $formatters since it need to retain 1.2 compatibility.

This module requires Angular 1.3+
This module requires Angular 1.3+, and has no dependencies other than Angular itself.

It also supports 3rd party promise libraries such as RSVP, Q, Bluebird, etc.

[DEMO](http://plnkr.co/edit/jIhkAPShgK4ggRYN4N1S?p=preview)

## Motivation

Expand Down
15 changes: 5 additions & 10 deletions angular-async-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ var AsyncValidator;
});
this.run = function (name, value, options, returnValue) {
if (typeof provider.validations[name] === 'undefined' || typeof provider.validations[name].validator !== 'function') {
return $q.reject(name + ' isn\'t a registered async validator');
return $q.reject("" + name + " isn't a registered async validator");
}
options = angular.extend({}, provider.validations[name].options.options, options);
return new $q(function (resolve, reject) {
try {
resolve(provider.validations[name].validator(value, options));
}
catch (e) {
reject(e);
}
}).then(function (result) {
return new $q(function asyncValidatorRunResolver(resolve) {
resolve(provider.validations[name].validator(value, options));
}).then(function asyncValidatorResolved(result) {
if (!!result) {
return returnValue === false ? true : value;
}
return $q.reject();
}, function (e) {
}, function asyncValidatorRejected(e) {
if (provider.validations[name].options.silentRejection) {
return $q.reject(e);
}
Expand Down
15 changes: 5 additions & 10 deletions angular-async-validator.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@ var AsyncValidator;
});
this.run = function (name, value, options, returnValue) {
if (typeof provider.validations[name] === 'undefined' || typeof provider.validations[name].validator !== 'function') {
return $q.reject(name + ' isn\'t a registered async validator');
return $q.reject("" + name + " isn't a registered async validator");
}
options = angular.extend({}, provider.validations[name].options.options, options);
return new $q(function (resolve, reject) {
try {
resolve(provider.validations[name].validator(value, options));
}
catch (e) {
reject(e);
}
}).then(function (result) {
return new $q(function asyncValidatorRunResolver(resolve) {
resolve(provider.validations[name].validator(value, options));
}).then(function asyncValidatorResolved(result) {
if (!!result) {
return returnValue === false ? true : value;
}
return $q.reject();
}, function (e) {
}, function asyncValidatorRejected(e) {
if (provider.validations[name].options.silentRejection) {
return $q.reject(e);
}
Expand Down
69 changes: 64 additions & 5 deletions tests/karma.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe('AsyncValidator', function () {
})
);

afterEach(function(){
if ($exceptionHandler.fn.restore) {
$exceptionHandler.fn.restore();
}
});


function input(inputHtml) {
var
Expand Down Expand Up @@ -112,7 +118,7 @@ describe('AsyncValidator', function () {
return function(){
return $q.reject('ok');
};
}, { silentRejection: false });
});

expect(function(){
Provider.register('dummy3', fn, { overwrite: false });
Expand Down Expand Up @@ -158,8 +164,7 @@ describe('AsyncValidator', function () {
})
.catch(function(err){
expect($exceptionHandler.fn.callCount).to.equal(2);
expect($exceptionHandler.fn.args[1][0].message).to.equal('ok');
$exceptionHandler.fn.restore();
expect(err).to.equal('ok');
done();
});

Expand Down Expand Up @@ -353,7 +358,6 @@ describe('AsyncValidator', function () {
$scope.$digest();

expect($exceptionHandler.fn.args[0][0]).to.match(/uhoh/);
$exceptionHandler.fn.restore();
});

it('ignores invalid expressions', function(){
Expand Down Expand Up @@ -386,7 +390,6 @@ describe('AsyncValidator', function () {
$scope.$destroy();

expect(el.controller().$asyncValidators).to.deep.equal({});
$exceptionHandler.fn.restore();
});

it('removes synchronous validators with same name', function(){
Expand Down Expand Up @@ -531,6 +534,62 @@ describe('AsyncValidator', function () {
expect($scope['ok'].args[0]).to.be.deep.equal([{ }]);
});

it('can use the same validator many times with different options', function(){
var spy = sinon.spy();

Provider.register('reuseValidator', function(){
return function($model, options) {
spy($model.$viewValue, options.mode);

switch (options.mode) {
case 'number':
return /^[0-9]+$/.test($model.$viewValue);
case 'object':
try {
return angular.isObject(JSON.parse($model.$viewValue));
} catch (e) {
return false;
}
case 'boolean':
return !!$model.$viewValue;
}
return true; // never fail the base validator
};
});

$scope['options'] = {
number: { mode: 'number' },
object: { mode: 'object' },
boolean: { mode: 'boolean' },
};

var el = input('<input ng-model="data.n6" async-validator-options-boolean="options.boolean" async-validator-options-object="options.object" async-validator-options-number="options.number" async-validator="{number: \'reuseValidator\', object: \'reuseValidator\', boolean: \'reuseValidator\'}">');
el.compiled($scope);
$scope.$digest();

expect(spy.args[0]).to.deep.equal([$scope.data.n6, 'number']);
expect(spy.args[1]).to.deep.equal([$scope.data.n6, 'object']);
expect(spy.args[2]).to.deep.equal([$scope.data.n6, 'boolean']);

expect(el.controller()).to.have.deep.property('$error.number', true);
expect(el.controller()).to.not.have.deep.property('$error.boolean');
expect(el.controller()).to.have.deep.property('$error.object', true);

$scope.data.n6 = '{}';
$scope.$digest();

expect(el.controller()).to.have.deep.property('$error.number', true);
expect(el.controller()).to.not.have.deep.property('$error.boolean');
expect(el.controller()).to.not.have.deep.property('$error.object');

$scope.data.n6 = '10';
$scope.$digest();

expect(el.controller()).to.not.have.deep.property('$error.number');
expect(el.controller()).to.not.have.deep.property('$error.boolean');
expect(el.controller()).to.have.deep.property('$error.object', true);
});


});
});
Expand Down

0 comments on commit 3aac2b6

Please sign in to comment.