Comparing version 0.0.2 to 0.0.3
@@ -1,1 +0,1 @@ | ||
/*global module, exports*/var iz=iz||{};(function(){"use strict";function e(e){return/^[a-z0-9]+$/i.test(e)}function t(e){return typeof e!="string"&&typeof e!="number"||!!isNaN(e%1)?!1:!0}function n(e,t,n){return typeof e=="object"||typeof e=="function"||typeof t=="object"||typeof t=="function"||typeof n=="object"||typeof n=="function"?!1:e>=t&&e<=n?!0:!1}function r(e){return typeof e=="boolean"||typeof e=="number"&&(e===0||e===1)?!0:!1}function i(e,n){return typeof n!="boolean"&&(n=!1),n?t(e)&&e%1===0?!0:!1:/^\s*(\+|-)?\d+\s*$/.test(e)}function s(e){var t=e.length,n=0,r=[[0,1,2,3,4,5,6,7,8,9],[0,2,4,6,8,1,3,5,7,9]],i=0;while(t--)i+=r[n][parseInt(e.charAt(t),10)],n^=1;return i%10===0&&i>0}function o(e){if(e&&e.constructor&&e.constructor.toString){var t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return undefined}function u(e){return typeof e!="string"&&typeof e!="number"?!1:(e=e.replace(/[ -]/g,""),i(e)?s(e):!1)}function a(e){return o(e)==="Date"?!0:(new Date(e)).toString()!=="Invalid Date"||!isNaN(new Date(e))?!0:!1}function f(e){return t(e)&&e%1!==0?!0:!1}function l(e){return typeof e!="string"?!1:/\S+@\S+/.test(e)}function c(e,t){var n;if(typeof e!="object"||typeof t!="object")return!1;for(n in t)if(t.hasOwnProperty(n)&&typeof e[n]=="undefined")return!1;return!0}function h(e,t){var n;return typeof e!="object"||typeof e.indexOf=="undefined"||typeof t!="string"?!1:(n=t.split(".").pop().toLowerCase(),e.indexOf(n)!==-1?!0:!1)}function p(e){var t=["mp3","ogg","aac","wav"];return h(t,e)}function d(e){var t=["gif","png","jpeg","jpg","svg","bmp"];return h(t,e)}function v(e){var t=["mp4","ogv","m4v","mov","avi"];return h(t,e)}function m(e,t){return typeof e!="object"||typeof e.indexOf=="undefined"?!1:e.indexOf(t)!==-1?!0:!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25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,2}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:(?:[0-9a-fA-F]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,3}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:[0-9a-fA-F]{1,4})):)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,4}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,5}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::))))$/;return t.test(e)}function y(e,t){return(typeof e=="string"||typeof e=="object")&&typeof e.length!="undefined"&&i(t)&&e.length>=t?!0:!1}function b(e,t){return(typeof e=="string"||typeof e=="object")&&typeof e.length!="undefined"&&i(t)&&e.length<=t?!0:!1}function w(e,t){return typeof e!="number"||typeof t!="number"?!1:e%t===0?!0:!1}function E(e,t){return typeof e=="object"&&typeof e.length=="undefined"&&typeof t=="string"&&o(e)===t?!0:!1}function S(e){var t="",n=[];if(typeof e=="string"){t=e.replace(/[^x0-9]/g,""),n=t.split("x");if(n.length>0&&i(n[0])&&(n[0].length===10||n[0].length===11)&&i(n.pop()))return!0}return!1}function x(e){var t="";if(typeof e=="string"){t=e.replace(/[^0-9]/g,"");if(i(t)&&(t.length===5||t.length===9))return!0}return!1}function T(e){var t="";if(typeof e=="string"){t=e.replace(/[^0-9]/g,"");if(i(t)&&t.length===9)return!0}return!1}iz.alphaNumeric=e,iz.between=n,iz.boolean=r,iz.cc=u,iz.date=a,iz.decimal=f,iz.email=l,iz.extension=c,iz.fileExtension=h,iz.fileExtensionAudio=p,iz.fileExtensionImage=d,iz.fileExtensionVideo=v,iz.inArray=m,iz.int=i,iz.ip=g,iz.minLength=y,iz.maxLength=b,iz.multiple=w,iz.number=t,iz.ofType=E,iz.phone=S,iz.postal=x,iz.ssn=T})(),typeof exports!="undefined"&&(exports.iz=iz),typeof module!="undefined"&&(module.exports=iz); | ||
/*global module, exports, window*/(function(){"use strict";function n(e){return/^[a-z0-9]+$/i.test(e)}function r(e){return typeof e!="string"&&typeof e!="number"||!!isNaN(e%1)?!1:!0}function i(e,t,n){return typeof e=="object"||typeof e=="function"||typeof t=="object"||typeof t=="function"||typeof n=="object"||typeof n=="function"?!1:e>=t&&e<=n?!0:!1}function s(e){return typeof e=="boolean"||typeof e=="number"&&(e===0||e===1)?!0:!1}function o(e,t){return typeof t!="boolean"&&(t=!1),t?r(e)&&e%1===0?!0:!1:/^\s*(\+|-)?\d+\s*$/.test(e)}function u(e){var t=e.length,n=0,r=[[0,1,2,3,4,5,6,7,8,9],[0,2,4,6,8,1,3,5,7,9]],i=0;while(t--)i+=r[n][parseInt(e.charAt(t),10)],n^=1;return i%10===0&&i>0}function a(e){if(e&&e.constructor&&e.constructor.toString){var t=e.constructor.toString().match(/function\s*(\w+)/);if(t&&t.length===2)return t[1]}return undefined}function f(e){return typeof e!="string"&&typeof e!="number"?!1:(e=e.replace(/[ \-]/g,""),o(e)?u(e):!1)}function l(e){return a(e)==="Date"?!0:(new Date(e)).toString()!=="Invalid Date"||!isNaN(new Date(e))?!0:!1}function c(e){return r(e)&&e%1!==0?!0:!1}function h(e){return typeof e!="string"?!1:/\S+@\S+/.test(e)}function p(e){var t=typeof e,n;if(e.hasOwnProperty("length")&&t!=="function"&&e.length>0)return!1;if(t==="function"||t==="object")for(n in e)if(e.hasOwnProperty(n))return!1;return!0}function d(e,t){var n=typeof e,r=typeof t,i;if(n!=="object"&&n!=="function"||typeof e.equals!="function"){if(n==="object"||n==="function"){for(i in e)if(e.hasOwnProperty(i)&&!t.hasOwnProperty(i)&&i!=="equals"){if(typeof e[i]=="object"||typeof e[i]=="function"&&!d(e[i],t[i]))return!1;if(e[i]!==t[i])return!1}return!0}}else if(r==="object"||r==="function")return e.equals(t);return e===t}function v(e,t){var n;if(typeof e!="object"||typeof t!="object")return!1;for(n in t)if(t.hasOwnProperty(n)&&typeof e[n]=="undefined")return!1;return!0}function m(e,t){var n;return typeof t!="object"||typeof t.indexOf=="undefined"||typeof e!="string"?!1:(n=e.split(".").pop().toLowerCase(),t.indexOf(n)!==-1?!0:!1)}function g(e){var t=["mp3","ogg","aac","wav"];return m(e,t)}function y(e){var t=["gif","png","jpeg","jpg","svg","bmp"];return m(e,t)}function b(e){var t=["mp4","ogv","m4v","mov","avi"];return m(e,t)}function w(e,t){return typeof t!="object"||typeof t.indexOf=="undefined"?!1:t.indexOf(e)!==-1?!0:!1}function E(e){var t=/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])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-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,3}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:[0-9a-fA-F]{1,4})):)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,4}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,5}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::))))$/;return t.test(e)}function S(e,t){return(typeof e=="string"||typeof e=="object")&&typeof e.length!="undefined"&&o(t)&&e.length>=t?!0:!1}function x(e,t){return(typeof e=="string"||typeof e=="object")&&typeof e.length!="undefined"&&o(t)&&e.length<=t?!0:!1}function T(e,t){return typeof e!="number"||typeof t!="number"?!1:e%t===0?!0:!1}function N(e,t){return typeof e=="object"&&typeof e.length=="undefined"&&typeof t=="string"&&a(e)===t?!0:!1}function C(e){var t="",n=[];if(typeof e=="string"){t=e.replace(/[^x0-9]/g,""),n=t.split("x");if(n.length>0&&o(n[0])&&(n[0].length===10||n[0].length===11)&&o(n.pop()))return!0}return!1}function k(e){var t="";if(typeof e=="string"){t=e.replace(/[^0-9]/g,"");if(o(t)&&(t.length===5||t.length===9))return!0}return!1}function L(e){var t="";if(typeof e=="string"){t=e.replace(/[^0-9]/g,"");if(o(t)&&t.length===9)return!0}return!1}var e={},t;e.alphaNumeric=n,e.between=i,e.boolean=s,e.cc=f,e.date=l,e.decimal=c,e.email=h,e.empty=p,e.equal=d,e.extension=v,e.fileExtension=m,e.fileExtensionAudio=g,e.fileExtensionImage=y,e.fileExtensionVideo=b,e.inArray=w,e.int=o,e.ip=E,e.minLength=S,e.maxLength=x,e.multiple=T,e.number=r,e.ofType=N,e.phone=C,e.postal=k,e.ssn=L,t=function(t,n){function r(e,t){function r(){return n._not=!0,n}var n=this;typeof t=="object"?this.error_messages=t:this.error_messages={},this._not=!1,this.not=r,this.value=e,this.errors=[],this.valid=!0}function i(n){var r=Array.prototype.slice.call(arguments,1);return r.unshift(t),function(){var t=r.concat(Array.prototype.slice.call(arguments)),i=e[n].apply(null,t);if(!this._not&&!i||this._not&&i)!this._not&&typeof this.error_messages[n]!="undefined"?this.errors.push(this.error_messages[n]):this._not&&typeof this.error_messages["not_"+n]!="undefined"?this.errors.push(this.error_messages["not_"+n]):this._not?this.errors.push("Not "+n):this.errors.push(n),this.valid=!1;return this._not=!1,this}}for(var s in e)e.hasOwnProperty(s)&&(r.prototype[s]=i(s));return new r(t,n)};for(var A in e)e.hasOwnProperty(A)&&(t[A]=e[A]);typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(exports=module.exports=t),exports.iz=t):window.iz=t})(); |
103
iz.js
@@ -71,4 +71,4 @@ /*global module, exports, window*/ | ||
* Returns the name of the class that the object is | ||
* @author Ateş Göral: http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects | ||
* @param object | ||
* @author http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects | ||
* @param obj{Object} | ||
* @return String name of the class | ||
@@ -140,2 +140,66 @@ */ | ||
/** | ||
* True if the parameter is empty. length > 0 for objects (if exists) or arrays, Functions/Objects have no properties, | ||
* or the type is primitive. | ||
* @param value of any type | ||
* @return {Boolean} | ||
*/ | ||
function iz_empty(value) { | ||
var type = typeof value, | ||
key; | ||
//arrays and objects with length properties | ||
if(value.hasOwnProperty("length") && type !== "function" && value.length > 0) { | ||
return false; | ||
} else if(type === "function" || type === "object") { | ||
for (key in value) { | ||
if(value.hasOwnProperty(key)) { | ||
return false; //on first valid key, return false; | ||
} | ||
} | ||
} | ||
//primitives are empty as are objects without properties and empty arrays | ||
return true; | ||
} | ||
/** | ||
* Strictly Equal | ||
* @param value | ||
* @param value2 | ||
* @return {Boolean} | ||
*/ | ||
function iz_equal(value, value2) { | ||
var valueType = typeof value, | ||
value2Type = typeof value2, | ||
key; | ||
if ((valueType === "object" || valueType === "function") && typeof value.equals === "function") { | ||
if ((value2Type === "object" || value2Type === "function")) { | ||
//value2 does not need the equals method, if an exception is thrown here that is the implementor | ||
//catching it returning false might result in a bug that is hard to track | ||
return value.equals(value2); | ||
} | ||
} else if(valueType === "object" || valueType === "function") { | ||
for(key in value) { | ||
if (value.hasOwnProperty(key) && !value2.hasOwnProperty(key) && key !== "equals") { | ||
//if property is an object then recursively check | ||
if (typeof value[key] === "object" || typeof value[key] === "function" && !iz_equal(value[key], value2[key])) { | ||
return false; | ||
} else if (value[key] !== value2[key]) { //if not object or function | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
return value === value2; | ||
} | ||
/** | ||
* Is obj1 and extension of obj2? True if this is the case. | ||
* @param obj1 | ||
* @param obj2 | ||
* @return {Boolean} | ||
*/ | ||
function iz_extension(obj1, obj2) { | ||
@@ -301,2 +365,4 @@ var key; | ||
validators.email = iz_email; | ||
validators.empty = iz_empty; | ||
validators.equal = iz_equal; | ||
validators.extension = iz_extension; | ||
@@ -321,3 +387,4 @@ validators.fileExtension = iz_fileExtension; | ||
* Factory for creating chained checking objects | ||
* @param value | ||
* @param value{*} | ||
* @param error_messages{Object} | ||
* @return {Object} of type Iz | ||
@@ -328,5 +395,8 @@ */ | ||
* @param value | ||
* @param error_messages | ||
* @constructor | ||
*/ | ||
function Iz(value, error_messages) { | ||
var self = this; | ||
if (typeof error_messages === "object") { | ||
@@ -337,2 +407,12 @@ this.error_messages = error_messages; | ||
} | ||
this._not = false; | ||
function not() { | ||
self._not = true; | ||
return self; | ||
} | ||
this.not = not; | ||
this.value = value; | ||
@@ -347,3 +427,3 @@ this.errors = []; | ||
* Also sets valid to false if an error is found. It can't ever set valid to true. | ||
* @param key | ||
* @param fn | ||
*/ | ||
@@ -356,11 +436,20 @@ function validator_partial(fn) { | ||
result = validators[fn].apply(null, allArguments); | ||
if (!result) { | ||
if (typeof this.error_messages[fn] !== "undefined") { | ||
//2 failed validation cases | ||
if ((!this._not && !result) || (this._not && result)) { | ||
//change error message based on not and if an error message is specified | ||
if (!this._not && typeof this.error_messages[fn] !== "undefined") { | ||
this.errors.push(this.error_messages[fn]); | ||
} else if (this._not && typeof this.error_messages["not_" + fn] !== "undefined") { | ||
this.errors.push(this.error_messages["not_" + fn]); | ||
} else if (this._not) { | ||
this.errors.push("Not " + fn); | ||
} else { | ||
this.errors.push(fn); | ||
} | ||
this.errors.push(fn); | ||
//all of these cases result in non-validity | ||
this.valid = false; | ||
} | ||
//set not back for the next test | ||
this._not = false; | ||
//chain | ||
return this; | ||
@@ -367,0 +456,0 @@ }; |
{ | ||
"name": "iz", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Some basic validation functions that were useful across projects", | ||
@@ -5,0 +5,0 @@ "main": "iz.js", |
@@ -11,2 +11,8 @@ [![Build Status](https://secure.travis-ci.org/parris/iz.png)](http://travis-ci.org/parris/iz) | ||
Change Log: 0.0.3 | ||
---- | ||
- Added equal method | ||
- Added empty method | ||
- Added not() operation | ||
Change Log: 0.0.2 | ||
@@ -22,9 +28,12 @@ ---- | ||
iz(10).between(2, 15).int().multiple(5); | ||
iz(10).between(2, 15).int().multiple(5); //why yes, yes it is | ||
iz(10).between(2, 15).not().between(1, 5).int().multiple(5); // the fancy not operator will cause the opposite result to happen next... this is also true! | ||
When using the chained notation an object containing an errors{array} and valid{bool} is returned. You could take the | ||
returned object and run more validations on it later as well. This function also accepts an object with error names. | ||
returned object and run more validations on it later as well. This function also accepts an object with error names. If you `.not()` something | ||
then you can provide not_* in the error_messages object to return a custom error. | ||
var errors = { | ||
between: "Is not between", | ||
between: "Is not between, please fix", | ||
not_between: "Value must be between!", | ||
int: "Not an int!!!", | ||
@@ -39,3 +48,3 @@ multiple: "This is terrible and you should fix it" | ||
Possible validations so far. All return true or false. The comment next to each is the true case: | ||
Possible validations so far (true case in comments): | ||
@@ -47,5 +56,7 @@ iz.alphaNumeric(*); // Is number or string(contains only numbers or strings) | ||
iz.cc(*); // Luhn checksum approved value | ||
iz.date(*); // Is a data obj or is a string that is easily converted to a date | ||
iz.date(*); // Is a date obj or is a string that is easily converted to a date | ||
iz.decimal(*); // Contains 1 decimal point and potentially can have a - at the beginning | ||
iz.email(*); // Seems like a valid email address | ||
iz.empty(*); // If an object, array or function contains no properties true. All primitives return true. | ||
iz.equal(*, *); // Any 2 things are strictly equal. If 2 objects their internal properties will be checked. If the first parameter has an equals method that will be run instead | ||
iz.extension(ob1, ob2); // If obj2's methods are all found in obj1 | ||
@@ -73,10 +84,7 @@ iz.fileExtension(value, arr); // Checks if the extension of value is in arr. An obj can be provide, but must have indexOf defined. | ||
- Equality: There is no guarantee of an equalTo method for objects and even if there were object equality can mean a variety of things. The 'primitives' are easily compared, which makes this check sort of useless. If this catches on maybe I'll enforce equalTo methods on objects? | ||
- Uniqueness: I would LOVE to check for uniqueness; however, since that implementation is dependant on environment it is not possible within the scope of this package. Also it is non-trivial in EVERY case to solve async type requests synchronously. I am working on uniqueness currently in mongo and I'll post a gist or something eventually. | ||
- File: Requires async not ready yet to do that yet. | ||
- ExpDate: Just make it so an old date can't be entered. You can't validate this number until the bank reports a failure anyways. | ||
- In depth email address regex: Not really possible it seems. You can either write some really complicated regex that will likely pass 99.9% of things or write something simple that will pass everything with the @ symbol. I choose the later. The other option was to ask the ISPs. Once again async is required. Not ready for that yet. Also some ISPs have blocked those features (sbcglobal for instance). Just make the user "confirm" their email address. That should be your validation. | ||
- Money: The scope is just too large. I started making this and realized there are about 50x ways to skin this. If you have ideas I'd love to hear them. I started doing it by locale then realized that was too limiting. Then I had about 7 "modes" and an optional regex, which also sucked. I think I'll settle on it depends too much on specification and as such it shouldn't be part of a library. | ||
- Empty: Underscore's implementation is awesome :). | ||
- URL: No real non-crazy regex exists. Checking for http:// at the front is lame, why force your user to type that in? The alternative is AJAX. | ||
- Uniqueness: I may eventually write some sort of interface for uniqueness checks within some db, but for now this is non-trivial. First up would be mongodb. | ||
- File: Not sure what the scope should be yet. Mime types? Existence on the web? | ||
- Email (more in depth): Right now we check for the @ symbol. This accepts all email address. Some more hard regex would be cool, but a real valid email regex is overly complicated and doesn't accept everything. The other option is an in depth check with an email provider (sbcglobal comes to mind). | ||
- Money: The scope is really large. Thinking about having localized settings. Perhaps specifying some simple format. Not sure yet! | ||
- URL: No real non-crazy regex exists. Checking for http:// at the front is lame, why force your user to type that in? | ||
@@ -87,5 +95,4 @@ Did I miss a validation? Send me a message or a pull request. | ||
---- | ||
- There is a ton of "checking" done, but the library doesn't expose calculated values (even though it finds them). For example the library doesn't tell you what type something is, it simply tells you if the type matches some string. It might be useful to provide both checking methods and value methods. | ||
- I may expand the scope of this project to have client/server side validations for mongo. | ||
- It may be cool to define locals since some of the functions depend on it. Then for each locale have different tests set-up. I'll wait till this gains some more steam or I have a need for such a system. | ||
- A ton of "checking" done, but the library doesn't expose calculated values (even though it finds them). For example the library doesn't tell you what type something is, it simply tells you if the type matches some string. It might be useful to provide checking methods along with calculation and sanitization. | ||
- Getters could be used instead of the "not()" function. All it does is set _not to true and then return Iz; however, the check done to see if getters are available in the environment is the same check that would need to be done when running the validations. Since we are trying to make this tool relatively cross-platform I decided to omit this functionality. | ||
@@ -99,2 +106,5 @@ Installation | ||
Client side: simply include bin/iz-latest-min.js (if you are feeling bold) or a specific version. iz.js in the root directory is un-minified and could also be useful for debugging. | ||
Client side: | ||
Simply include bin/iz-latest-min.js or a specific version. iz.js in the root directory is un-minified and could also be useful for debugging. I will keep all previous releases in the bin directory UNTIL it no longer becomes managable. So if you use some specific version make sure to host it client side. | ||
We will maintain backwards compatibility between 0.0.0 releases although new features will definitely be added. |
/*global describe, it, xit, xdescribe, before, require */ | ||
/*jshint expr:true*/ | ||
@@ -41,2 +42,21 @@ var iz = require("../iz"); | ||
}); | ||
it("allows to .not validations", function () { | ||
iz(5).not().between(10, 20).valid.should.be.ok; | ||
iz(5).not().email().valid.should.be.ok; | ||
iz("bob@yahoo").not().email().valid.should.not.be.ok; | ||
}); | ||
it("accepts errors for .not'd validations", function () { | ||
var error_messages = { | ||
not_between: "Your value needs to not be between!" | ||
}; | ||
iz(5, error_messages).not().between(4, 6).errors.should.include(error_messages.not_between); | ||
iz(5).not().between(4, 6).errors.should.include("Not between"); | ||
}); | ||
it("allows switching between .not and normal validations", function () { | ||
iz(5).between(4, 6).not().ip().int().valid.should.be.ok; | ||
}); | ||
}); | ||
@@ -82,3 +102,3 @@ | ||
iz.boolean("*").should.not.be.ok; | ||
iz.boolean(/[ -]/g).should.not.be.ok | ||
iz.boolean(/[ \-]/g).should.not.be.ok; | ||
}); | ||
@@ -153,2 +173,42 @@ | ||
it("can validate that something is empty", function () { | ||
iz.empty([]).should.be.ok; | ||
iz.empty({}).should.be.ok; | ||
iz.empty(function () {}).should.be.ok; | ||
iz.empty(true).should.be.ok; | ||
iz.empty(5).should.be.ok; | ||
iz.empty({bob: true}).should.not.be.ok; | ||
iz.empty(["hi"]).should.not.be.ok; | ||
}); | ||
it("can validate that 2 things are strictly equal", function () { | ||
var obj1 = { | ||
bob: "cheese", | ||
equals : function(obj2) { | ||
if (this.bob === obj2.bob) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
}, | ||
//equals method can be different and it will still match | ||
obj2 = { | ||
bob: "cheese", | ||
equals : function () {} | ||
}, | ||
//don't even need the equals method | ||
obj3 = { | ||
bob: "cheese" | ||
}, | ||
obj4 = { | ||
bob: "pizza" | ||
}; | ||
iz.equal("bob", "bob").should.be.ok; | ||
iz.equal({},{}).should.be.ok; | ||
iz.equal({},{"bob": true}).should.be.ok; | ||
iz.equal(obj1, obj2).should.be.ok; | ||
iz.equal(obj1, obj3).should.be.ok; | ||
iz.equal(obj1, obj4).should.not.be.ok; | ||
}); | ||
it("can validate that an object is an extension of another object", function () { | ||
@@ -237,3 +297,3 @@ iz.extension({},"5").should.not.be.ok; | ||
iz.ip("0300.0000.0002.0353").should.not.be.ok; | ||
iz.ip("0xC00002EB").should.not.be.ok | ||
iz.ip("0xC00002EB").should.not.be.ok; | ||
iz.ip("3221226219").should.not.be.ok; | ||
@@ -240,0 +300,0 @@ iz.ip("030000001353").should.not.be.ok; |
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
64945
13
768
104