Comparing version 0.0.19 to 0.0.20
64
index.js
@@ -5,3 +5,3 @@ //----------------------------------------------------- | ||
// Site: 666.io | ||
// Version: 0.00.019 | ||
// Version: 0.00.020 | ||
// | ||
@@ -99,3 +99,3 @@ //----------------------------------------------------- | ||
"typenize": function(schema, data, options) { | ||
return parseSchema(C_MODE_TYPENIZE, schema, data, options, { | ||
return runSchema(C_MODE_TYPENIZE, schema, data, options, { | ||
"string": function(schema, data, options) { | ||
@@ -165,5 +165,5 @@ return $typenize(schema, data, options); | ||
"sanitize": function(schema, data, options) { | ||
return parseSchema(C_MODE_SANITIZE, schema, data, options, { | ||
return runSchema(C_MODE_SANITIZE, schema, data, options, { | ||
"string": function(schema, data, options) { | ||
return $sanitize(schema, $typenize(schema, data, options), options); | ||
return $sanitize(schema, $typenize(schema, data, options, true), options); | ||
}, | ||
@@ -222,3 +222,3 @@ | ||
result[field] = $sanitize(nameFunc, $typenize(nameFunc, fieldData, schemaData), schemaData); | ||
result[field] = $sanitize(nameFunc, $typenize(nameFunc, fieldData, schemaData, true), schemaData); | ||
} | ||
@@ -232,3 +232,3 @@ | ||
"validate": function(schema, data, options) { | ||
return parseSchema(C_MODE_VALIDATE, schema, data, options, { | ||
return runSchema(C_MODE_VALIDATE, schema, data, options, { | ||
"string": function(schema, data, options) { | ||
@@ -348,3 +348,3 @@ return $validate(schema, data, options, options.data); | ||
function $typenize(type, input, options) { | ||
function $typenize(type, input, options, isUseSanitize) { | ||
if(type === "custom") | ||
@@ -396,37 +396,39 @@ return input; | ||
case "hashTable": | ||
if(!input || Array.isArray(input)) | ||
input = {}; | ||
if(input && typeof(input) === "string") { | ||
try { | ||
input = JSON.parse(input); | ||
} catch(e) { } | ||
} | ||
input = input && typeof(input) === "object" && !Array.isArray(input) ? input : {}; | ||
if(typeof(options.schema) === "object") { | ||
return gExport.typenize(options.schema, input, options); | ||
return gExport[isUseSanitize ? "sanitize" : "typenize"](options.schema, input, options); | ||
} | ||
if(typeof(input) === "object") | ||
return input; | ||
return input; | ||
if(typeof(input) !== "string") | ||
return {}; | ||
try { | ||
input = JSON.parse(input); | ||
return Array.isArray(input) ? {} : input; | ||
} catch(e) { | ||
case "array": | ||
if(input && typeof(input) === "string") { | ||
try { | ||
input = JSON.parse(input); | ||
} catch(e) { } | ||
} | ||
return {}; | ||
input = Array.isArray(input) ? input : []; | ||
case "array": | ||
if(Array.isArray(input)) | ||
return input; | ||
if(Array.isArray(options.schema)) { | ||
var result = isUseSanitize ? [] : input, | ||
if(!input || typeof(input) !== "string") | ||
return []; | ||
func = gExport[isUseSanitize ? "sanitize" : "typenize"], | ||
sch = options.schema; | ||
try { | ||
input = JSON.parse(input); | ||
return Array.isArray(input) ? input : []; | ||
} catch(e) { | ||
for(var i = sch.length - 1; i >= 0; i--) { | ||
result[i] = func(sch[i], input[i], options); | ||
} | ||
return result; | ||
} | ||
return []; | ||
return input; | ||
@@ -848,3 +850,3 @@ case "json": | ||
function parseSchema(mode, schema, data, options, callbacks) { | ||
function runSchema(mode, schema, data, options, callbacks) { | ||
if(!schema) | ||
@@ -851,0 +853,0 @@ throw new Error("[!] Empty schema."); |
{ | ||
"name": "aigis", | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"description": "Simple and Powerful module for strict data control", | ||
@@ -40,4 +40,4 @@ | ||
"_id": "aigis@0.0.19", | ||
"_id": "aigis@0.0.20", | ||
"_from": "aigis@" | ||
} |
@@ -7,8 +7,10 @@ `npm install aigis -g` | ||
$typenize({name: "string"}, {name: 969, "skipThisField": "data"}); | ||
$typenize({name: "string"}, {name: 13, "skipThisField": "data"}); | ||
$sanitize({name: {type: "string", max: 2}}, {name: "Omnomnomnus", "delThisField": "data"}); | ||
$validate("integer", "2"); | ||
$sanitize({"data": {"type": "array", "schema": ["integer", "string"]}}, {"data": [6.9, "name", 100]}); | ||
``` | ||
* Support schema-tree: + | ||
* Schema-tree (hashTable, array): + | ||
* Tests: + | ||
@@ -74,3 +76,3 @@ * Examples: + | ||
| hashTable | - | schema | | ||
| array | - || | ||
| array | - | schema | | ||
| json | - || | ||
@@ -77,0 +79,0 @@ |
@@ -16,2 +16,79 @@ //----------------------------------------------------- | ||
var data = { | ||
"data": [2.2, "name", 100] | ||
}; | ||
console.log( | ||
$typenize({"data": {"type": "array", "schema": ["integer", "string"]}}, data) | ||
); | ||
console.log( | ||
$sanitize({"data": {"type": "array", "schema": ["integer", "string"]}}, data) | ||
); | ||
//-----------]> | ||
var data = { | ||
"data": [{"pts": 2.2}, "name"] | ||
}; | ||
console.log( | ||
$typenize({"data": {"type": "array", "schema": [{"pts": "integer"}]}}, data) | ||
); | ||
console.log( | ||
$sanitize({"data": {"type": "array", "schema": [{"pts": "integer"}]}}, data) | ||
); | ||
console.log("\n\n"); | ||
//-----------]> | ||
var schema = { | ||
"data": { | ||
"type": "hashTable", | ||
"schema": { | ||
"login": "string", | ||
"password": "string", | ||
"more": { | ||
"type": "hashTable", | ||
"schema": { | ||
"someData": "string" | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var data = { | ||
"test": 1, | ||
"data": JSON.stringify({ | ||
"login": 1, | ||
"tex": 2 | ||
}) | ||
}; | ||
console.log($sanitize(schema, data)); | ||
console.log($typenize(schema, data)); | ||
console.log("\n\n"); | ||
var data = { | ||
"test": 1, | ||
"data": { | ||
"login": 1, | ||
"tex": 2 | ||
} | ||
}; | ||
console.log($typenize(schema, data)); | ||
console.log($sanitize(schema, data)); | ||
console.log("\n\n"); | ||
//----------------------------------------------------- | ||
console.log("+-------------------------+"); | ||
@@ -18,0 +95,0 @@ console.log("| Schema"); |
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
68534
1286
314