Comparing version 1.0.21 to 1.0.22
{ | ||
"name": "gotu", | ||
"version": "1.0.21", | ||
"version": "1.0.22", | ||
"description": "Domain entities javascript library.", | ||
@@ -5,0 +5,0 @@ "main": "./src/gotu.js", |
<p align="center"><img src="https://raw.githubusercontent.com/herbsjs/gotu/master/docs/logo.png" height="220"></p> | ||
![Node.js CI](https://github.com/herbsjs/gotu/workflows/Node.js%20CI/badge.svg?branch=master)[![codecov](https://codecov.io/gh/herbsjs/gotu/branch/master/graph/badge.svg)](https://codecov.io/gh/herbsjs/gotu) | ||
![Node.js CI](https://github.com/herbsjs/gotu/workflows/Node.js%20CI/badge.svg?branch=master) [![codecov](https://codecov.io/gh/herbsjs/gotu/branch/master/graph/badge.svg)](https://codecov.io/gh/herbsjs/gotu) | ||
@@ -13,3 +13,3 @@ # Gotu Kola | ||
$ npm install gotu | ||
```$ npm install gotu``` | ||
@@ -81,4 +81,20 @@ ### Using | ||
You can also simplify you validation method using `isValid()` method that execute validate for you entity and return true/false in a single execution. | ||
You can also simplify you validation method using `isValid()` method that execute validate implicit for you entity and return true/false in a single execution and also you can check the errors. | ||
```javascript | ||
const Plan = | ||
entity('Plan', { | ||
... | ||
monthlyCost: field(Number), | ||
}) | ||
const plan = new Plan() | ||
plan.plan.monthlyCost = true | ||
plan.isValid() // false | ||
plan.errors // { monthlyCost: [ wrongType: 'Number' ] } | ||
``` | ||
### Custom Validation | ||
@@ -85,0 +101,0 @@ |
@@ -65,11 +65,5 @@ const { validate, checker } = require("suma") | ||
let jsonKeys = [] | ||
let entityKeys = [] | ||
if (obj instanceof BaseEntity) { | ||
jsonKeys = allowExtraKeys ? Object.keys(obj) : [] | ||
entityKeys = Object.keys(obj.meta.schema) | ||
} | ||
else { | ||
jsonKeys = Object.keys(obj) | ||
} | ||
const jsonKeys = allowExtraKeys ? Object.keys(obj) : [] | ||
const entityKeys = Object.keys(obj.meta.schema) | ||
const mergedKeys = jsonKeys.concat(entityKeys.filter((item) => jsonKeys.indexOf(item) < 0)) | ||
@@ -80,3 +74,3 @@ | ||
if (value instanceof BaseEntity) value = deepCopy(value, allowExtraKeys) | ||
if (Array.isArray(value)) | ||
if (Array.isArray(value) && value[0] instanceof BaseEntity) | ||
value = value.map((i) => deepCopy(i, allowExtraKeys)) | ||
@@ -95,5 +89,3 @@ if (value instanceof Function) continue | ||
function parse(type, value) { | ||
if (value === undefined) return undefined | ||
if (value === null) return null | ||
if (type === Date) return new Date(value) | ||
@@ -100,0 +92,0 @@ if (type.prototype instanceof BaseEntity) { |
@@ -222,2 +222,16 @@ const { entity } = require('../../src/entity') | ||
const givenAnEntityToBuildAJSONWithArraysOfPrimitiveType = () => { | ||
const EntityType = givenAnEntityToBeUsedAsType | ||
const AnEntity = entity('A entity', { | ||
field1: field([Number]), | ||
field2: field([String]), | ||
field3: field([Object]), | ||
field4: field([Date]), | ||
field5: field([Boolean]), | ||
field6: field([EntityType]), | ||
}) | ||
return AnEntity | ||
} | ||
it('valid data to JSON', () => { | ||
@@ -274,2 +288,25 @@ //given | ||
it('valid data to JSON - primitive type arrays', () => { | ||
//given | ||
const AnEntity = givenAnEntityToBuildAJSONWithArraysOfPrimitiveType() | ||
const instance = new AnEntity() | ||
const AnTypeEntity = givenAnEntityToBeUsedAsType | ||
instance.field1 = [1, 2] | ||
instance.field2 = ["testing", "primitive"] | ||
instance.field3 = [{ f1: true, f2: "2" }] | ||
instance.field4 = [new Date('2019-09-30T23:45:34.324Z'), new Date('2019-09-30T23:45:34.324Z')], | ||
instance.field5 = [true, false], | ||
instance.field6 = [new AnTypeEntity(), new AnTypeEntity()] | ||
//when | ||
instance.validate() | ||
const json = JSON.stringify(instance.toJSON({ allowExtraKeys: true })) | ||
//then | ||
assert.deepStrictEqual( | ||
json, | ||
'{"field1":[1,2],"field2":["testing","primitive"],"field3":[{"f1":true,"f2":"2"}],"field4":["2019-09-30T23:45:34.324Z","2019-09-30T23:45:34.324Z"],"field5":[true,false],"field6":[{"errors":{}},{"errors":{}}],"errors":{}}' | ||
) | ||
}) | ||
it('invalid data to JSON', () => { | ||
@@ -309,3 +346,29 @@ //given | ||
}) | ||
it('invalid data to JSON - primitive type arrays', () => { | ||
//given | ||
const AnEntity = givenAnEntityToBuildAJSONWithArraysOfPrimitiveType() | ||
const instance = new AnEntity() | ||
instance.field1 = [1,2] | ||
instance.field2 = [1] | ||
//when | ||
instance.validate() | ||
const json = JSON.stringify(instance.toJSON()) | ||
//then | ||
assert.deepStrictEqual( | ||
json, | ||
'{"field1":[1,2],"field2":[1]}' | ||
) | ||
assert.deepStrictEqual(instance.errors, { | ||
field2: [ | ||
{ | ||
wrongType: ["String"], | ||
}, | ||
], | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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
141204
1253
354