Comparing version 0.0.25 to 0.1.0
250
index.js
@@ -5,7 +5,6 @@ //----------------------------------------------------- | ||
// Site: 666.io | ||
// Version: 0.00.025 | ||
// | ||
//----------------------------------------------------- | ||
var $aigis = (function createInstance() { | ||
var $aigis = (function createInstance(isGlobal) { | ||
"use strict"; | ||
@@ -52,74 +51,20 @@ | ||
var gExport = { | ||
"createInstance": function(isGlobal) { | ||
var r = createInstance(); | ||
"createInstance": createInstance, | ||
r.global(isGlobal); | ||
return r; | ||
}, | ||
//--------]> | ||
"global": function(v) { | ||
if(global && typeof(global) !== "object" || typeof(v) === "undefined") | ||
return this; | ||
"global": setGlobal, | ||
if(v) { | ||
var gTObj = gExport.typenize, | ||
gSObj = gExport.sanitize, | ||
gVObj = gExport.validate; | ||
"type": type, | ||
"rule": rule, | ||
for(var i in gExport) { | ||
if(gExport.hasOwnProperty(i)) | ||
gTObj[i] = gSObj[i] = gVObj[i] = gExport[i]; | ||
} | ||
//--------]> | ||
if(typeof(global.$typenize) === "undefined") | ||
global.$typenize = gTObj; | ||
"typenize": typenize, | ||
"sanitize": sanitize, | ||
"validate": validate, | ||
if(typeof(global.$sanitize) === "undefined") | ||
global.$sanitize = gSObj; | ||
if(typeof(global.$validate) === "undefined") | ||
global.$validate = gVObj; | ||
} else { | ||
if(global.$typenize === gExport.typenize) | ||
delete global.$typenize; | ||
if(global.$sanitize === gExport.sanitize) | ||
delete global.$sanitize; | ||
if(global.$validate === gExport.validate) | ||
delete global.$validate; | ||
} | ||
return this; | ||
}, | ||
"type": function(name, func) { | ||
wFuncStore(name, func, gCustomTypesStore); | ||
return this; | ||
}, | ||
"rule": function(name, func) { | ||
wFuncStore(name, func, gCustomRulesStore); | ||
return this; | ||
}, | ||
//--------]> | ||
"typenize": function(schema, data, options) { | ||
return arguments.length === 1 ? | ||
createModel(schema, gExport.typenize) : runSchema(C_MODE_TYPENIZE, schema, data, options, $typenize, $typenizeHashTable); | ||
}, | ||
"sanitize": function(schema, data, options) { | ||
return arguments.length === 1 ? | ||
createModel(schema, gExport.sanitize) : runSchema(C_MODE_SANITIZE, schema, data, options, $sanitizeString, $sanitizeHashTable); | ||
}, | ||
"validate": function(schema, data, options) { | ||
return arguments.length === 1 ? | ||
createModel(schema, gExport.validate) : runSchema(C_MODE_VALIDATE, schema, data, options, $validateString, $validateHashTable); | ||
} | ||
"mid": mid | ||
}; | ||
@@ -156,2 +101,4 @@ | ||
gExport.global(isGlobal); | ||
//------------------)> | ||
@@ -163,4 +110,164 @@ | ||
function setGlobal(v) { | ||
if(global && typeof(global) !== "object" || typeof(v) === "undefined") | ||
return gExport; | ||
if(v) { | ||
var gTObj = gExport.typenize, | ||
gSObj = gExport.sanitize, | ||
gVObj = gExport.validate; | ||
for(var i in gExport) { | ||
if(gExport.hasOwnProperty(i)) | ||
gTObj[i] = gSObj[i] = gVObj[i] = gExport[i]; | ||
} | ||
if(typeof(global.$typenize) === "undefined") | ||
global.$typenize = gTObj; | ||
if(typeof(global.$sanitize) === "undefined") | ||
global.$sanitize = gSObj; | ||
if(typeof(global.$validate) === "undefined") | ||
global.$validate = gVObj; | ||
} else { | ||
if(global.$typenize === gExport.typenize) | ||
delete global.$typenize; | ||
if(global.$sanitize === gExport.sanitize) | ||
delete global.$sanitize; | ||
if(global.$validate === gExport.validate) | ||
delete global.$validate; | ||
} | ||
return gExport; | ||
} | ||
function type(name, func) { | ||
wFuncStore(name, func, gCustomTypesStore); | ||
return gExport; | ||
} | ||
function rule(name, func) { | ||
wFuncStore(name, func, gCustomRulesStore); | ||
return gExport; | ||
} | ||
//-----)> | ||
function typenize(schema, data, options) { | ||
return arguments.length === 1 ? | ||
createModel(schema, gExport.typenize) : runSchema(C_MODE_TYPENIZE, schema, data, options, $typenize, $typenizeHashTable); | ||
} | ||
function sanitize(schema, data, options) { | ||
return arguments.length === 1 ? | ||
createModel(schema, gExport.sanitize) : runSchema(C_MODE_SANITIZE, schema, data, options, $sanitizeString, $sanitizeHashTable); | ||
} | ||
function validate(schema, data, options) { | ||
return arguments.length === 1 ? | ||
createModel(schema, gExport.validate) : runSchema(C_MODE_VALIDATE, schema, data, options, $validateString, $validateHashTable); | ||
} | ||
//-----)> | ||
function mid(models, permittedMethods) { | ||
if(Array.isArray(permittedMethods)) { | ||
permittedMethods = permittedMethods.map(function(v) { | ||
return v.toUpperCase(); | ||
}); | ||
if(permittedMethods.length == 1) | ||
permittedMethods = permittedMethods[0]; | ||
} else if(typeof(permittedMethods) === "string") | ||
permittedMethods = permittedMethods.toUpperCase(); | ||
else | ||
permittedMethods = undefined; | ||
//------------------]> | ||
return main; | ||
//------------------]> | ||
function main(req, res, next) { | ||
var model, options, | ||
mdlName, mdlData, mdlScenario; | ||
//----------]> | ||
if(!permittedMethods) { | ||
if(!extrFromOther()) extrFromGet(); | ||
} else { | ||
if(permittedMethods === req.method || typeof(permittedMethods) !== "string" && permittedMethods.indexOf(req.method) != -1) { | ||
switch(req.method) { | ||
case "GET": | ||
extrFromGet(); | ||
break; | ||
default: | ||
extrFromOther(); | ||
} | ||
} | ||
} | ||
if(model) { | ||
if(mdlScenario) | ||
options = {"scenario": mdlScenario}; | ||
mdlData = sanitize(model, mdlData, options); | ||
req.model = { | ||
"data": mdlData, | ||
"validate": function() { return validate(model, mdlData, options); } | ||
}; | ||
} else { | ||
req.model = null; | ||
} | ||
//----------]> | ||
next(); | ||
//----------]> | ||
function extrFromGet() { | ||
mdlName = req.query && req.query.model; | ||
if(mdlName) { | ||
mdlData = req.query.data; | ||
if(mdlData) | ||
mdlData = decodeURIComponent(mdlData); | ||
mdlData = $typenize("hashTable", mdlData, {}); | ||
mdlScenario = req.query.scenario; | ||
model = models[mdlName]; | ||
} | ||
return !!mdlName; | ||
} | ||
function extrFromOther() { | ||
mdlName = req.body && req.body.model; | ||
if(mdlName) { | ||
mdlData = $typenize("hashTable", req.body.data, {}); | ||
mdlScenario = req.body.scenario; | ||
model = models[mdlName]; | ||
} | ||
return !!mdlName; | ||
} | ||
} | ||
} | ||
//----------------)> | ||
function $typenizeHashTable(schema, data, options) { | ||
var optScenario = options.on; | ||
var optScenario = options.scenario; | ||
@@ -228,3 +335,3 @@ var result = data; | ||
function $sanitizeHashTable(schema, data, options) { | ||
var optScenario = options.on; | ||
var optScenario = options.scenario; | ||
@@ -292,3 +399,3 @@ var result = {}; | ||
function $validateHashTable(schema, data, options) { | ||
var optScenario = options.on, | ||
var optScenario = options.scenario, | ||
optErrors = options.errors; | ||
@@ -441,3 +548,4 @@ | ||
input = JSON.parse(input); | ||
} catch(e) { } | ||
} catch(e) { | ||
} | ||
} | ||
@@ -1054,3 +1162,3 @@ | ||
} | ||
})(); | ||
})(true); | ||
@@ -1060,3 +1168,3 @@ //----------------------------------------------------- | ||
if(module && typeof(module) == "object") { | ||
module.exports = $aigis.global(true); | ||
module.exports = $aigis; | ||
} |
{ | ||
"name": "aigis", | ||
"version": "0.0.25", | ||
"version": "0.1.0", | ||
"description": "Simple and Powerful module for strict data control", | ||
@@ -12,2 +12,5 @@ | ||
"validation", | ||
"connect", | ||
"express", | ||
"middleware", | ||
"data", | ||
@@ -42,4 +45,4 @@ "format", | ||
"_id": "aigis@0.0.25", | ||
"_id": "aigis@0.1.0", | ||
"_from": "aigis@" | ||
} |
262
readme.md
@@ -9,7 +9,2 @@ `npm install aigis -g` | ||
$typenize("hashTable").format("{video}: {views}", '{"video": "cats", "views": 100500}'); | ||
$typenize("string").format("Date: {}", new Date()); | ||
$sanitize("array").format("Array: {2}, {1}, {0}", "[11, 12, 13]", {"max": 2}) | ||
$typenize({name: "string"}, {name: 13, skipThisField: "data"}); | ||
@@ -30,2 +25,7 @@ $sanitize({name: {type: "string", max: 2}}, {name: "Omnomnus", delThisField: "data"}); | ||
$typenize("hashTable").format("{video}: {views}", {"video": "cats", "views": 100500}); | ||
$typenize("string").format("Date: {}", new Date()); | ||
$sanitize("array").format("Array: {2}, {1}, {0}", "[11, 12, 13]", {"max": 2}) | ||
//---------]> | ||
@@ -52,2 +52,3 @@ | ||
* Connect/Express middleware: + | ||
* Schema-tree (hashTable, array): + | ||
@@ -66,26 +67,28 @@ * Tests: + | ||
| Name | Desc | Args | | ||
|-------------|-------------|-------------| | ||
| | - || | ||
| createInstance | Create new instance | ([isGlobal]) | | ||
| global | Set `$typenize, $sanitize, $validate` as Global Var (NodeJS) | (v [default: true]) | | ||
| | - || | ||
| type | Set/Delete custom Type (Sanitize) | (name (String/HashTable), [func]) ~ func(input, options) | | ||
| rule | Set/Delete custom Rule (Validate) | (name (String/HashTable), [func]) ~ func(input, options) | | ||
| | - || | ||
| typenize | - | (schema (String/HashTable), [data], [options]) | | ||
| sanitize | - | (schema (String/HashTable), [data], [options]) | | ||
| validate | - | (schema (String/HashTable), [data], [options]) | | ||
| Name | Desc | Args | | ||
|-------------------|---------------------------------------------------------------|---------------| | ||
| | - | | | ||
| createInstance | Create new instance | ([isGlobal]) | | ||
| global | Set `$typenize, $sanitize, $validate` as Global Var (NodeJS) | (v [default: true]) | | ||
| | - | | | ||
| type | Set/Delete custom Type (Sanitize) | (name (String/HashTable), [func]) ~ func(input, options) | | ||
| rule | Set/Delete custom Rule (Validate) | (name (String/HashTable), [func]) ~ func(input, options) | | ||
| | - | | | ||
| typenize | - | (schema (String/HashTable), [data], [options]) | | ||
| sanitize | - | (schema (String/HashTable), [data], [options]) | | ||
| validate | - | (schema (String/HashTable), [data], [options]) | | ||
| Options | Desc | Val | | ||
|-------------|-------------|-------------| | ||
| | - || | ||
| | ALL || | ||
| on | Scenario | - | | ||
| | - || | ||
| | Validate || | ||
| errors | Validate method returns null or an array of errors | true/false (def: false)| | ||
#### Typenize, Sanitize, Validate options | ||
| Options | Desc | Val | | ||
|---------------|-------------|-------------| | ||
| | - | | | ||
| | ALL | | | ||
| scenario | - | - | | ||
| | - | | | ||
| | Validate | | | ||
| errors | Validate method returns null or an array of errors | true/false (def: false)| | ||
```js | ||
@@ -104,15 +107,15 @@ //_ Validation error: structure | ||
| Type | Desc | Params/Options | | ||
|-------------|-------------|----------------| | ||
| | - || | ||
| | ALL (If `schema` is HashTable) | on | | ||
| custom | - | - | | ||
| boolean | true: "true", "on", "yes", "1" | - | | ||
| string | - | - | | ||
| integer | - | - | | ||
| float | - | - | | ||
| date | - | - | | ||
| hashTable | - | schema | | ||
| array | - | schema | | ||
| json | - | - | | ||
| Type | Desc | Params/Options | | ||
|---------------|-----------------------------------|-------------------| | ||
| | - | | | ||
| | ALL (If `schema` is a HashTable) | on | | ||
| custom | - | - | | ||
| boolean | true: "true", "on", "yes", "1" | - | | ||
| string | - | - | | ||
| integer | - | - | | ||
| float | - | - | | ||
| date | - | - | | ||
| hashTable | - | schema | | ||
| array | - | schema | | ||
| json | - | - | | ||
@@ -122,15 +125,15 @@ | ||
| Type | Desc | Params/Options | | ||
|-------------|-------------|----------------| | ||
| | - || | ||
| | ALL (If `schema` is HashTable) | on | | ||
| custom | - | - | | ||
| boolean | true: "true", "on", "yes", "1" | - | | ||
| string | - | default, enum, max, trim, ltrim, rtrim, escape, lowercase, uppercase, onlyDigits, onlyAlphanumeric, onlyWordchar | | ||
| integer | - | default, enum, min, max, abs | | ||
| float | - | default, enum, min, max, abs | | ||
| date | - | default, min, max | | ||
| hashTable | - | schema | | ||
| array | - | schema, max | | ||
| json | - | - | | ||
| Type | Desc | Params/Options | | ||
|---------------|-----------------------------------|-------------------| | ||
| | - | | | ||
| | ALL (If `schema` is a HashTable) | on | | ||
| custom | - | - | | ||
| boolean | true: "true", "on", "yes", "1" | - | | ||
| string | - | default, enum, max, trim, ltrim, rtrim, escape, lowercase, uppercase, onlyDigits, onlyAlphanumeric, onlyWordchar | | ||
| integer | - | default, enum, min, max, abs | | ||
| float | - | default, enum, min, max, abs | | ||
| date | - | default, min, max | | ||
| hashTable | - | schema | | ||
| array | - | schema, max | | ||
| json | - | - | | ||
@@ -148,49 +151,49 @@ ``` | ||
| Rule | Desc | Params/Options | | ||
|-------------|-------------|-------------| | ||
| | - || | ||
| | ALL (If `schema` is HashTable) | on | | ||
| null | - | - | | ||
| nan | - | - | | ||
| finite | If number and not: NaN, INFINITY | - | | ||
| | - || | ||
| boolean | - | - | | ||
| string | - | min, max, enum, pattern | | ||
| integer | - | min, max, enum, divisibleBy | | ||
| float | - | min, max, enum, divisibleBy | | ||
| date | - | min, max | | ||
| hashTable | - | - | | ||
| array | - | min, max | | ||
| json | - | - | | ||
| | - || | ||
| required | Not: null, undefined, length==0, NaN, Invalid Date | - | | ||
| equal | If the string matches the comparison | value, field (If `schema` is HashTable) | | ||
| notEmpty | If string not empty | - | | ||
| lowercase | If string is lowercase | - | | ||
| uppercase | If string is uppercase | - | | ||
| | - || | ||
| wordchar | Alphanumeric characters including the underscore | - | | ||
| alphanumeric | If string is only letters and numbers | - | | ||
| alpha | If string is only letters | - | | ||
| numeric | If string is only numbers | - | | ||
| hexadecimal | - | - | | ||
| email | - | - | | ||
| url | mailto, http, https, ftp, ssh, ws, gopher, news, telnet, ldap | - | | ||
| mongoId | - | - | | ||
| | - || | ||
| hexColor | - | strict (def: false) | | ||
| creditcard | Visa, MasterCard, American Express, Discover, Diners Club, and JCB card | - | | ||
| phone | `ru-RU`, `zh-CN`, `en-ZA`, `en-AU`, `en-HK`, `fr-FR`, `pt-PT`, `el-GR` | locale (def: "ru-RU") | | ||
| | - || | ||
| uuid | - | version (def: 3,4,5) | | ||
| uuid.v3 | - | - | | ||
| uuid.v4 | - | - | | ||
| uuid.v5 | - | - | | ||
| | - || | ||
| ip | This function simply check whether the address is a valid IPv4 or IPv6 address | version (def: 4,6) | | ||
| ip.v4 | - | - | | ||
| ip.v6 | - | - | | ||
| | - || | ||
| ascii | - | - | | ||
| base64 | - | - | | ||
| Rule | Desc | Params/Options | | ||
|---------------|-----------------------------------------------------------------------------------|-----------------------| | ||
| | - | | | ||
| | ALL (If `schema` is HashTable) | on | | ||
| null | - | - | | ||
| nan | - | - | | ||
| finite | If number and not: NaN, INFINITY | - | | ||
| | - | | | ||
| boolean | - | - | | ||
| string | - | min, max, enum, pattern | | ||
| integer | - | min, max, enum, divisibleBy | | ||
| float | - | min, max, enum, divisibleBy | | ||
| date | - | min, max | | ||
| hashTable | - | - | | ||
| array | - | min, max | | ||
| json | - | - | | ||
| | - | | | ||
| required | Not: null, undefined, length==0, NaN, Invalid Date | - | | ||
| equal | If the string matches the comparison | value, field (If `schema` is HashTable) | | ||
| notEmpty | If string not empty | - | | ||
| lowercase | If string is lowercase | - | | ||
| uppercase | If string is uppercase | - | | ||
| | - | | | ||
| wordchar | Alphanumeric characters including the underscore | - | | ||
| alphanumeric | If string is only letters and numbers | - | | ||
| alpha | If string is only letters | - | | ||
| numeric | If string is only numbers | - | | ||
| hexadecimal | - | - | | ||
| email | - | - | | ||
| url | mailto, http, https, ftp, ssh, ws, gopher, news, telnet, ldap | - | | ||
| mongoId | - | - | | ||
| | - | | | ||
| hexColor | - | strict (def: false) | | ||
| creditcard | Visa, MasterCard, American Express, Discover, Diners Club, and JCB card | - | | ||
| phone | `ru-RU`, `zh-CN`, `en-ZA`, `en-AU`, `en-HK`, `fr-FR`, `pt-PT`, `el-GR` | locale (def: "ru-RU") | | ||
| | - | | | ||
| uuid | - | version (def: 3,4,5) | | ||
| uuid.v3 | - | - | | ||
| uuid.v4 | - | - | | ||
| uuid.v5 | - | - | | ||
| | - | | | ||
| ip | This function simply check whether the address is a valid IPv4 or IPv6 address | version (def: 4,6) | | ||
| ip.v4 | - | - | | ||
| ip.v6 | - | - | | ||
| | - | | | ||
| ascii | - | - | | ||
| base64 | - | - | | ||
@@ -205,2 +208,59 @@ | ||
#### Middleware | ||
```js | ||
require("aigis"); | ||
var rExpress = require("express"); | ||
var rBodyParser = require("body-parser"); | ||
//----------------------------------------------------- | ||
var objApp = rExpress(); | ||
var appModels = { | ||
"user": { | ||
"id": "integer", | ||
"name": "string", | ||
"pswd": {"type": "string", "rule": "required", "on": ["update"]} | ||
}, | ||
"task": { | ||
"id": "integer" | ||
} | ||
}; | ||
//-------------------]> | ||
objApp | ||
.use(rBodyParser.urlencoded({"extended": false})) | ||
.use(rBodyParser.json()); | ||
//--------)> | ||
objApp.use($sanitize.mid(appModels)); //_Default: GET -> POST or PUT or ... etc | ||
//objApp.use($sanitize.mid(appModels, "POST")); //_Only: POST | ||
//objApp.use($sanitize.mid(appModels, ["GET", "POST", "PUT"])); //_Only: GET, POST, PUT | ||
//-------------------]> | ||
objApp.all("/", function(req, res) { | ||
if(!req.model) | ||
return res.send("req.model: empty"); | ||
res.send( | ||
JSON.stringify(req.model.data) + | ||
" | validate: " + req.model.validate() | ||
); | ||
}); | ||
//-------------------]> | ||
objApp.listen(3000, "127.0.0.1"); | ||
//_ GET: http://127.0.0.1:3000/?model=user&scenario=update&data=%7B%22id%22%3A%226d%22%2C%22name%22%3A%22DT%22%7D | ||
//_ POST: {"model":"user", "data": {id: "6d", name: "DT"}} | ||
``` | ||
#### Examples | ||
@@ -309,3 +369,3 @@ | ||
//data = {"name": "DT", "pts": "32", "pswd": "", "pswdCheck": /\w+/g}; //_ #2 | ||
console.log("1#", $validate(schema, data)); | ||
@@ -316,3 +376,3 @@ console.log("2#", $validate(schema, data, {"errors": true})); | ||
#### 3# of the fundamental modules | ||
[2# Fire-Inject][2] | ||
[2# lightject][2] | ||
@@ -328,2 +388,2 @@ ## License | ||
[1]: http://666.io | ||
[2]: https://www.npmjs.com/package/fire-inject | ||
[2]: https://www.npmjs.com/package/lightject |
@@ -140,6 +140,6 @@ //----------------------------------------------------- | ||
result = $sanitize(schema, {"pts": 50}, {"on": ""}); | ||
result = $sanitize(schema, {"pts": 50}, {"scenario": ""}); | ||
testX(undefined, result.pts, "result.pts"); | ||
result = $sanitize(schema, {"pts": 50}, {"on": "update"}); | ||
result = $sanitize(schema, {"pts": 50}, {"scenario": "update"}); | ||
testX(30, result.pts, "result.pts"); | ||
@@ -245,6 +245,6 @@ } | ||
result = $validate(schema, {"pts": 50}, {"on": ""}); | ||
result = $validate(schema, {"pts": 50}, {"scenario": ""}); | ||
testX(true, result, "result.pts"); | ||
result = $validate(schema, {"pts": 50}, {"on": "update"}); | ||
result = $validate(schema, {"pts": 50}, {"scenario": "update"}); | ||
testX(false, result, "result.pts"); | ||
@@ -251,0 +251,0 @@ } |
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
85821
10
1631
378