Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enforce

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enforce - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

38

lib/enforcements/patterns.js

@@ -58,2 +58,40 @@ /// <reference path="../node.d.ts" />

/**
* 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.

@@ -60,0 +98,0 @@ **/

@@ -57,2 +57,37 @@ /// <reference path="../node.d.ts" />

/**
* 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.

@@ -59,0 +94,0 @@ **/

2

package.json

@@ -13,3 +13,3 @@ {

],
"version" : "0.1.4",
"version" : "0.1.5",
"license" : "MIT",

@@ -16,0 +16,0 @@ "repository" : "http://github.com/dresende/node-enforce.git",

@@ -71,2 +71,8 @@ ## Data Validations [![Build Status](https://secure.travis-ci.org/dresende/node-enforce.png?branch=master)](http://travis-ci.org/dresende/node-enforce)

### Same as
`enforce.sameAs(property, [ msg ])`
Checks if a property has the same (strict) value as another one. This is usefull for testing password matching.
### Lists

@@ -164,2 +170,26 @@

#### IPv6
`enforce.patterns.ipv6([ msg ])`
Checks if a property matches a predefined `RegExp` object accepting valid IPv6 address.
#### MAC
`enforce.patterns.mac([ msg ])`
Checks if a property matches a predefined `RegExp` object accepting valid MAC address.
#### UUID v3
`enforce.patterns.uuid3([ msg ])`
Checks if a property matches a predefined `RegExp` object accepting valid UUID version 3.
#### UUID v4
`enforce.patterns.uuid4([ msg ])`
Checks if a property matches a predefined `RegExp` object accepting valid UUID version 4.
## Chaining

@@ -193,2 +223,2 @@ `Enforce` supports chaining operations on all `Validator` objects, these allow you to add additional common

Only proceedes to check the `validation` if the property's value is not of the specified type. Checked with a `typeof value != type` operation.
Only proceedes to check the `validation` if the property's value is not of the specified type. Checked with a `typeof value != type` operation.

@@ -26,3 +26,23 @@ var should = require("should");

});
it("should have .ipv6()", function (done) {
enforce.patterns.ipv6.should.be.a("function");
return done();
});
it("should have .mac()", function (done) {
enforce.patterns.mac.should.be.a("function");
return done();
});
it("should have .uuid3()", function (done) {
enforce.patterns.uuid3.should.be.a("function");
return done();
});
it("should have .uuid4()", function (done) {
enforce.patterns.uuid4.should.be.a("function");
return done();
});
describe("with custom error", function () {

@@ -187,2 +207,62 @@ it("should fail 'abc' on /def/ with 'invalid'", function (done) {

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 () {

@@ -189,0 +269,0 @@ var validator = enforce.patterns.mac();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc