herbsjs / gotu

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

Validation if the entity is not working properly #72

Closed vitorgamer58 closed 1 year ago

vitorgamer58 commented 1 year ago

Describe the bug The isEntity function is not properly reflecting whether or not it is an entity, as the validation whether it is an instance of BaseEntity is returning false.

entity.isEntity = (instance) => {
    return (
        instance instanceof BaseEntity || 
        instance.prototype instanceof BaseEntity)
}

To Reproduce Steps to reproduce the behavior:

The issue can be reproduced by invoking entity.isEntity on an instance of BaseEntity within the buildAllFields method of the DataMapper class in Herbs2Mongo. This has been tested using nested entities.

Expected behavior The isEntity function should return true for all instances of BaseEntity.

Proposed solution When calling instance.toString() we receive a string class extends BaseEntity {}, so it is possible to validate that the instance converted to string includes extends BaseEntity

entity.isEntity = (instance) => {
    return (
        instance instanceof BaseEntity ||
        instance.prototype instanceof BaseEntity ||
        instance.toString()?.includes('extends BaseEntity'))
}

Screenshots Here's a print of the problem happening when Herbs2Mongo tries to do this validation: Herbs2Mongo

Additional context Resolution of issue https://github.com/herbsjs/herbs2mongo/issues/35 depends on this bug being fixed.

vitorgamer58 commented 1 year ago

It was a problem related to npm link locally, it doesn't happen in production.