data-defender
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -17,3 +17,4 @@ 'use strict'; | ||
INVAL_SCHEMA: 'INVALID_SCHEMA_GIVEN', | ||
INVAL_VALIDATION: 'INVALID_PROPERY_VALIDATION_FUNC' | ||
INVAL_VALIDATION: 'INVALID_PROPERY_VALIDATION_FUNC', | ||
NO_VAL: 'MISSING_VALUE' | ||
}; | ||
@@ -25,2 +26,9 @@ | ||
exports.NO_VAL = function (struct, msg) { | ||
var m = exports.CODE.NO_VAL + ': Property "' + msg + '" is missing its value and there is no defualt value'; | ||
var error = new Error(m); | ||
error.code = exports.CODE.NO_VAL; | ||
return handleError(struct, error); | ||
}; | ||
exports.INVAL_VALIDATION = function (struct, msg) { | ||
@@ -84,4 +92,4 @@ var m = exports.CODE.INVAL_VALIDATION + ': The validation function "' + msg + '" is invalid'; | ||
exports.INVAL_DEFAULT = function (struct, msg) { | ||
var m = exports.CODE.INVAL_DEF + ': The default "' + msg + '" value is invalid'; | ||
exports.INVAL_DEFAULT = function (struct, name, msg) { | ||
var m = exports.CODE.INVAL_DEF + ': Property ' + name + '\'s default "' + msg + '" is invalid'; | ||
var error = new Error(m); | ||
@@ -88,0 +96,0 @@ error.code = exports.CODE.INVAL_DEF; |
{ | ||
"name": "data-defender", | ||
"description": "A database-agnostic simple data schema enforcer for node.js", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Nobuyori Takahashi <voltrue2@yahoo.com>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -124,2 +124,4 @@ # data-defender | ||
**NOTE**: By setting this value to `null`, you can allow `null` value for this property. | ||
##### validation [*function] | ||
@@ -126,0 +128,0 @@ |
@@ -24,4 +24,5 @@ 'use strict'; | ||
*default: <default value>, | ||
*max: <number> | ||
*min: <number> | ||
*max: <number>, | ||
*min: <number>, | ||
*validation: <function> | ||
}; | ||
@@ -78,3 +79,3 @@ */ | ||
if (valMap.default !== undefined && !isValid(valMap, valMap.default)) { | ||
return ERROR.INVAL_DEFAULT(this, valMap.default); | ||
return ERROR.INVAL_DEFAULT(this, name, valMap.default); | ||
} | ||
@@ -122,2 +123,5 @@ // validate schema if given | ||
if (item.type !== DATATYPE.UNIQUE) { | ||
if (item.default === undefined) { | ||
return ERROR.NO_VAL(this, name); | ||
} | ||
if (typeof item.default === 'function') { | ||
@@ -127,5 +131,3 @@ values[name] = item.default(); | ||
} | ||
if (item.default !== null) { | ||
values[name] = item.default; | ||
} | ||
values[name] = item.default; | ||
} else { | ||
@@ -202,5 +204,12 @@ // unique type property | ||
// if null is not allowd | ||
if (constraints.default !== null && value === null) { | ||
return false; | ||
// null is allowed | ||
if (value === null) { | ||
// null is allowed | ||
if (constraints.default === null) { | ||
return true; | ||
} | ||
// null is not allowed | ||
if (constraints.default !== null) { | ||
return false; | ||
} | ||
} | ||
@@ -207,0 +216,0 @@ |
@@ -45,3 +45,4 @@ var assert = require('assert'); | ||
max: 10, | ||
min: 2 | ||
min: 2, | ||
default: null | ||
}); | ||
@@ -51,6 +52,8 @@ test.define('number', { | ||
max: 100, | ||
min: 1 | ||
min: 1, | ||
default: null | ||
}); | ||
test.define('list', { | ||
type: defender.DATATYPE.ARR, | ||
default: null, | ||
max: 3, | ||
@@ -61,2 +64,3 @@ min: 1 | ||
type: defender.DATATYPE.OBJ, | ||
default: null, | ||
max: 2, | ||
@@ -66,3 +70,4 @@ min: 1 | ||
test.define('struct', { | ||
type: defender.DATATYPE.OBJ | ||
type: defender.DATATYPE.OBJ, | ||
default: null | ||
}); | ||
@@ -69,0 +74,0 @@ dataObj = test.load(); |
39420
1118
246