Skip to content

Commit

Permalink
Resolves #2 and resolves #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ClickerMonkey committed May 31, 2016
1 parent 7fc96ec commit ca01ea2
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 57 deletions.
20 changes: 20 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"bitwise": false,
"curly": true,
"eqeqeq": true,
"esversion": 5,
"freeze": true,
"funcscope": true,
"loopfunc": false,
"maxdepth": 5,
"noarg": true,
"nocomma": true,
"nonbsp": true,
"nonew": true,
"notypeof": true,
"predef": [ "MY_GLOBAL" ],
"shadow": true,
"singleGroups": false,
"undef": true,
"unused": false
}
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "0.10"
- "4.0"
before_script:
- npm install -g gulp
- npm install -g gulp
13 changes: 13 additions & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var qunit = require('gulp-qunit');
var shell = require('gulp-shell');
var merge = require('merge-stream');
var size = require('gulp-check-filesize');
var jshint = require('gulp-jshint');

var build = {
filename: 'rekord-validation.js',
Expand Down Expand Up @@ -43,6 +44,9 @@ var executeBuild = function(props)
.pipe( plugins.concat( props.filename ) )
.pipe( size({enableGzip: true}) )
.pipe( gulp.dest( props.output ) )
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'))
;
};
};
Expand All @@ -54,6 +58,15 @@ var executeTest = function(file)
};
};

gulp.task('lint', function() {
return gulp
.src(build.output + build.filename)
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'))
;
});

gulp.task( 'docs', shell.task(['./node_modules/.bin/jsdoc -c jsdoc.json']));
gulp.task( 'clean', shell.task(['rm -rf build/*.js', 'rm -rf build/*.map']));
gulp.task( 'test', executeTest( './test/index.html' ) );
Expand Down
7 changes: 5 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rekord-validation",
"version": "1.0.0",
"version": "1.0.1",
"homepage": "https://github.com/Rekord/rekord-validation",
"authors": [
"Philip Diffenderfer <[email protected]>"
Expand All @@ -21,5 +21,8 @@
"bower_components",
"test",
"tests"
]
],
"dependencies": {
"rekord": "^1.2.0"
}
}
57 changes: 33 additions & 24 deletions build/rekord-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Database = Rekord.Database;
var Promise = Rekord.Promise;
var Collection = Rekord.Collection;
var ModelCollection = Rekord.ModelCollection;

var isEmpty = Rekord.isEmpty;
var isString = Rekord.isString;
Expand All @@ -16,6 +17,7 @@
var isValue = Rekord.isValue;
var isPrimitiveArray = Rekord.isPrimitiveArray;
var isRegExp = Rekord.isRegExp;
var isRekord = Rekord.isRekord;

var noop = Rekord.noop;
var equalsCompare = Rekord.equalsCompare;
Expand Down Expand Up @@ -116,7 +118,7 @@ function determineMessage(ruleName, message)
return message || Validation.Rules[ ruleName ].message;
}

function joinFriendly(arr, lastSeparator, itemSeparator, getAlias)
function joinFriendly(arr, lastSeparatorCustom, itemSeparatorCustom, getAlias)
{
var copy = arr.slice();

Expand All @@ -129,8 +131,8 @@ function joinFriendly(arr, lastSeparator, itemSeparator, getAlias)
}

var last = copy.pop();
var lastSeparator = lastSeparator || 'and';
var itemSeparator = itemSeparator || ', ';
var lastSeparator = lastSeparatorCustom || 'and';
var itemSeparator = itemSeparatorCustom || ', ';

