Comparing version 1.0.14 to 1.0.15
{ | ||
"name": "gotu", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "Domain entities javascript library.", | ||
@@ -5,0 +5,0 @@ "main": "./src/gotu.js", |
@@ -123,22 +123,14 @@ <p align="center"><img src="https://raw.githubusercontent.com/herbsjs/gotu/master/docs/logo.png" height="220"></p> | ||
By default `fromJSON` allows serialize only keys that have been defined in the entity. If you want to add other keys during serialization, use `allowExtraKeys: true`. | ||
By default `fromJSON` serializes only keys that have been defined in the entity. If you want to add other keys during serialization, use `.fromJSON(data, { allowExtraKeys: true })`. | ||
```javascript | ||
const User = | ||
entity('User', { | ||
name: field(String) | ||
}) | ||
const user = User.fromJSON({ name: 'Beth', newKey: 'value'}, { allowExtraKeys: true }) | ||
// user = { name: 'Beth', newKey: 'value'} | ||
``` | ||
### `JSON.stringify(entity)` | ||
To serialize an entity to JSON string use `JSON.stringify` function. | ||
To serialize an entity to JSON string use `JSON.stringify` or `entity.toJSON` function. | ||
```javascript | ||
const json = JSON.stringify(user) // { "name": "Beth"} | ||
const json = JSON.stringify(user) // { "name": "Beth" } | ||
``` | ||
By default `toJSON` serializes only keys that have been defined in the entity. If you want to add other keys during serialization, use `entity.toJSON({ allowExtraKeys: true })`. | ||
## Field definition | ||
@@ -145,0 +137,0 @@ |
@@ -51,6 +51,11 @@ const { validate, checker } = require("suma") | ||
toJSON() { | ||
toJSON(options = { allowExtraKeys: false }) { | ||
function deepCopy(obj) { | ||
const copy = {} | ||
for (const field in obj.meta.schema) { | ||
const jsonKeys = options.allowExtraKeys ? Object.keys(obj) : [] | ||
const entityKeys = Object.keys(obj.meta.schema) | ||
const mergedKeys = jsonKeys.concat(entityKeys.filter((item) => jsonKeys.indexOf(item) < 0)) | ||
for (const field of mergedKeys) { | ||
let value = obj[field] | ||
@@ -93,10 +98,9 @@ if (value instanceof BaseEntity) value = deepCopy(value) | ||
const jsonKeys = Object.keys(data) | ||
const jsonKeys = options.allowExtraKeys ? Object.keys(data) : [] | ||
const entityKeys = Object.keys(instance.meta.schema) | ||
const keys = jsonKeys.concat(entityKeys.filter((item) => jsonKeys.indexOf(item) < 0)) | ||
const mergedKeys = jsonKeys.concat(entityKeys.filter((item) => jsonKeys.indexOf(item) < 0)) | ||
for (const key of keys) { | ||
for (const key of mergedKeys) { | ||
const field = instance.meta.schema[key] | ||
if (field === undefined) { | ||
if (!options.allowExtraKeys) continue | ||
instance[key] = data[key] | ||
@@ -103,0 +107,0 @@ continue |
@@ -158,3 +158,2 @@ const { entity } = require('../../src/entity') | ||
const AnEntity = givenAnEntityToReceiveObject() | ||
const options = { allowExtraKeys: true } | ||
@@ -179,3 +178,5 @@ //when | ||
method2() { return "Niente" } | ||
}, options) | ||
}, | ||
{ allowExtraKeys: true }) | ||
//then | ||
@@ -247,2 +248,28 @@ assert.strictEqual(instance['field1'], 1) | ||
it('should allow extra fields from JSON', () => { | ||
//given | ||
const AnEntity = givenAnEntityToBuildAJSON() | ||
const instance = new AnEntity() | ||
instance.field1 = 1 | ||
instance.field2 = "1" | ||
instance.field3 = new Date('2019-09-30T23:45:34.324Z') | ||
instance.field4 = true | ||
instance.field5.f1 = true | ||
instance.field5.f2 = "2" | ||
instance.field6.push({ f1: true, f2: "2" }) | ||
instance.field1x = 1 | ||
instance.field2x = "1" | ||
instance.field3x = new Date('2019-09-30T23:45:34.324Z') | ||
instance.field4x = true | ||
instance.field5x = { f1: true, f2: "2" } | ||
instance.field6x = [{ f1: true, f2: "2" }] | ||
//when | ||
instance.validate() | ||
const json = JSON.stringify(instance.toJSON({ allowExtraKeys: true })) | ||
//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"}]}') | ||
}) | ||
it('invalid data to JSON', () => { | ||
@@ -249,0 +276,0 @@ //given |
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
133112
1108
334