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

Cannot extend custom SchemaObject and print not working properly

brunocanella opened this issue · comments

According to documentation, the following minimal code should be able to run and print the equivalente to the "toObject" output on the console:

main.js

let SchemaObject = require('schema-object');
let A = new SchemaObject({
	foo: String
});
let a = new A({
	foo: "Foo"
});
console.log( a );

let B = A.extend({
	bar: Number
});
let b = new B({
	foo: "Bar",
	bar: 10
});
console.log( b );

The following command is producing this error: node ./main.js

SchemaObjectInstance { foo: 'Foo' }
/home/bruno/Documents/Workspace/Html/minimal/node_modules/schema-object/dist/schemaobject.js:823
                                throw _iteratorError;
                                ^

TypeError: Cannot read property 'constructors' of undefined
    at _loop (/home/bruno/Documents/Workspace/Html/minimal/node_modules/schema-object/dist/schemaobject.js:786:84)
    at Function.extend (/home/bruno/Documents/Workspace/Html/minimal/node_modules/schema-object/dist/schemaobject.js:811:29)
    at Object.<anonymous> (/home/bruno/Documents/Workspace/Html/minimal/main.js:13:11)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:422:7)`

I am using npm 4.1.2 and node v7.6.0, on a linux mint (17.3) system, with schema-object version schema-object@4.0.10 extraneous

I am not sure if this is a configuration problem on my end, but I think it is correctly configured. I am new to this whole node, npm and Javascript thing, but I have a background that goes way back in programming.

My guess is that this is a problem related to Proxy, although I've no idea about what it does. Maybe a problem with it's interfacing (function calling).

According to the main page, since I am using a newer version, I don't have to use the harmony flag option. Even using it, these errors occur.

Ok, so here is a fix for this:

on line 765:
value: function extend(extendSchema, extendOptions) {

add the following code: extendOptions = extendOptions || {}; or change that previous line to value: function extend(extendSchema, extendOptions = {}) {

This will avoid the error from the previous post.

Unfortunately, the prints are still like this:

SchemaObjectInstance { foo: 'Foo' }
SchemaObjectInstance { foo: 'Bar', bar: 10 }

in roder to avoid the SchemaObjectInstance from appearing, if I use ...
console.log( a.toObject() );
console.log( b.toObject() );
... the output will be:
{ foo: 'Foo' }
{ foo: 'Bar', bar: 10 }

According to the documentation on the github page, this should be the default behavior, even for the case where I don't use the toObject method.

Anyway, I think this matter is mostly solved.

Ah, it's just that you aren't providing options and there isn't a default. I'll fix it and add a test for that behavior. Thanks for the report.

PS: The SchemaObjectInstance is just the way the console.log works, it's not actually having an impact on the serialization of the object.

I see. Thanks for the reply Scott.

@brunocanella FYI the new version is up on NPM now: 4.0.11

Just tested. It is working now. Thanks o/