switch (copy.length) {
case 0:
Expand Down Expand Up @@ -207,7 +209,7 @@ Rekord.on( Rekord.Events.Plugins, function(model, db, options)

for ( var field in rules )
{
db.validations[ field ] = Validation.parseRules( rules[ field ], field, db, getAlias, messages[ field ] )
db.validations[ field ] = Validation.parseRules( rules[ field ], field, db, getAlias, messages[ field ] );
}

addMethod( model.prototype, '$validate', function()
Expand All @@ -226,7 +228,7 @@ Rekord.on( Rekord.Events.Plugins, function(model, db, options)
var value = this.$get( field );
var fieldValid = true;

var setMessage = function(message)
var setMessage = function(message) // jshint ignore:line
{
// Only accept for the first valid message
if ( message && fieldValid )
Expand Down Expand Up @@ -316,26 +318,26 @@ var Validation =
for (var i = 0; i < rules.length; i++)
{
var rule = rules[ i ];
var validator = this.parseRule( rule, field, database, getAlias, message );
var defaultMessageValidator = this.parseRule( rule, field, database, getAlias, message );

validators.push( validator );
validators.push( defaultMessageValidator );
}
}
else if ( isObject( rules ) )
{
for (var rule in rules)
for (var ruleProperty in rules)
{
var ruleMessageOrData = rules[ rule ];
var ruleMessageOrData = rules[ ruleProperty ];

var ruleMessage = isObject( ruleMessageOrData ) ? ruleMessageOrData.message :
( isString( ruleMessageOrData ) ? ruleMessageOrData : undefined );

var ruleInput = isObject( ruleMessageOrData ) && ruleMessageOrData.message ? ruleMessageOrData.input :
( isString( ruleMessageOrData ) ? undefined : ruleMessageOrData );

var validator = this.parseRule( rule, field, database, getAlias, ruleMessage || message, ruleInput );
var customMessageValidator = this.parseRule( ruleProperty, field, database, getAlias, ruleMessage || message, ruleInput );

validators.push( validator );
validators.push( customMessageValidator );
}
}

Expand Down Expand Up @@ -641,7 +643,7 @@ function collectionRuleGenerator(ruleName, defaultMessage, isInvalid)

if ( indexOf( database.fields, matchField ) === -1 )
{
throw otherField + ' is not a valid field for the ' + ruleName + ' rule';
throw matchField + ' is not a valid field for the ' + ruleName + ' rule';
}

var messageTemplate = determineMessage( ruleName, message );
Expand Down Expand Up @@ -679,11 +681,11 @@ Validation.Rules.validate = function(field, params, database, getAlias, message)

for (var i = 0; i < value.length; i++)
{
var model = value[ i ];
var related = value[ i ];

if ( model && model.$validate && !model.$validate() )
if ( related && related.$validate && !related.$validate() )
{
invalid.push( model );
invalid.push( related );
}
}

Expand Down Expand Up @@ -1080,7 +1082,7 @@ function fieldsRuleGenerator(ruleName, defaultMessage, isInvalid)
};

Validation.Rules[ ruleName ].message = defaultMessage;
};
}

// exists:X,Y
foreignRuleGenerator('exists',
Expand Down Expand Up @@ -1299,7 +1301,7 @@ listRuleGenerator('not_in',
'{$alias} must not be one of {$list}.',
function isInvalid(value, model, inList)
{
return inList( value, model )
return inList( value, model );
}
);

Expand Down Expand Up @@ -1949,22 +1951,29 @@ Validation.Rules.startOfDay = function(field, params, database, alias, message)

Validation.Rules.trim = function(field, params, database, alias, message)
{
// String.trim polyfill
if ( !String.prototype.trim )
var trim = (function()
{
if ( String.prototype.trim )
{
return function(x) {
return x.trim();
};
}

var regex = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;

String.prototype.trim = function()
return function(x)
{
return this.replace( regex, '' );
return x.replace( regex, '' );
};
}

})();

return function(value, model, setMessage)
{
if ( isString( value ) )
{
value = value.trim();
value = trim( value );
}

return value;
Expand Down Expand Up @@ -2010,4 +2019,4 @@ Validation.Rules.unbase64 = function(field, params, database, alias, message)
Rekord.checkNoParams = checkNoParams;
Rekord.generateMessage = generateMessage;

})(this, Rekord);
})(this, this.Rekord);
2 changes: 1 addition & 1 deletion build/rekord-validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/rekord-validation.min.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rekord-validation",
"version": "1.0.0",
"version": "1.0.1",
"description": "Advanced validation rules for rekord",
"author": "Philip Diffenderfer",
"license": "MIT",
Expand All @@ -15,15 +15,20 @@
"gulp": "^3.9.0",
"gulp-check-filesize": "^2.0.1",
"gulp-concat": "^2.6.0",
"gulp-jshint": "^2.0.1",
"gulp-load-plugins": "^1.1.0",
"gulp-qunit": "^1.3.0",
"gulp-shell": "^0.5.1",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.5.1",
"gulp-util": "^3.0.7",
"gulp-watch": "^4.3.5",
"gulp-watch": "^4.3.6",
"jaguarjs-jsdoc": "0.0.1",
"jsdoc": "^3.4.0",
"merge-stream": "^1.0.0"
"merge-stream": "^1.0.0",
"jshint": "^2.9.2"
},
"dependencies": {
"rekord": "^1.2.0"
}
}
2 changes: 1 addition & 1 deletion src/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
Rekord.checkNoParams = checkNoParams;
Rekord.generateMessage = generateMessage;

})(this, Rekord);
})(this, this.Rekord);
2 changes: 2 additions & 0 deletions src/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Database = Rekord.Database;
var Promise = Rekord.Promise;
var Collection = Rekord.Collection;
var ModelCollection = Rekord.ModelCollection;

