scotthovestadt / schema-object

Enforce schema on JavaScript objects, including type, transformation, and validation. Supports extends, sub-schemas, and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with array usage in nested object schema

blnprasad opened this issue · comments

Hi,
If we use array type in nested object its throwing "cannot set property "length" of undefined" in some cases. I'm pasting sample problematic schema for above. Please have a look into this.

var schemaObject = require('node-schema-object');
var type_string = {type: String};

var sample = new schemaObject({
"_array": {type: Array, arrayType: String},
"hi": String,
"hello": String,
});

var _ext_sample = new schemaObject({
"_sample": sample,
})

var dev = new _ext_sample({"_sample": {"hi": "123", "hello": "456", "_array": ["1", "2", "3"]}});
console.log(dev.getErrors());
console.log(dev.toObject());

Thanks,
BLN

This was an interesting bug.

When setting an object within an object via the constructor, the clear() function is called internally. Previously, clear() simply reset the object to {}. However, this means that all defaults were destroyed, breaking an assumption within the code that an Array would always exist on fields with the Array type.

I've updated clear() to reset all indexes to their defaults instead, which fixes this bug. Additionally, I've written several unit tests to ensure that this bug will not happen again.

Thank you for your report and let me know if you have any questions.

You can pull the new code from NPM, version 0.9.2.

Hi,
With new changes i'm seeing some strage behaviour where the following code corrupting passed value field in typecast function.
schemaObject = originalValue;
schemaObject.clear() ->> this statment is causing "value" and "properties" to be corrupted in some nested schmea object cases which is resulting into pouplating of schemaObject with undefined instead of default values.

If above is not clear from code review then i'll try to get example schmea for above.

Thanks,
BLN

Discuss this issue in #12