talyssonoc/structure

Schema subclass loses validate method

jeremyruppel opened this issue · 2 comments

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:

  1. Make VALIDATE enumerable here, which will make Object.assign do the right thing. The only thing I'd be concerned about is now Object.keys would pick up VALIDATE as well and place it on the instance where it probably shouldn't be.
  2. 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!

Hello @jeremyruppel, I like the second approach! The contribution will be appreciated!

Right on, I'll send up the PR today. Thanks @talyssonoc!