var isEmpty = Rekord.isEmpty;
var isString = Rekord.isString;
Expand All @@ -16,6 +17,7 @@
var isValue = Rekord.isValue;
var isPrimitiveArray = Rekord.isPrimitiveArray;
var isRegExp = Rekord.isRegExp;
var isRekord = Rekord.isRekord;

var noop = Rekord.noop;
var equalsCompare = Rekord.equalsCompare;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/rules/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function collectionRuleGenerator(ruleName, defaultMessage, isInvalid)

if ( indexOf( database.fields, matchField ) === -1 )
{
throw otherField + ' is not a valid field for the ' + ruleName + ' rule';
throw matchField + ' is not a valid field for the ' + ruleName + ' rule';
}

var messageTemplate = determineMessage( ruleName, message );
Expand Down Expand Up @@ -103,11 +103,11 @@ Validation.Rules.validate = function(field, params, database, getAlias, message)

for (var i = 0; i < value.length; i++)
{
var model = value[ i ];
var related = value[ i ];

if ( model && model.$validate && !model.$validate() )
if ( related && related.$validate && !related.$validate() )
{
invalid.push( model );
invalid.push( related );
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/rules/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ function fieldsRuleGenerator(ruleName, defaultMessage, isInvalid)
};

Validation.Rules[ ruleName ].message = defaultMessage;
};
}
2 changes: 1 addition & 1 deletion src/lib/rules/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ listRuleGenerator('not_in',
'{$alias} must not be one of {$list}.',
function isInvalid(value, model, inList)
{
return inList( value, model )
return inList( value, model );
}
);

Expand Down
19 changes: 13 additions & 6 deletions src/lib/transforms/trim.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
Validation.Rules.trim = function(field, params, database, alias, message)
{
// String.trim polyfill
if ( !String.prototype.trim )
var trim = (function()
{
if ( String.prototype.trim )
{
return function(x) {
return x.trim();
};
}

var regex = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;

String.prototype.trim = function()
return function(x)
{
return this.replace( regex, '' );
return x.replace( regex, '' );
};
}

})();

return function(value, model, setMessage)
{
if ( isString( value ) )
{
value = value.trim();
value = trim( value );
}

return value;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function determineMessage(ruleName, message)
return message || Validation.Rules[ ruleName ].message;
}

function joinFriendly(arr, lastSeparator, itemSeparator, getAlias)
function joinFriendly(arr, lastSeparatorCustom, itemSeparatorCustom, getAlias)
{
var copy = arr.slice();

Expand All @@ -95,8 +95,8 @@ function joinFriendly(arr, lastSeparator, itemSeparator, getAlias)
}

var last = copy.pop();
var lastSeparator = lastSeparator || 'and';
var itemSeparator = itemSeparator || ', ';
var lastSeparator = lastSeparatorCustom || 'and';
var itemSeparator = itemSeparatorCustom || ', ';

switch (copy.length) {
case 0:
Expand Down
Loading

0 comments on commit ca01ea2

Please sign in to comment.