Comparing version 0.1.0 to 0.2.0
/*! | ||
* iz - v0.1.0 https://github.com/parris/iz | ||
* Built: 2013-04-06 | ||
* iz - v0.2.0 https://github.com/parris/iz | ||
* Built: 2013-05-21 | ||
* Copyright (c) 2013 Parris Khachi; | ||
@@ -5,0 +5,0 @@ * Licensed under the MIT license |
/*! | ||
* iz - v0.1.0 https://github.com/parris/iz | ||
* Built: 2013-04-06 | ||
* iz - v0.2.0 https://github.com/parris/iz | ||
* Built: 2013-05-21 | ||
* Copyright (c) 2013 Parris Khachi; | ||
@@ -5,0 +5,0 @@ * Licensed under the MIT license |
{ | ||
"name": "iz", | ||
"version": "0.1.0", | ||
"description": "Some basic validation functions that were useful across projects", | ||
"version": "0.2.0", | ||
"description": "Validation for node and the web.", | ||
"main": "app.js", | ||
@@ -24,3 +24,3 @@ "homepage": "https://github.com/parris/iz", | ||
"grunt-shell": "~0.2.2", | ||
"one": "~2.0.8", | ||
"one": "~3.0.13", | ||
"grunt-simple-mocha": "~0.3.2", | ||
@@ -27,0 +27,0 @@ "grunt-cli": "~0.1.7" |
@@ -10,2 +10,3 @@ [![Build Status](https://secure.travis-ci.org/parris/iz.png)](http://travis-ci.org/parris/iz) | ||
format that is our job to normalize! In fact that might be a good next project... "norm.js" sounds fairly sexy to me :). | ||
It also provides validation for required fields and separates it from whether what a user has entered is valid. | ||
@@ -61,2 +62,18 @@ Setup | ||
Required Fields: | ||
When using iz.required(*) it can be used alone or chained with other validators to cover the following scenarios: | ||
iz(value).required() //value is required | ||
iz(value).required().email() //value is required and is a valid email | ||
iz(value).date() //value is not required but must be a date if provided | ||
This behaviour is great for validating user input or payloads sent to an api where validation of required fields is a common need. | ||
N.B. However, it means that iz(null).date(), iz(undefined).email() and iz('').int() will all return true!!! While this seems counter intuitive, it is important to realise that iz validates only if a value is actually provided. Whether a value is required or not is a separate concern altogether and is covered by iz.required(*); | ||
Validators: | ||
All validators (apart from iz.required) return true if no value is provided (e.g. null, undefined or ''). | ||
Possible validations so far (true case in comments): | ||
@@ -88,2 +105,3 @@ | ||
iz.postal(*); // Is a postal code or zip code | ||
iz.required(*); // Is not null, undefined or an empty string | ||
iz.ssn(*); // Is a social security number | ||
@@ -161,2 +179,7 @@ | ||
0.2.0 | ||
---- | ||
- Added iz.required() | ||
- Falsy values now pass through as valid without `.required` | ||
0.1.0 | ||
@@ -163,0 +186,0 @@ ---- |
@@ -19,2 +19,5 @@ /*global describe, it, xit, xdescribe, before, require */ | ||
iz.alphaNumeric([]).should.not.be.ok; | ||
iz.alphaNumeric(null).should.be.ok; | ||
iz.alphaNumeric(undefined).should.be.ok; | ||
}); | ||
@@ -34,2 +37,5 @@ | ||
iz.between(function (){}, function (){}, function () {}).should.not.be.ok; //it also despises functions | ||
iz.between(null, 1, 5).should.be.ok; | ||
iz.between(undefined, 1, 5).should.be.ok; | ||
}); | ||
@@ -46,2 +52,5 @@ | ||
iz.boolean(/[ \-]/g).should.not.be.ok; | ||
iz.boolean(null).should.be.ok; | ||
iz.boolean(undefined).should.be.ok; | ||
}); | ||
@@ -52,5 +61,6 @@ | ||
iz.blank("hi").should.not.be.ok; | ||
iz.blank(undefined).should.not.be.ok; | ||
iz.blank(null).should.not.be.ok; | ||
iz.blank([]).should.not.be.ok; | ||
iz.blank(undefined).should.be.ok; | ||
iz.blank(null).should.be.ok; | ||
}); | ||
@@ -88,2 +98,5 @@ | ||
iz.cc(["5"]).should.not.be.ok; | ||
iz.cc(null).should.be.ok; | ||
iz.cc(undefined).should.be.ok; | ||
}); | ||
@@ -101,2 +114,5 @@ | ||
iz.date([]).should.not.be.ok; | ||
iz.date(null).should.be.ok; | ||
iz.date(undefined).should.be.ok; | ||
}); | ||
@@ -115,2 +131,5 @@ | ||
iz.decimal([]).should.not.be.ok; | ||
iz.decimal(null).should.be.ok; | ||
iz.decimal(undefined).should.be.ok; | ||
}); | ||
@@ -126,2 +145,5 @@ | ||
iz.email(5).should.not.be.ok; | ||
iz.email(null).should.be.ok; | ||
iz.email(undefined).should.be.ok; | ||
}); | ||
@@ -137,2 +159,5 @@ | ||
iz.empty(["hi"]).should.not.be.ok; | ||
iz.empty(null).should.be.ok; | ||
iz.empty(undefined).should.be.ok; | ||
}); | ||
@@ -196,4 +221,2 @@ | ||
iz.fileExtension("hello.PNG", ["png"]).should.be.ok; | ||
iz.fileExtension("", []).should.not.be.ok; | ||
iz.fileExtension("","").should.not.be.ok; | ||
iz.fileExtension("hello.png", ["PNG"]).should.not.be.ok; | ||
@@ -203,2 +226,5 @@ iz.fileExtension("hello.png", [".png"]).should.not.be.ok; | ||
iz.fileExtension({},{}).should.not.be.ok; | ||
iz.fileExtension(null).should.be.ok; | ||
iz.fileExtension(undefined).should.be.ok; | ||
}); | ||
@@ -209,2 +235,5 @@ | ||
iz.fileExtensionAudio("apple.png").should.not.be.ok; | ||
iz.fileExtensionAudio(null).should.be.ok; | ||
iz.fileExtensionAudio(undefined).should.be.ok; | ||
}); | ||
@@ -215,2 +244,5 @@ | ||
iz.fileExtensionImage("apple.mp3").should.not.be.ok; | ||
iz.fileExtensionImage(null).should.be.ok; | ||
iz.fileExtensionImage(undefined).should.be.ok; | ||
}); | ||
@@ -221,2 +253,5 @@ | ||
iz.fileExtensionVideo("apple.mp3").should.not.be.ok; | ||
iz.fileExtensionVideo(null).should.be.ok; | ||
iz.fileExtensionVideo(undefined).should.be.ok; | ||
}); | ||
@@ -244,2 +279,5 @@ | ||
iz.int(function () {}).should.not.be.ok; | ||
iz.int(null).should.be.ok; | ||
iz.int(undefined).should.be.ok; | ||
}); | ||
@@ -262,2 +300,5 @@ | ||
iz.ip("030000001353").should.not.be.ok; | ||
iz.ip(null).should.be.ok; | ||
iz.ip(undefined).should.be.ok; | ||
}); | ||
@@ -271,2 +312,5 @@ | ||
iz.minLength("lizard", {}).should.not.be.ok; | ||
iz.minLength(null).should.be.ok; | ||
iz.minLength(undefined).should.be.ok; | ||
}); | ||
@@ -278,2 +322,5 @@ | ||
iz.minLength([1, 2, 3, 4, 5, 6], 7).should.not.be.ok; | ||
iz.minLength(null).should.be.ok; | ||
iz.minLength(undefined).should.be.ok; | ||
}); | ||
@@ -287,2 +334,5 @@ | ||
iz.maxLength("lizard", {}).should.not.be.ok; | ||
iz.maxLength(null).should.be.ok; | ||
iz.maxLength(undefined).should.be.ok; | ||
}); | ||
@@ -294,2 +344,5 @@ | ||
iz.maxLength([1, 2, 3, 4, 5, 6], 5).should.not.be.ok; | ||
iz.maxLength(null).should.be.ok; | ||
iz.maxLength(undefined).should.be.ok; | ||
}); | ||
@@ -302,2 +355,5 @@ | ||
iz.multiple(5, {}).should.not.be.ok; // disallow everything but numbers | ||
iz.multiple(null).should.be.ok; | ||
iz.multiple(undefined).should.be.ok; | ||
}); | ||
@@ -311,2 +367,5 @@ | ||
iz.number("bob").should.not.be.ok; | ||
iz.number(null).should.be.ok; | ||
iz.number(undefined).should.be.ok; | ||
}); | ||
@@ -322,2 +381,5 @@ | ||
iz.ofType(obj, "Object").should.be.ok; | ||
iz.ofType(null).should.be.ok; | ||
iz.ofType(undefined).should.be.ok; | ||
}); | ||
@@ -339,2 +401,5 @@ | ||
iz.phone("12345678901").should.be.ok; | ||
iz.phone(null).should.be.ok; | ||
iz.phone(undefined).should.be.ok; | ||
}); | ||
@@ -351,2 +416,5 @@ | ||
iz.postal("9411").should.not.be.ok; | ||
iz.postal(null).should.be.ok; | ||
iz.postal(undefined).should.be.ok; | ||
}); | ||
@@ -363,3 +431,17 @@ | ||
iz.ssn("123-45-678").should.not.be.ok; | ||
iz.ssn(null).should.be.ok; | ||
iz.ssn(undefined).should.be.ok; | ||
}); | ||
it("can validate required", function() { | ||
iz.required(null).should.not.be.ok; | ||
iz.required(undefined).should.not.be.ok; | ||
iz.required("").should.not.be.ok; | ||
iz.required({}).should.be.ok; | ||
iz.required(function () {}).should.be.ok; | ||
iz.required([]).should.be.ok; | ||
iz.required(5).should.be.ok; | ||
iz.required(new Date()).should.be.ok; | ||
}); | ||
}); |
(function() { | ||
var validators = {}; | ||
function iz_alphaNumeric(value) { | ||
@@ -346,29 +346,41 @@ return (/^[a-z0-9]+$/i).test(value); | ||
function iz_required(obj) { | ||
return obj !== undefined && obj !== null && obj !== ''; | ||
} | ||
function iz_required_or(validator) { | ||
return function(val) { | ||
return !iz_required(val) || validator.apply(this, Array.prototype.slice.call(arguments)); | ||
}; | ||
} | ||
//Expose some methods, this is done to preserve function names in all browsers | ||
validators.alphaNumeric = iz_alphaNumeric; | ||
validators.between = iz_between; | ||
validators.blank = iz_blank; | ||
validators.boolean = iz_boolean; | ||
validators.cc = iz_cc; | ||
validators.date = iz_date; | ||
validators.decimal = iz_decimal; | ||
validators.email = iz_email; | ||
validators.empty = iz_empty; | ||
validators.equal = iz_equal; | ||
validators.alphaNumeric = iz_required_or(iz_alphaNumeric); | ||
validators.between = iz_required_or(iz_between); | ||
validators.blank = iz_required_or(iz_blank); | ||
validators.boolean = iz_required_or(iz_boolean); | ||
validators.cc = iz_required_or(iz_cc); | ||
validators.date = iz_required_or(iz_date); | ||
validators.decimal = iz_required_or(iz_decimal); | ||
validators.email = iz_required_or(iz_email); | ||
validators.empty = iz_required_or(iz_empty); | ||
validators.equal = iz_required_or(iz_equal); | ||
validators.extension = iz_extension; | ||
validators.fileExtension = iz_fileExtension; | ||
validators.fileExtensionAudio = iz_fileExtensionAudio; | ||
validators.fileExtensionImage = iz_fileExtensionImage; | ||
validators.fileExtensionVideo = iz_fileExtensionVideo; | ||
validators.fileExtension = iz_required_or(iz_fileExtension); | ||
validators.fileExtensionAudio = iz_required_or(iz_fileExtensionAudio); | ||
validators.fileExtensionImage = iz_required_or(iz_fileExtensionImage); | ||
validators.fileExtensionVideo = iz_required_or(iz_fileExtensionVideo); | ||
validators.inArray = iz_inArray; | ||
validators.int = iz_int; | ||
validators.ip = iz_ip; | ||
validators.minLength = iz_minLength; | ||
validators.maxLength = iz_maxLength; | ||
validators.multiple = iz_multiple; | ||
validators.number = iz_number; | ||
validators.ofType = iz_ofType; | ||
validators.phone = iz_phone; | ||
validators.postal = iz_postal; | ||
validators.ssn = iz_ssn; | ||
validators.int = iz_required_or(iz_int); | ||
validators.ip = iz_required_or(iz_ip); | ||
validators.minLength = iz_required_or(iz_minLength); | ||
validators.maxLength = iz_required_or(iz_maxLength); | ||
validators.multiple = iz_required_or(iz_multiple); | ||
validators.number = iz_required_or(iz_number); | ||
validators.ofType = iz_required_or(iz_ofType); | ||
validators.phone = iz_required_or(iz_phone); | ||
validators.postal = iz_required_or(iz_postal); | ||
validators.required = iz_required; | ||
validators.ssn = iz_required_or(iz_ssn); | ||
@@ -375,0 +387,0 @@ |
@@ -5,5 +5,3 @@ /*global require, module, window */ | ||
// serves as our bootstrap into other node apps | ||
// let's not clutter up the .bin folder | ||
// this also helps when using onejs to compile for client side | ||
// helps when using onejs to compile for client side | ||
var iz = require('./iz'), | ||
@@ -10,0 +8,0 @@ are = require('./are'), |
{ | ||
"name": "iz", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "src/web.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
86788
1566
207
0