Comparing version 0.1.3 to 0.1.4
@@ -27,5 +27,5 @@ /// <reference path="../node.d.ts" /> | ||
/** | ||
* Check if a value is an hexadecimal string | ||
* (letters from A to F and numbers). | ||
**/ | ||
* Check if a value is an hexadecimal string | ||
* (letters from A to F and numbers). | ||
**/ | ||
function hexString(message) { | ||
@@ -38,5 +38,5 @@ if (typeof message === "undefined") { message = 'not-hex-string'; } | ||
/** | ||
* Check if a value is an e-mail address | ||
* (simple checking, works 99%). | ||
**/ | ||
* Check if a value is an e-mail address | ||
* (simple checking, works 99%). | ||
**/ | ||
function email(message) { | ||
@@ -49,4 +49,4 @@ if (typeof message === "undefined") { message = 'not-valid-email'; } | ||
/** | ||
* Check if it's a valid IPv4 address. | ||
**/ | ||
* Check if it's a valid IPv4 address. | ||
**/ | ||
function ipv4(message) { | ||
@@ -60,1 +60,31 @@ if (typeof message === "undefined") { message = 'not-valid-ipv4'; } | ||
/** | ||
* Check if it's a valid MAC address. | ||
**/ | ||
function mac(message) { | ||
if (typeof message === "undefined") { message = 'not-valid-mac'; } | ||
var p = "[0-9a-f]{1,2}"; | ||
var s = "[\\.:]"; | ||
return exports.match("^" + [p,p,p,p,p,p].join(s) + "$", "i", message); | ||
} | ||
exports.mac = mac; | ||
/** | ||
* Check if it's a valid UUID version 3 (MD5 hash). | ||
* http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_3_.28MD5_hash.29 | ||
**/ | ||
function uuid3(message) { | ||
if (typeof message === "undefined") { message = 'not-valid-uuid3'; } | ||
return exports.match("^[a-f0-9]{8}\-[a-f0-9]{4}\-3[a-f0-9]{3}\-[89ab][a-f0-9]{3}\-[a-f0-9]{12}$", "i", message); | ||
} | ||
exports.uuid3 = uuid3; | ||
/** | ||
* Check if it's a valid UUID version 4 (random). | ||
* http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29 | ||
**/ | ||
function uuid4(message) { | ||
if (typeof message === "undefined") { message = 'not-valid-uuid4'; } | ||
return exports.match("^[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-[89ab][a-f0-9]{3}\-[a-f0-9]{12}$", "i", message); | ||
} | ||
exports.uuid4 = uuid4; |
@@ -8,7 +8,7 @@ /// <reference path="../node.d.ts" /> | ||
/** | ||
* Check if a value matches a given pattern. | ||
* You can define a pattern string and regex | ||
* modifiers or just send the RegExp object | ||
* as 1st argument. | ||
**/ | ||
* Check if a value matches a given pattern. | ||
* You can define a pattern string and regex | ||
* modifiers or just send the RegExp object | ||
* as 1st argument. | ||
**/ | ||
export function match(pattern: RegExp, message?: string): enforce.IValidator; | ||
@@ -33,5 +33,5 @@ export function match(pattern: string, modifiers?: string, message?: string): enforce.IValidator; | ||
/** | ||
* Check if a value is an hexadecimal string | ||
* (letters from A to F and numbers). | ||
**/ | ||
* Check if a value is an hexadecimal string | ||
* (letters from A to F and numbers). | ||
**/ | ||
export function hexString(message: string = 'not-hex-string'): enforce.IValidator { | ||
@@ -42,5 +42,5 @@ return match("^[a-f0-9]+$", "i", message); | ||
/** | ||
* Check if a value is an e-mail address | ||
* (simple checking, works 99%). | ||
**/ | ||
* Check if a value is an e-mail address | ||
* (simple checking, works 99%). | ||
**/ | ||
export function email(message: string = 'not-valid-email') { | ||
@@ -51,4 +51,4 @@ return match("^[a-z0-9\\._%\\+\\-]+@[a-z0-9\\.\\-]+\\.[a-z]{2,6}$", "i", message); | ||
/** | ||
* Check if it's a valid IPv4 address. | ||
**/ | ||
* Check if it's a valid IPv4 address. | ||
**/ | ||
export function ipv4(message: string = 'not-valid-ipv4'): enforce.IValidator { | ||
@@ -58,2 +58,27 @@ var p1 = "([1-9]|1[0-9][0-9]?|2[0-4][0-9]|25[0-4])"; | ||
return match("^" + [p1, p2, p2, p1].join("\\.") + "$", "", message); | ||
} | ||
} | ||
/** | ||
* Check if it's a valid MAC address. | ||
**/ | ||
export function mac(message: string = 'not-valid-mac'): enforce.IValidator { | ||
var p = "[0-9a-f]{1,2}"; | ||
var s = "[\\.:]"; | ||
return match("^" + [p, p, p, p, p, p].join(s) + "$", "i", message); | ||
} | ||
/** | ||
* Check if it's a valid UUID version 3 (MD5 hash). | ||
* http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_3_.28MD5_hash.29 | ||
**/ | ||
export function uuid3(message: string = 'not-valid-uuid3'): enforce.IValidator { | ||
return exports.match("^[a-f0-9]{8}\-[a-f0-9]{4}\-3[a-f0-9]{3}\-[89ab][a-f0-9]{3}\-[a-f0-9]{12}$", "i", message); | ||
} | ||
/** | ||
* Check if it's a valid UUID version 4 (random). | ||
* http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29 | ||
**/ | ||
export function uuid4(message: string = 'not-valid-uuid3'): enforce.IValidator { | ||
return exports.match("^[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-[89ab][a-f0-9]{3}\-[a-f0-9]{12}$", "i", message); | ||
} |
@@ -13,3 +13,3 @@ { | ||
], | ||
"version" : "0.1.3", | ||
"version" : "0.1.4", | ||
"license" : "MIT", | ||
@@ -16,0 +16,0 @@ "repository" : "http://github.com/dresende/node-enforce.git", |
@@ -185,1 +185,133 @@ var should = require("should"); | ||
}); | ||
describe("enforce.patterns.mac()", function () { | ||
var validator = enforce.patterns.mac(); | ||
it("should pass '0.1.2.3.4.5'", function (done) { | ||
validator.validate('0.1.2.3.4.5', common.checkValidation(done)); | ||
}); | ||
it("should pass '01:23:45:67:89:aB'", function (done) { | ||
validator.validate('01:23:45:67:89:ab', common.checkValidation(done)); | ||
}); | ||
it("should pass '00:1:22:3:44:5'", function (done) { | ||
validator.validate('00:1:22:3:44:5', common.checkValidation(done)); | ||
}); | ||
it("should not pass '000:11:22:33:44:55'", function (done) { | ||
validator.validate('000:11:22:33:44:55', common.checkValidation(done, 'not-valid-mac')); | ||
}); | ||
it("should not pass 'aa:bb:dd:ee:ff:gg'", function (done) { | ||
validator.validate('aa:bb:dd:ee:ff:gg', common.checkValidation(done, 'not-valid-mac')); | ||
}); | ||
it("should not pass 'aa:bb::cc:dd:ee'", function (done) { | ||
validator.validate('aa:bb::cc:dd:ee', common.checkValidation(done, 'not-valid-mac')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-valid-mac')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-valid-mac')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.patterns.mac('custom-error'); | ||
it("should not pass '0.1.2.3' with 'custom-error'", function (done) { | ||
validator.validate('0.1.2.3', common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); | ||
describe("enforce.patterns.uuid3()", function () { | ||
var validator = enforce.patterns.uuid3(); | ||
it("should pass '6ba7b810-9dad-31d1-80b4-00c04fd430c8'", function (done) { | ||
validator.validate('6ba7b810-9dad-31d1-80b4-00c04fd430c8', common.checkValidation(done)); | ||
}); | ||
it("should not pass '6ba7b810-9dad-11d1-80b4-00c04fd430c8'", function (done) { | ||
validator.validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8', common.checkValidation(done, 'not-valid-uuid3')); | ||
}); | ||
it("should not pass '6ba7b810-9dad-31d1-70b4-00c04fd430c8'", function (done) { | ||
validator.validate('6ba7b810-9dad-31d1-70b4-00c04fd430c8', common.checkValidation(done, 'not-valid-uuid3')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-valid-uuid3')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-valid-uuid3')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.patterns.uuid3('custom-error'); | ||
it("should not pass '6ba7b810-9dad-11d1-80b4-00c04fd430c8' with 'custom-error'", function (done) { | ||
validator.validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8', common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); | ||
describe("enforce.patterns.uuid4()", function () { | ||
var validator = enforce.patterns.uuid4(); | ||
it("should pass '6ba7b810-9dad-41d1-80b4-00c04fd430c8'", function (done) { | ||
validator.validate('6ba7b810-9dad-41d1-80b4-00c04fd430c8', common.checkValidation(done)); | ||
}); | ||
it("should not pass '6ba7b810-9dad-11d1-80b4-00c04fd430c8'", function (done) { | ||
validator.validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8', common.checkValidation(done, 'not-valid-uuid4')); | ||
}); | ||
it("should not pass '6ba7b810-9dad-31d1-70b4-00c04fd430c8'", function (done) { | ||
validator.validate('6ba7b810-9dad-31d1-70b4-00c04fd430c8', common.checkValidation(done, 'not-valid-uuid4')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-valid-uuid4')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-valid-uuid4')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.patterns.uuid4('custom-error'); | ||
it("should not pass '6ba7b810-9dad-11d1-80b4-00c04fd430c8' with 'custom-error'", function (done) { | ||
validator.validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8', common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); |
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
130642
2939
0