Schema subclass loses validate method
jeremyruppel opened this issue · 2 comments
jeremyruppel commented
Currently, if you try to subclass a structure with another structure, you get an error when you call .validate()
on the subclass.
const { attributes } = require('structure')
const Foo = attributes({})(class Foo {})
const Bar = attributes({})(class Bar extends Foo {})
const bar = new Bar
bar.validate()
/* throws:
const errors = validation.validate(data);
^
TypeError: Cannot read property 'validate' of undefined
*/
The problem is this bit, where all enumerable properties get assigned from schema
and WrapperClass[SCHEMA]
to a new object and non-enumerable properties get left behind.
I can think of two ways to fix this:
- Make
VALIDATE
enumerable here, which will makeObject.assign
do the right thing. The only thing I'd be concerned about is nowObject.keys
would pick upVALIDATE
as well and place it on the instance where it probably shouldn't be. - Move the
Schema.normalize
call down a few lines so it takes effect after the schema extension.
I'm happy to submit a patch for either approach, just let me know what you think would be best. Thanks!
talyssonoc commented
Hello @jeremyruppel, I like the second approach! The contribution will be appreciated!
jeremyruppel commented
Right on, I'll send up the PR today. Thanks @talyssonoc!