Skip to content

Commit 2d21ea7

Browse files
Added does_not_contain
1 parent d192a65 commit 2d21ea7

File tree

6 files changed

+84
-3
lines changed

6 files changed

+84
-3
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fieldval-basicval",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"main": "fieldval-basicval.js",
55
"ignore": [
66
"coverage",

fieldval-basicval.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ var BasicVal = (function(){
137137
error: 104,
138138
error_message: "Value not allowed"
139139
};
140+
},
141+
should_not_contain: function(characters) {
142+
var disallowed = characters.join(",");
143+
return {
144+
error: 105,
145+
error_message: "Cannot contain "+disallowed
146+
};
140147
}
141148
},
142149
equal_to: function(match, flags){
@@ -316,6 +323,25 @@ var BasicVal = (function(){
316323
check: check
317324
};
318325
},
326+
does_not_contain: function(characters, flags){
327+
if(!Array.isArray(characters)){
328+
characters = [characters];
329+
}
330+
var check = function(value) {
331+
for(var i = 0; i < characters.length; i++){
332+
if(value.indexOf(characters[i])){
333+
return FieldVal.create_error(BasicVal.errors.should_not_contain, flags, characters);
334+
}
335+
}
336+
};
337+
if(flags){
338+
flags.check = check;
339+
return flags;
340+
}
341+
return {
342+
check: check
343+
};
344+
},
319345
one_of: function(array, flags) {
320346
var valid_values = [];
321347
if(Object.prototype.toString.call(array) === '[object Array]'){

fieldval-basicval.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fieldval-basicval",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "fieldval-basicval",
55
"main": "fieldval-basicval.js",
66
"scripts": {
@@ -17,6 +17,7 @@
1717
"mocha": "*",
1818
"gulp": "^3.6.1",
1919
"gulp-concat": "^2.2.0",
20+
"gulp-imports" : "MarcusLongmuir/gulp-imports",
2021
"gulp-istanbul": "^0.2.2",
2122
"gulp-jshint": "^1.8.4",
2223
"gulp-mocha": "^1.0.0",

src/BasicVal.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ var BasicVal = (function(){
137137
error: 104,
138138
error_message: "Value not allowed"
139139
};
140+
},
141+
should_not_contain: function(characters) {
142+
var disallowed = characters.join(",");
143+
return {
144+
error: 105,
145+
error_message: "Cannot contain "+disallowed
146+
};
140147
}
141148
},
142149
equal_to: function(match, flags){
@@ -316,6 +323,25 @@ var BasicVal = (function(){
316323
check: check
317324
};
318325
},
326+
does_not_contain: function(characters, flags){
327+
if(!Array.isArray(characters)){
328+
characters = [characters];
329+
}
330+
var check = function(value) {
331+
for(var i = 0; i < characters.length; i++){
332+
if(value.indexOf(characters[i])!==-1){
333+
return FieldVal.create_error(BasicVal.errors.should_not_contain, flags, characters);
334+
}
335+
}
336+
};
337+
if(flags){
338+
flags.check = check;
339+
return flags;
340+
}
341+
return {
342+
check: check
343+
};
344+
},
319345
one_of: function(array, flags) {
320346
var valid_values = [];
321347
if(Object.prototype.toString.call(array) === '[object Array]'){

test/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,34 @@ describe('BasicVal', function() {
196196
})
197197
})
198198

199+
describe('does_not_contain()', function() {
200+
201+
it('should return a value when it is below the specified value', function() {
202+
var my_validator = new FieldVal({
203+
"my_value": "ABCDEF"
204+
})
205+
assert.equal("ABCDEF", my_validator.get("my_value", bval.string(true), bval.does_not_contain(["G","H","I"])));
206+
assert.strictEqual(null, my_validator.end());
207+
})
208+
209+
it('should create an error when the value is below the specified value', function() {
210+
var my_validator = new FieldVal({
211+
"my_value": "ABCDEF"
212+
})
213+
assert.strictEqual(undefined, my_validator.get("my_value", bval.string(true), bval.does_not_contain(["A","C","F"])));
214+
assert.deepEqual({
215+
"invalid":{
216+
"my_value":{
217+
"error":105,
218+
"error_message":"Cannot contain A,C,F"
219+
}
220+
},
221+
"error_message":"One or more errors.",
222+
"error":0
223+
}, my_validator.end());
224+
})
225+
})
226+
199227
describe('min_length()', function() {
200228

201229
it('should return a value when it is longer than the specified length', function() {

0 commit comments

Comments
 (0)