Skip to content

Commit

Permalink
version 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shimataro committed May 6, 2018
2 parents 55b9c95 + e70de13 commit b3d7a23
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Thumbs.db
/*.iml
/nbproject/
/.project
/*.code-workspace
68 changes: 57 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ npm install -S adjuster
import adjuster from "adjuster";

// should be OK
adjuster.number().adjust("-123"); // === -123;
adjuster.number().adjust(-123); // === -123;
adjuster.number().in(1, 3, 5).adjust(1); // === 1

// should be adjusted
adjuster.number().adjust("-123"); // === -123;
adjuster.number().default(10).adjust(undefined); // === 10
adjuster.number().allowEmpty(123).adjust(""); // === 123
adjuster.number().minValue(1, true).adjust(0); // === 1
Expand All @@ -43,11 +44,12 @@ adjuster.number().maxValue(100).adjust(101); // throws AdjusterError; err.cause
import adjuster from "adjuster";

// should be OK
adjuster.string().adjust(123); // === "123"
adjuster.string().adjust("123"); // === "123"
adjuster.string().allowEmpty("xyz").adjust(""); // === "xyz"
adjuster.string().in("eat", "sleep", "play").adjust("sleep"); // === "sleep"

// should be adjusted
adjuster.string().adjust(123); // === "123"
adjuster.string().default("xyz").adjust(undefined); // === "xyz"
adjuster.string().maxLength(5, true).adjust("abcdefg"); // === "abcde"

Expand All @@ -59,25 +61,59 @@ adjuster.string().minLength(5).adjust("a"); // throws AdjusterError; err.cause =
adjuster.string().maxLength(5).adjust("abcdefg"); // throws AdjusterError; err.cause === adjuster.CAUSE.MAX_LENGTH
```

### IPv4

```javascript
import adjuster from "adjuster";

// should be OK
adjuster.ipv4().adjust("0.0.0.0"); // === "0.0.0.0"
adjuster.ipv4().adjust("192.168.0.1"); // === "192.168.0.1"
adjuster.ipv4().adjust("255.255.255.255"); // === "255.255.255.255"

// should cause errors; err.cause === adjuster.CAUSE.IPV4
adjuster.ipv4().adjust("0.0.0.");
adjuster.ipv4().adjust("0.0.0.0.");
adjuster.ipv4().adjust("255.255.255.256");
```

### IPv6

```javascript
import adjuster from "adjuster";

// should be OK
adjuster.ipv6().adjust("0000:0000:0000:0000:0000:0000:0000:0000"); // === "0000:0000:0000:0000:0000:0000:0000:0000"
adjuster.ipv6().adjust("::1"); // === "::1"
adjuster.ipv6().adjust("::"); // === "::"
adjuster.ipv6().adjust("1::1"); // === "1::1"
adjuster.ipv6().adjust("::ffff:192.0.2.1"); // === "::ffff:192.0.2.1"; IPv4-mapped address

// should cause errors; err.cause === adjuster.CAUSE.IPV6
adjuster.ipv6().adjust("0000");
adjuster.ipv6().adjust("ffff:");
adjuster.ipv6().adjust("0000:0000:0000:0000:0000:0000:0000:0000:");
```

### e-mail

```javascript
import adjuster from "adjuster";

// should be OK
adjuster.email().adjust("user+mailbox/[email protected]"); // dot-string
adjuster.email().adjust("!#$%&'*+-/=?^_`.{|}[email protected]"); // dot-string
adjuster.email().adjust("\"Fred\\\"Bloggs\"@example.com"); // quoted-string
adjuster.email().adjust("\"Joe.\\\\Blow\"@example.com"); // quoted-string
adjuster.email().adjust("[email protected]");
adjuster.email().adjust("[email protected]");
adjuster.email().adjust("user+mailbox/[email protected]"); // === "user+mailbox/[email protected]"; dot-string
adjuster.email().adjust("!#$%&'*+-/=?^_`.{|}[email protected]"); // === "!#$%&'*+-/=?^_`.{|}[email protected]"; dot-string
adjuster.email().adjust("\"Fred\\\"Bloggs\"@example.com"); // === "\"Fred\\\"Bloggs\"@example.com"; quoted-string
adjuster.email().adjust("\"Joe.\\\\Blow\"@example.com"); // === "\"Joe.\\\\Blow\"@example.com"; quoted-string
adjuster.email().adjust("[email protected]"); // === "[email protected]"
adjuster.email().adjust("[email protected]"); // === "[email protected]"

// should cause errors; err.cause === adjuster.CAUSE.EMAIL
adjuster.email().adjust("@example.com");
adjuster.email().adjust("[email protected]");
adjuster.email().adjust("[email protected]");
adjuster.email().adjust("[email protected]");
adjuster.email().adjust("user@example@com");
adjuster.emal().adjust("user@example@com");
adjuster.email().adjust("user-example-com");
adjuster.email().adjust("user@example_domain.com");
adjuster.email().adjust("[email protected]");
Expand All @@ -93,13 +129,17 @@ const inputData = {
name: "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Ciprin Cipriano de la Santísima Trinidad Ruiz y Picasso",
email: "[email protected]",
state: "active",
remote_addr: "127.0.0.1",
remote_addr_ipv6: "::1",
limit: "0",
};
const adjusters = {
id: adjuster.number().minValue(1),
name: adjuster.string().maxLength(16, true),
email: adjuster.email(),
state: adjuster.string().in("active", "inactive"),
remote_addr: adjuster.ipv4(),
remote_addr_ipv6: adjuster.ipv6(),
limit: adjuster.number().default(10).minValue(1, true).maxValue(100, true),
offset: adjuster.number().default(0).minValue(0, true),
};
Expand All @@ -108,6 +148,8 @@ const expected = {
name: "Pablo Diego José",
email: "[email protected]",
state: "active",
remote_addr: "127.0.0.1",
remote_addr_ipv6: "::1",
limit: 1,
offset: 0,
};
Expand All @@ -118,19 +160,23 @@ const adjusted = adjuster.adjustData(inputData, adjusters);

## Release notes

* 2018/05/06 *version 0.4.0*
* New Functions
* `adjuster.ipv4()`
* `adjuster.ipv6()`
* Change Specifications
* strict IPv4 and IPv6 validation for `adjuster.email()`
* 2018/04/22 *version 0.3.0*
* Bugfixes
* quoted-pair of email
* import error in `EmailAdjuster.es`
* Change Specifications
* limit the length of local/domain part of email

* 2018/04/21 *version 0.2.0*
* Bugfixes
* test error on npm@5
* Change Specifications
* enable to specify value to `allowEmpty()`
* support IPv6 domain for `EmailAdjuster`

* 2018/04/18 *version 0.1.0*
* First release.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adjuster",
"description": "validate and adjust input values",
"version": "0.3.0",
"version": "0.4.0",
"author": "shimataro",
"license": "MIT",
"repository": {
Expand Down
12 changes: 12 additions & 0 deletions src/index.es
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {CAUSE} from "./libs/constants";
import adjustData from "./libs/adjustData";
import NumberAdjuster from "./libs/NumberAdjuster";
import StringAdjuster from "./libs/StringAdjuster";
import IPv4Adjuster from "./libs/IPv4Adjuster";
import IPv6Adjuster from "./libs/IPv6Adjuster";
import EmailAdjuster from "./libs/EmailAdjuster";

export default {
Expand All @@ -20,6 +22,16 @@ export default {
{
return new StringAdjuster();
},
/** @return {IPv4Adjuster} */
ipv4: () =>
{
return new IPv4Adjuster();
},
/** @return {IPv6Adjuster} */
ipv6: () =>
{
return new IPv6Adjuster();
},
/** @return {EmailAdjuster} */
email: () =>
{
Expand Down
54 changes: 25 additions & 29 deletions src/libs/EmailAdjuster.es
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {CAUSE} from "./constants";
import {PATTERN as PATTERN_IPV4} from "./IPv4Adjuster";
import {PATTERN as PATTERN_IPV6} from "./IPv6Adjuster";

import AdjusterInterface from "./AdjusterInterface";
import AdjusterError from "./AdjusterError";
import StringAdjuster from "./StringAdjuster";
Expand All @@ -9,34 +12,27 @@ const MAX_LENGTH = MAX_LENGTH_LOCAL + 1 + MAX_LENGTH_DOMAIN; // local-part + "@"

// https://tools.ietf.org/html/rfc5321
// https://tools.ietf.org/html/rfc5322
const REGEXP_CHARSET_DOT = "[\\w!#$%&'*+\\-\\/=?^`{|}~]";
const REGEXP_CHARSET_QUOTED = "[\\w!#$%&'*+\\-\\/=?^`{|}~. ()<>\\[\\]:;@,]";
const REGEXP_CHARSET_TLD = "[a-zA-Z]";
const REGEXP_CHARSET_SLD = "[a-zA-Z\\d\\-]";
const REGEXP_CHARSET_IPV4 = "\\d";
const REGEXP_CHARSET_IPV6 = "[\\da-fA-F]";

const REGEXP_COMPONENT_DOT = `${REGEXP_CHARSET_DOT}+`;
const REGEXP_COMPONENT_QUOTED = `(${REGEXP_CHARSET_QUOTED}|\\\\[\\\\"])+`;
const REGEXP_COMPONENT_TLD = `${REGEXP_CHARSET_TLD}+`;
const REGEXP_COMPONENT_SLD = `${REGEXP_CHARSET_SLD}+`;
const REGEXP_COMPONENT_IPV4 = `${REGEXP_CHARSET_IPV4}{1,3}`;
const REGEXP_COMPONENT_IPV6 = `${REGEXP_CHARSET_IPV6}{0,4}`;

const REGEXP_LOCAL_DOT = `${REGEXP_COMPONENT_DOT}(\\.${REGEXP_COMPONENT_DOT})*`;
const REGEXP_LOCAL_QUOTED = `"${REGEXP_COMPONENT_QUOTED}"`;
const REGEXP_LOCAL = `(${REGEXP_LOCAL_DOT}|${REGEXP_LOCAL_QUOTED})`;

const REGEXP_DOMAIN_GENERAL = `(${REGEXP_COMPONENT_SLD}\\.)+${REGEXP_COMPONENT_TLD}`;
const REGEXP_DOMAIN_IPV4 = `${REGEXP_COMPONENT_IPV4}(\\.${REGEXP_COMPONENT_IPV4}){3}`;
const REGEXP_DOMAIN_IPV6_COMMON = `${REGEXP_COMPONENT_IPV6}(:${REGEXP_COMPONENT_IPV6}){1,5}`;
const REGEXP_DOMAIN_IPV6_REST = `(:${REGEXP_DOMAIN_IPV4}|(:${REGEXP_COMPONENT_IPV6}){0,2})`;
const REGEXP_DOMAIN_IPV6 = `IPv6:${REGEXP_DOMAIN_IPV6_COMMON}${REGEXP_DOMAIN_IPV6_REST}`;
const REGEXP_DOMAIN_IP = `\\[(${REGEXP_DOMAIN_IPV4}|${REGEXP_DOMAIN_IPV6})\\]`;
const REGEXP_DOMAIN = `(${REGEXP_DOMAIN_GENERAL}|${REGEXP_DOMAIN_IP})`;

const REGEXP_EMAIL = `^${REGEXP_LOCAL}@${REGEXP_DOMAIN}$`;
const PATTERN = new RegExp(REGEXP_EMAIL);
const PATTERN_CHARSET_DOT = "[\\w!#$%&'*+\\-\\/=?^`{|}~]";
const PATTERN_CHARSET_QUOTED = "[\\w!#$%&'*+\\-\\/=?^`{|}~. ()<>\\[\\]:;@,]";
const PATTERN_CHARSET_TLD = "[a-zA-Z]";
const PATTERN_CHARSET_SLD = "[a-zA-Z\\d\\-]";

const PATTERN_COMPONENT_DOT = `${PATTERN_CHARSET_DOT}+`;
const PATTERN_COMPONENT_QUOTED = `(${PATTERN_CHARSET_QUOTED}|\\\\[\\\\"])+`;
const PATTERN_COMPONENT_TLD = `${PATTERN_CHARSET_TLD}+`;
const PATTERN_COMPONENT_SLD = `${PATTERN_CHARSET_SLD}+`;

const PATTERN_LOCAL_DOT = `${PATTERN_COMPONENT_DOT}(\\.${PATTERN_COMPONENT_DOT})*`;
const PATTERN_LOCAL_QUOTED = `"${PATTERN_COMPONENT_QUOTED}"`;
const PATTERN_LOCAL = `(${PATTERN_LOCAL_DOT}|${PATTERN_LOCAL_QUOTED})`;

const PATTERN_DOMAIN_GENERAL = `(${PATTERN_COMPONENT_SLD}\\.)+${PATTERN_COMPONENT_TLD}`;
const PATTERN_DOMAIN_IP = `\\[(${PATTERN_IPV4}|IPv6:${PATTERN_IPV6})\\]`;
const PATTERN_DOMAIN = `(${PATTERN_DOMAIN_GENERAL}|${PATTERN_DOMAIN_IP})`;

const PATTERN = `${PATTERN_LOCAL}@${PATTERN_DOMAIN}`;

const REGEXP = new RegExp(`^${PATTERN}$`);

/**
* adjuster for e-mail
Expand All @@ -52,7 +48,7 @@ export default class EmailAdjuster extends AdjusterInterface

this._objAdjuster = new StringAdjuster()
.maxLength(MAX_LENGTH)
.pattern(PATTERN);
.pattern(REGEXP);
}

/**
Expand Down
72 changes: 72 additions & 0 deletions src/libs/IPv4Adjuster.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {CAUSE} from "./constants";

import AdjusterInterface from "./AdjusterInterface";
import StringAdjuster from "./StringAdjuster";

const PATTERN_COMPONENT = `(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})`;
const PATTERN = `${PATTERN_COMPONENT}(\\.${PATTERN_COMPONENT}){3}`;

const REGEXP = new RegExp(`^${PATTERN}$`);

export {PATTERN};

/**
* adjuster for IPv4
*/
export default class IPv4Adjuster extends AdjusterInterface
{
/**
* constructor
*/
constructor()
{
super();

this._objAdjuster = new StringAdjuster()
.pattern(REGEXP);
}

/**
* set default value; enable to omit
* @param {string} value default value
* @return {IPv4Adjuster}
*/
default(value)
{
this._objAdjuster.default(value);
return this;
}

/**
* allow empty string (NOT undefined)
* @param {?string} [value=null] value on empty
* @return {IPv4Adjuster}
*/
allowEmpty(value = null)
{
this._objAdjuster.allowEmpty(value);
return this;
}

/**
* do adjust
* @param {*} value value to be checked
* @param {?_OnError} onError callback function on error
* @return {string} adjusted value
*/
adjust(value, onError = null)
{
try
{
return this._objAdjuster.adjust(value);
}
catch(err)
{
if(err.cause === CAUSE.PATTERN)
{
err.cause = CAUSE.IPV4;
}
return AdjusterInterface._handleError(onError, err.cause, value);
}
}
}
Loading

0 comments on commit b3d7a23

Please sign in to comment.