Browse files

Adds enforce.patterns.ipv6()

  • Loading branch information...
1 parent b5249b9 commit ded73796304aabae01eaaf61c4dfe52a2b6a8b1c @dresende committed Apr 26, 2014
Showing with 144 additions and 0 deletions.
  1. +6 −0 Readme.md
  2. +38 −0 lib/enforcements/patterns.js
  3. +35 −0 lib/enforcements/patterns.ts
  4. +65 −0 test/integration/patterns.js
View
6 Readme.md
@@ -166,6 +166,12 @@ Checks if a property matches a predefined `RegExp` object accepting valid e-mail
Checks if a property matches a predefined `RegExp` object accepting valid IPv4 address.
+#### IPv6
+
+`enforce.patterns.ipv6([ msg ])`
+
+Checks if a property matches a predefined `RegExp` object accepting valid IPv6 address.
+
#### MAC
`enforce.patterns.mac([ msg ])`
View
38 lib/enforcements/patterns.js
@@ -55,6 +55,44 @@ function ipv4(message) {
exports.ipv4 = ipv4;
/**
+ * Check if it's a valid IPv6 address.
+ **/
+function ipv6(message) {
+ if (typeof message === "undefined") { message = 'not-valid-ipv6'; }
+
+ var p1 = new RegExp("^([a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$", "i");
+ var p2 = new RegExp("^([a-f0-9]{1,4}:)*[a-f0-9]{1,4}$", "i");
+
+ return new Validator(function (value, next) {
+ if (typeof value != "string") {
+ return next(message);
+ }
+ // unspecified / loopback
+ if ([ "::", "::1" ].indexOf(value) >= 0) {
+ return next();
+ }
+ // normal address (with possible leading zeroes omitted)
+ if (value.match(p1)) {
+ return next();
+ }
+ if (value.indexOf("::") == -1) {
+ return next(message);
+ }
+ // grouping zeroes
+ var g = value.split("::");
+
+ if (g.length != 2) {
+ return next(message);
+ }
+ if (!g[0].match(p2) || !g[1].match(p2)) {
+ return next(message);
+ }
+ return next();
+ });
+}
+exports.ipv6 = ipv6;
+
+/**
* Check if it's a valid MAC address.
**/
function mac(message) {
View
35 lib/enforcements/patterns.ts
@@ -54,6 +54,41 @@ export function ipv4(message: string = 'not-valid-ipv4'): enforce.IValidator {
}
/**
+ * Check if it's a valid IPv6 address.
+ **/
+export function ipv6(message: string = 'not-valid-ipv6'): enforce.IValidator {
+ var p1 = new RegExp("^([a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$", "i");
+ var p2 = new RegExp("^([a-f0-9]{1,4}:)*[a-f0-9]{1,4}$", "i");
+
+ return new Validator(function (value, next) {
+ if (typeof value != "string") {
+ return next(message);
+ }
+ // unspecified / loopback
+ if ([ "::", "::1" ].indexOf(value) >= 0) {
+ return next();
+ }
+ // normal address (with possible leading zeroes omitted)
+ if (value.match(p1)) {
+ return next();
+ }
+ if (value.indexOf("::") == -1) {
+ return next(message);
+ }
+ // grouping zeroes
+ var g = value.split("::");
+
+ if (g.length != 2) {
+ return next(message);
+ }
+ if (!g[0].match(p2) || !g[1].match(p2)) {
+ return next(message);
+ }
+ return next();
+ });
+}
+
+/**
* Check if it's a valid MAC address.
**/
export function mac(message: string = 'not-valid-mac'): enforce.IValidator {
View
65 test/integration/patterns.js
@@ -23,6 +23,11 @@ describe("enforce.patterns", function () {
return done();
});
+ it("should have .ipv6()", function (done) {
+ enforce.patterns.ipv4.should.be.a("function");
+
+ return done();
+ });
describe("with custom error", function () {
it("should fail 'abc' on /def/ with 'invalid'", function (done) {
@@ -183,6 +188,66 @@ describe("enforce.patterns.ipv4()", function () {
});
});
+describe("enforce.patterns.ipv6()", function () {
+ var validator = enforce.patterns.ipv6();
+
+ it("should pass '2001:0db8:85a3:0000:0000:8a2e:0370:7334'", function (done) {
+ validator.validate('2001:0db8:85a3:0000:0000:8a2e:0370:7334', common.checkValidation(done));
+ });
+
+ it("should pass '2001:db8:85a3:0:0:8a2e:370:7334'", function (done) {
+ validator.validate('2001:db8:85a3:0:0:8a2e:370:7334', common.checkValidation(done));
+ });
+
+ it("should pass '2001:db8:85a3::8a2e:370:7334'", function (done) {
+ validator.validate('2001:db8:85a3::8a2e:370:7334', common.checkValidation(done));
+ });
+
+ it("should pass '::'", function (done) {
+ validator.validate('::', common.checkValidation(done));
+ });
+
+ it("should pass '::1'", function (done) {
+ validator.validate('::1', common.checkValidation(done));
+ });
+
+ it("should not pass '::0'", function (done) {
+ validator.validate('::0', common.checkValidation(done, 'not-valid-ipv6'));
+ });
+
+ it("should not pass '2001:db8:85a3:8d3:1319:8a2e:370'", function (done) {
+ validator.validate('2001:db8:85a3:8d3:1319:8a2e:370', common.checkValidation(done, 'not-valid-ipv6'));
+ });
+
+ it("should not pass '2001:db8::8d3::8a2e:370:7348'", function (done) {
+ validator.validate('2001:db8::8d3::8a2e:370:7348', common.checkValidation(done, 'not-valid-ipv6'));
+ });
+
+ it("should not pass null", function (done) {
+ validator.validate(null, common.checkValidation(done, 'not-valid-ipv6'));
+ });
+
+ it("should not pass undefined", function (done) {
+ validator.validate(undefined, common.checkValidation(done, 'not-valid-ipv6'));
+ });
+
+ 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.ipv6('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.mac()", function () {
var validator = enforce.patterns.mac();

0 comments on commit ded7379

Please sign in to comment.