-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from shimataro/develop
version 0.14.0
- Loading branch information
Showing
57 changed files
with
3,019 additions
and
2,905 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
{ | ||
"comments": false, | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
}, | ||
"exclude": [ | ||
"transform-regenerator" | ||
], | ||
"useBuiltIns": true | ||
} | ||
] | ||
["@babel/preset-env", {"targets": {"node": "current"}, "useBuiltIns": "usage"}] | ||
], | ||
"plugins": [ | ||
"transform-decorators-legacy" | ||
["@babel/plugin-proposal-decorators", {"legacy": true}] | ||
], | ||
"comments": false | ||
"env": { | ||
"esm": { | ||
"presets": [ | ||
["@babel/preset-env", {"targets": {"node": "current"}, "useBuiltIns": "usage", "modules": false}] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,5 @@ Thumbs.db | |
/*.iml | ||
/nbproject/ | ||
/.project | ||
/.vscode/ | ||
/.vscode/** | ||
!/.vscode/tasks.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,4 @@ cache: | |
directories: | ||
- "node_modules" | ||
script: | ||
- "npm test" | ||
- "npm run lint" | ||
- "npm run verify" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"script": "verify", | ||
"group": { | ||
"kind": "test", | ||
"isDefault": true | ||
}, | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"script": "test", | ||
"group": "test", | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"script": "watch", | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
}, | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"script": "build", | ||
"group": "build", | ||
"problemMatcher": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ validate and adjust input values | |
|
||
## Table of Contents | ||
|
||
* [Introduction](#introduction) | ||
* [Install](#install) | ||
* [Loading](#loading) | ||
* [Reference](#reference) | ||
|
@@ -29,13 +30,84 @@ validate and adjust input values | |
|
||
--- | ||
|
||
## Introduction | ||
|
||
All of web applications need handling input parameters, consists of following steps: | ||
* fill default values | ||
* cast types | ||
* validate values | ||
* ...and revise them if needed | ||
|
||
`node-adjuster` does all of them, by compact and highly readable code! | ||
|
||
### example | ||
|
||
```javascript | ||
import adjuster from "adjuster"; | ||
import assert from "assert"; | ||
|
||
const constraints = { // constraints for input | ||
id: adjuster.number().minValue(1), // number, >=1 | ||
name: adjuster.string().maxLength(16, true), // string, max 16 characters (trims if over) | ||
age: adjuster.number().integer(true).minValue(0), // number, integer (trims if decimal), >=0 | ||
email: adjuster.email(), // e-mail | ||
state: adjuster.string().only("active", "inactive"), // string, accepts only "active" and "inactive" | ||
classes: adjuster.numberArray().separatedBy(",").ignoreEachErrors(), // array of number, separated by ",", ignores errors | ||
skills: adjuster.stringArray().separatedBy(",").ignoreEachErrors(), // array of string, separated by ",", ignores errors | ||
credit_card: adjuster.numericString().separatedBy("-").checksum(adjuster.NUMERIC_STRING_CHECKSUM_ALGORITHM.CREDIT_CARD), // numeric string, separated by "-", checks by Luhn algorithm | ||
remote_addr: adjuster.ipv4(), // IPv4 | ||
remote_addr_ipv6: adjuster.ipv6(), // IPv6 | ||
limit: adjuster.number().integer().default(10).minValue(1, true).maxValue(100, true), // number, integer, omittable (sets 10 if omitted), >=1 (sets 1 if less), <=100 (sets 100 if greater) | ||
offset: adjuster.number().integer().default(0).minValue(0, true), // number, integer, omiitable (sets 0 if omited), >=0 (sets 0 if less) | ||
}; | ||
const input = { // input values | ||
id: "1", | ||
name: "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Ciprin Cipriano de la Santísima Trinidad Ruiz y Picasso", | ||
age: 20.5, | ||
email: "[email protected]", | ||
state: "active", | ||
classes: "1,3,abc,4", | ||
skills: "c,c++,javascript,python,,swift,kotlin", | ||
credit_card: "4111-1111-1111-1111", | ||
remote_addr: "127.0.0.1", | ||
remote_addr_ipv6: "::1", | ||
limit: "0", | ||
}; | ||
const expected = { // should be adjusted to this | ||
id: 1, | ||
name: "Pablo Diego José", | ||
age: 20, | ||
email: "[email protected]", | ||
state: "active", | ||
classes: [1, 3, 4], | ||
skills: ["c", "c++", "javascript", "python", "swift", "kotlin"], | ||
credit_card: "4111111111111111", | ||
remote_addr: "127.0.0.1", | ||
remote_addr_ipv6: "::1", | ||
limit: 1, | ||
offset: 0, | ||
}; | ||
|
||
// Let's adjust! | ||
const adjusted = adjuster.adjust(input, constraints); | ||
|
||
// verification | ||
assert.deepStrictEqual(adjusted, expected); | ||
``` | ||
|
||
That's all! No control flows! Isn't it cool? | ||
|
||
For details, see [basic usage](#basic-usage). | ||
|
||
## Install | ||
|
||
install from [npm registry](https://www.npmjs.com/package/adjuster). | ||
```bash | ||
npm install -S adjuster | ||
``` | ||
|
||
NOTE: package name is `adjuster`, NOT `node-adjuster`! | ||
|
||
## Loading | ||
|
||
### using [Babel](https://babeljs.io/) | ||
|
@@ -54,7 +126,11 @@ or | |
var {default: adjuster} = require("adjuster"); | ||
``` | ||
|
||
ES Modules(`.mjs`) is not provided for now. | ||
### ES Modules | ||
```javascript | ||
// foo.mjs - in the same way as Babel! | ||
import adjuster from "adjuster"; | ||
``` | ||
|
||
## Reference | ||
### types and constants | ||
|
@@ -291,6 +367,28 @@ catch (err) { | |
} | ||
``` | ||
|
||
###### error handling 4 | ||
when input value is not an object | ||
|
||
NOTE: `constraint` won't be checked because it's predictable; should be generated by programmer, not an external input | ||
|
||
```javascript | ||
import adjuster from "adjuster"; | ||
import assert from "assert"; | ||
|
||
const constraints = {}; | ||
const input = 123; | ||
|
||
try { | ||
// `input` must be an object | ||
const adjusted = adjuster.adjust(input, constraints); | ||
} | ||
catch (err) { | ||
assert.strictEqual(err.key, null); | ||
assert.strictEqual(err.cause, adjuster.CAUSE.NOT_OBJECT); | ||
} | ||
``` | ||
|
||
### number | ||
#### ambient declarations | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.