Comparing version 0.5.0 to 0.5.1
@@ -220,7 +220,12 @@ | ||
var validate = mschema.validate = function (data, _schema) { | ||
var validate = mschema.validate = function (data, _schema, options) { | ||
var result = { valid: true, instance: {} }, | ||
errors = []; | ||
errors = [], | ||
options = options || {}; | ||
if (typeof options.strict === "undefined") { | ||
options.strict = true; | ||
} | ||
if (typeof _schema !== 'object') { | ||
@@ -257,4 +262,17 @@ return 'schema contains no properties, cannot validate'; | ||
if (options.strict === false) { | ||
var _value; | ||
// determine if any incoming data might need to be changed from a string number into a Number type | ||
if (typeof value === "string" && (property === "number" || property.type === "number")) { | ||
_value = parseInt(data[propertyName], 10); | ||
if (_value.toString() !== "NaN") { | ||
// a non NaN number was parsed, assign it as validation value and to instance value | ||
value = _value; | ||
data[propertyName] = value; | ||
} | ||
} | ||
} | ||
// check if it's value is required but undefined in value | ||
if (property.required && (value === undefined || value.length === 0)) { | ||
if (property.required && (value === null || value === undefined || value.length === 0)) { | ||
errors.push({ | ||
@@ -261,0 +279,0 @@ property: propertyName, |
24
index.js
@@ -18,7 +18,12 @@ var mschema = {}; | ||
var validate = mschema.validate = function (data, _schema) { | ||
var validate = mschema.validate = function (data, _schema, options) { | ||
var result = { valid: true, instance: {} }, | ||
errors = []; | ||
errors = [], | ||
options = options || {}; | ||
if (typeof options.strict === "undefined") { | ||
options.strict = true; | ||
} | ||
if (typeof _schema !== 'object') { | ||
@@ -55,4 +60,17 @@ return 'schema contains no properties, cannot validate'; | ||
if (options.strict === false) { | ||
var _value; | ||
// determine if any incoming data might need to be changed from a string number into a Number type | ||
if (typeof value === "string" && (property === "number" || property.type === "number")) { | ||
_value = parseInt(data[propertyName], 10); | ||
if (_value.toString() !== "NaN") { | ||
// a non NaN number was parsed, assign it as validation value and to instance value | ||
value = _value; | ||
data[propertyName] = value; | ||
} | ||
} | ||
} | ||
// check if it's value is required but undefined in value | ||
if (property.required && (value === undefined || value.length === 0)) { | ||
if (property.required && (value === null || value === undefined || value.length === 0)) { | ||
errors.push({ | ||
@@ -59,0 +77,0 @@ property: propertyName, |
{ | ||
"name": "mschema", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"keywords": ["schema", "mschema", "validator", "json", "json-schema"], | ||
@@ -5,0 +5,0 @@ "description": "A schema language for defining the structure of JSON data", |
@@ -203,3 +203,3 @@ # mschema | ||
Simply put: JSON-Schema has a lot of functionality that most developers don't need or want. The complexity of JSON-Schema makes it hard to use. | ||
Simply put: JSON-Schema has a lot of functionality that most developers don't need or want. The complexity of JSON-Schema makes it difficult to use and hard to build tools for. | ||
@@ -206,0 +206,0 @@ ### Key differences between mschema and JSON-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
67667
32
2144