mschema
Advanced tools
Comparing version 0.5.2 to 0.5.3
@@ -217,6 +217,9 @@ | ||
return typeof val === "object"; | ||
}, | ||
"any": function (val) { | ||
return true; | ||
} | ||
}; | ||
var validate = mschema.validate = function (data, _schema, options) { | ||
var validate = mschema.validate = function (_data, _schema, options) { | ||
@@ -248,3 +251,3 @@ var result = { valid: true, instance: {} }, | ||
if (typeof property === "string" && (property === 'string' || property === 'number' || property === 'object' || property === 'array' || property === 'boolean')) { | ||
if (typeof property === "string" && (property === 'string' || property === 'number' || property === 'object' || property === 'array' || property === 'boolean' || property === 'any')) { | ||
property = { | ||
@@ -452,6 +455,25 @@ "type": property, | ||
_parse(data, _schema); | ||
result.instance = data; | ||
// create a clone of the schema so the original schema passed is not modifed by _parse() | ||
var schemaCopy = {}; | ||
schemaCopy = clone(_schema); | ||
// if the incoming data is not an object, create a single key object to represent it | ||
if (typeof _data !== "object") { | ||
_data = { | ||
key: _data | ||
}; | ||
schemaCopy = { | ||
key: schemaCopy | ||
} | ||
} | ||
_parse(_data, schemaCopy); | ||
// TODO: clone data to fix immutable data issue | ||
// see: /test/immutable-data.js | ||
// var dataCopy = clone(_data); | ||
// _parse(dataCopy, schemaCopy); | ||
result.instance = _data; | ||
if (errors.length > 0) { | ||
@@ -555,4 +577,22 @@ result.valid = false; | ||
function clone (obj, copy) { | ||
if (obj == null || typeof obj != "object") { | ||
return obj; | ||
} | ||
if (obj.constructor != Object && obj.constructor != Array) { | ||
return obj; | ||
} | ||
if (obj.constructor == Date || obj.constructor == RegExp || obj.constructor == Function || | ||
obj.constructor == String || obj.constructor == Number || obj.constructor == Boolean) { | ||
return new obj.constructor(obj); | ||
} | ||
copy = copy || new obj.constructor(); | ||
for (var name in obj) { | ||
copy[name] = typeof copy[name] == "undefined" ? clone(obj[name], null) : copy[name]; | ||
} | ||
return copy; | ||
} | ||
module['exports'] = mschema; | ||
}); | ||
require.alias("mschema/index.js", "mschema/index.js"); |
43
index.js
@@ -21,3 +21,3 @@ var mschema = {}; | ||
var validate = mschema.validate = function (data, _schema, options) { | ||
var validate = mschema.validate = function (_data, _schema, options) { | ||
@@ -252,6 +252,25 @@ var result = { valid: true, instance: {} }, | ||
_parse(data, _schema); | ||
result.instance = data; | ||
// create a clone of the schema so the original schema passed is not modifed by _parse() | ||
var schemaCopy = {}; | ||
schemaCopy = clone(_schema); | ||
// if the incoming data is not an object, create a single key object to represent it | ||
if (typeof _data !== "object") { | ||
_data = { | ||
key: _data | ||
}; | ||
schemaCopy = { | ||
key: schemaCopy | ||
} | ||
} | ||
_parse(_data, schemaCopy); | ||
// TODO: clone data to fix immutable data issue | ||
// see: /test/immutable-data.js | ||
// var dataCopy = clone(_data); | ||
// _parse(dataCopy, schemaCopy); | ||
result.instance = _data; | ||
if (errors.length > 0) { | ||
@@ -355,2 +374,20 @@ result.valid = false; | ||
function clone (obj, copy) { | ||
if (obj == null || typeof obj != "object") { | ||
return obj; | ||
} | ||
if (obj.constructor != Object && obj.constructor != Array) { | ||
return obj; | ||
} | ||
if (obj.constructor == Date || obj.constructor == RegExp || obj.constructor == Function || | ||
obj.constructor == String || obj.constructor == Number || obj.constructor == Boolean) { | ||
return new obj.constructor(obj); | ||
} | ||
copy = copy || new obj.constructor(); | ||
for (var name in obj) { | ||
copy[name] = typeof copy[name] == "undefined" ? clone(obj[name], null) : copy[name]; | ||
} | ||
return copy; | ||
} | ||
module['exports'] = mschema; |
{ | ||
"name": "mschema", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"keywords": ["schema", "mschema", "validator", "json", "json-schema"], | ||
@@ -5,0 +5,0 @@ "description": "A schema language for defining the structure of JSON data", |
@@ -12,7 +12,4 @@ var tap = require("tap"), | ||
// TODO: implement validation for non-object values | ||
return; | ||
test("mschema.validate - valid data - constrained property - string - minLength", function (t) { | ||
test("mschema.validate - valid data - constrained property - string - regex", function (t) { | ||
var name = { | ||
@@ -33,2 +30,3 @@ "type": "string", | ||
test("mschema.validate - invalid data - constrained property - string - regex", function (t) { | ||
@@ -38,6 +36,6 @@ | ||
"type": "string", | ||
"minLength": 4 | ||
"regex": /^[\w|\-|\.]/ | ||
}; | ||
var data = "M"; | ||
var data = "(!@#@!)"; | ||
@@ -47,12 +45,11 @@ var result = mschema.validate(data, name); | ||
t.equal(result.valid, false); | ||
t.type(result.errors, Array) | ||
t.type(result.errors, Array); | ||
t.type(result.errors, Object); | ||
t.equal(result.errors.length, 1); | ||
//t.equal(result.errors[0].property, 'name'); | ||
t.equal(result.errors[0].constraint, 'minlength'); | ||
t.equal(result.errors[0].expected, 4); | ||
t.equal(result.errors[0].actual, 1); | ||
t.equal(result.errors[0].value, "M"); | ||
t.equal(result.errors[0].property, 'key'); | ||
t.equal(result.errors[0].constraint, 'regex'); | ||
t.equal(result.errors[0].actual, "(!@#@!)"); | ||
t.equal(result.errors[0].value, "(!@#@!)"); | ||
t.ok(result, "data is invalid"); | ||
@@ -59,0 +56,0 @@ t.end(); |
71577
34
2274