herbsjs / gotu

Entities - Create your Entity and enjoy an elegant validation and serialization
Other
11 stars 19 forks source link

entity.isEntity is not working is some cases #67

Closed jhomarolo closed 2 years ago

jhomarolo commented 2 years ago

Describe the bug After updating the project with a new version inside herbs (which had nothing to do with the gotu), the isEntitymethod simply stopped returning true for fields that are entities of other entities.

And so the insert method also stopped working, because herbs2knex does a filter of fields that are entities before inserting.

But after doing that check here: new String(instance.__proto__).valueOf() === new String(BaseEntity).valueOf() the return is true.

I'm having a hard time figuring out why.

image

To Reproduce To reproduce, just download the whole project and update herbs to the latest version 1.6.1

Expected behavior

 entity('To Do List', {
        id: id(Number),
        name: field(String, {
            validation: { presence: true, length: { minimum: 3 } }
        }),
        items: field([Item], { default: () => [] }),    /*<--- HERE the method is entity is returning false!*/

       })

Additional context I don't know if it's a problem with herbs2knex, but it hasn't been updated either.

dalssoft commented 2 years ago

It might not be the case, but scenarios like this happens when the require('@herbsjs/herbs') for each entity is loading it from different node_modules. Ex: One is loading from the project's node_modules and the other is loading from herbs2knex's node_modules. So when comparing, it is not the same class/object

jhomarolo commented 2 years ago

@dalssoft it seems you're right!

After changing the reference inside herbs2knes to node_modules from todo-list the insert method returned to work.

Any ideas about how to solve this?

Whats is the risks using this validation inside gotu? new String(instance.__proto__).valueOf() === new String(BaseEntity).valueOf()

entity.isEntity = (instance) => {
    return (
        instance instanceof BaseEntity || 
        instance.prototype instanceof BaseEntity ||
        new String(instance.__proto__).valueOf() === new String(BaseEntity).valueOf())
}
dalssoft commented 2 years ago

After changing the reference inside herbs2knes to node_modules from todo-list the insert method returned to work.

We need to find a way to fix this. It might be the peerDependency. Or if you are in dev mode, @herbsjs/herbs might be installed using devDependency

Whats is the risks using this validation inside gotu? new String(instance.proto).valueOf() === new String(BaseEntity).valueOf()

We should avoid. It can masquerade real dependencies problems

jhomarolo commented 2 years ago

Fixed with https://github.com/herbsjs/herbs2knex/releases/tag/v1.5.3