herbsjs / gotu

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

Default value ignored when use fromJSON #38

Closed jhomarolo closed 3 years ago

jhomarolo commented 3 years ago

Describe the bug

Given my entity:

const { entity, field } = require('gotu')
const { v4 } = require("uuid")

const Especie = entity('Especie', {
  id: field(String, {default: () => v4()}),
  nome: field(String),
  codigo: field(String),
  siglaPais: field(String)
})

When I use var especie = Especie.fromJSON(ctx.req), the id field comes empty if I do not pass the value of id in the parameter of fromJSON function.

I believe that is a bug, since gotu returns a new instance from this object within the values were send to the function.

Expected behavior When I use fromJSON all default variables that are not passed through the function, are filled with the respective default values

Additional context This, could be a flag optional or not inside the fromJSON function or inside de default value, maybe.

dalssoft commented 3 years ago

When I use var especie = Especie.fromJSON(ctx.req), the id field comes empty if I do not pass the value of id in the parameter of fromJSON function.

It is not clear what is the value of ctx.req. Could you make it explicit?

jhomarolo commented 3 years ago

In that case would be the hole entity, except the field id

{
"puData":"2021-06-0T00:00:00.000Z",
"pagamentoJuros":3.0,
"puAmortizacao":1.2,
"pagamentoTotal":360,
"puCheio":1165.77523304,
"puVazio":1165.77523304,
"puJuros":15.55861897
}'