Comparing version 1.0.15 to 1.0.16
{ | ||
"name": "gotu", | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"description": "Domain entities javascript library.", | ||
@@ -5,0 +5,0 @@ "main": "./src/gotu.js", |
@@ -283,13 +283,11 @@ <p align="center"><img src="https://raw.githubusercontent.com/herbsjs/gotu/master/docs/logo.png" height="220"></p> | ||
Check if a instance is the same type from its parent entity class (similar to `instanceOf`) | ||
Check if an object is a Gotu Entity class | ||
```javascript | ||
const AnEntity = entity('A entity', {}) | ||
const AnSecondEntity = entity('A second entity', {}) | ||
const instance1 = new AnEntity() | ||
const instance2 = new AnSecondEntity() | ||
AnEntity.isParentOf(instance1) // true | ||
AnEntity.isParentOf(instance2) // false | ||
entity.isEntity(AnEntity) // true | ||
entity.isEntity(Object) // false | ||
``` | ||
@@ -296,0 +294,0 @@ |
@@ -5,2 +5,12 @@ const { validate, checker } = require("suma") | ||
class BaseEntity { | ||
constructor() { | ||
for (const [name, definition] of Object.entries(this.meta.schema)) { | ||
// ignore functions | ||
if (checker.isFunction(definition)) continue | ||
this[name] = definition.defaultValue | ||
} | ||
} | ||
validate() { | ||
@@ -7,0 +17,0 @@ const errors = {} |
@@ -25,3 +25,2 @@ const { Field } = require('./field') | ||
info.name = name | ||
Entity.prototype[name] = info.defaultValue | ||
Entity.prototype.meta.schema[name] = info | ||
@@ -28,0 +27,0 @@ } |
@@ -55,2 +55,46 @@ const { entity } = require('../../src/entity') | ||
it.only('should have multiple instances with isolated valued from each other', () => { | ||
//given | ||
const instance1 = givenAnEntityWithMultipleFields() | ||
const instance2 = givenAnEntityWithMultipleFields() | ||
//when | ||
instance1.field1 = 1 | ||
instance1.field2 = "1" | ||
instance1.field3 = new Date('2019-09-30T23:45:34.324Z') | ||
instance1.field4 = true | ||
const newEntity = new NewEntity() | ||
newEntity.f1 = "abc" | ||
instance1.field5 = newEntity | ||
instance1.field6 = [newEntity] | ||
const instance3 = givenAnEntityWithMultipleFields() | ||
//then | ||
assert.strictEqual(instance1['field1'], 1) | ||
assert.strictEqual(instance1['field2'], "1") | ||
assert.deepStrictEqual(instance1['field3'], new Date('2019-09-30T23:45:34.324Z')) | ||
assert.strictEqual(instance1['field4'], true) | ||
assert.deepStrictEqual(instance1['field5'], newEntity) | ||
assert.deepStrictEqual(instance1['field5'].f1, "abc") | ||
assert.deepStrictEqual(instance1['field6'][0], newEntity) | ||
assert.deepStrictEqual(instance1['field6'][0].f1, "abc") | ||
assert.strictEqual(instance2['field1'], 0) | ||
assert.strictEqual(instance2['field2'], '') | ||
assert.deepStrictEqual(instance2['field3'], null) | ||
assert.strictEqual(instance2['field4'], false) | ||
assert.deepStrictEqual(instance2['field5'], new NewEntity()) | ||
assert.deepStrictEqual(instance2['field5'].f1, '') | ||
assert.deepStrictEqual(instance2['field6'], []) | ||
assert.strictEqual(instance3['field1'], 0) | ||
assert.strictEqual(instance3['field2'], '') | ||
assert.deepStrictEqual(instance3['field3'], null) | ||
assert.strictEqual(instance3['field4'], false) | ||
assert.deepStrictEqual(instance3['field5'], new NewEntity()) | ||
assert.deepStrictEqual(instance3['field5'].f1, '') | ||
assert.deepStrictEqual(instance3['field6'], []) | ||
}) | ||
it('should validate types and have valid value', () => { | ||
@@ -57,0 +101,0 @@ //given |
@@ -269,3 +269,3 @@ const { entity } = require('../../src/entity') | ||
//then | ||
assert.deepStrictEqual(json, '{"field1":1,"field2":"1","field3":"2019-09-30T23:45:34.324Z","field4":true,"field1x":1,"field2x":"1","field3x":"2019-09-30T23:45:34.324Z","field4x":true,"field5x":{"f1":true,"f2":"2"},"field6x":[{"f1":true,"f2":"2"}],"field5":{"f1":true,"f2":"2"},"field6":[{"f1":true,"f2":"2"}]}') | ||
assert.deepStrictEqual(json, '{"field1":1,"field2":"1","field3":"2019-09-30T23:45:34.324Z","field4":true,"field5":{"f1":true,"f2":"2"},"field6":[{"f1":true,"f2":"2"}],"field1x":1,"field2x":"1","field3x":"2019-09-30T23:45:34.324Z","field4x":true,"field5x":{"f1":true,"f2":"2"},"field6x":[{"f1":true,"f2":"2"}]}') | ||
}) | ||
@@ -272,0 +272,0 @@ |
@@ -15,12 +15,9 @@ const { entity } = require('../../../src/entity') | ||
const givenAnEntityToBeUsedAsType = () => { | ||
return EntityType | ||
} | ||
const givenAnEntityWithAEntityField = (fieldOptions) => { | ||
const EntityType = givenAnEntityToBeUsedAsType() | ||
const AnEntity = entity('A entity', { | ||
field1: field([EntityType], fieldOptions) | ||
field1: field([EntityType], fieldOptions), | ||
isEmpty() { | ||
return this.field1.length === 0 | ||
}, | ||
}) | ||
@@ -46,3 +43,2 @@ return new AnEntity() | ||
//given | ||
const EntityType = givenAnEntityToBeUsedAsType() | ||
const instanceOfEntityType = new EntityType() | ||
@@ -61,3 +57,2 @@ const instance = givenAnEntityWithAEntityField({ | ||
//given | ||
const EntityType = givenAnEntityToBeUsedAsType() | ||
const instance = givenAnEntityWithAEntityField() | ||
@@ -101,3 +96,22 @@ instance.field1[0] = new EntityType() | ||
it('should have multiple instances with isolated valued from each other', () => { | ||
//given | ||
const AnEntity = entity('A entity', { | ||
field1: field([EntityType]), | ||
isEmpty() { | ||
return this.field1.length === 0 | ||
}, | ||
}) | ||
const instance1 = new AnEntity() | ||
instance1.field1.push("error") | ||
const instance2 = new AnEntity() | ||
//then | ||
assert.strictEqual(instance1.isEmpty(), false) | ||
assert.strictEqual(instance2.isEmpty(), true) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
135705
1164
332