Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJSON() should initialize field values accordingly to the entity. #29

Open
m7vicente opened this issue Mar 22, 2021 · 12 comments
Open
Labels
enhancement New feature or request hacktoberfest help wanted Extra attention is needed severity-major Item is very important wip Work in progress

Comments

@m7vicente
Copy link
Contributor

When created an entity instance using fromJSON() method, in some cases, the entire json object has all attributes defined as string, but these values are not ignored by fromJSON(), they just set these values in the created instance.

Since parse function in fromJSON can create BaseEntity and Dates from string types it's possible to parse Numbers and strings accordingly to the definition in the entity.

@m7vicente m7vicente added the enhancement New feature or request label Mar 22, 2021
@dalssoft
Copy link
Member

Could you give a example of a input and the desired output?

@m7vicente
Copy link
Contributor Author

m7vicente commented Mar 22, 2021

const AnEntity = entity('An entity', {
                field1: field(Number),
                field2: field(String),
                field3: field(Date),
                field4: field(Boolean)
            })

const instance = AnEntity.fromJSON({
                    "field1": "1", 
                    "field2": "1",
                    "field3": "2019-09-30T23:45:00.000Z",
                    "field4": "true"
             })

instance.field1 // should be an Number
instance.field2 // should be an String
instance.field3 // should be an Date
instance.field4 // should be an Boolean

@dalssoft
Copy link
Member

What if there was a new options = { parseValue: true }?

@m7vicente
Copy link
Contributor Author

I think this should be since the case doesn't whos occurs all time.

Another thing, in some cases, the parse could occur wrong, to handle those cases, let de value be the error like NaN or InvalidDate, to make these errors more visible, run isValid to inform those errors from parse in the instance .errors (only if the option its enable).

@jhomarolo
Copy link
Contributor

Maybe the value parse itself, can be coupled with the isvalid() call or something related to that

I keep wondering, if the isvalid() call inside fromJson(), as a parameter would have any gain, since every time we call fromJson, we call isvalid on the next line of code.

I also believe that these parsers, can already reuse the suma checkers, which are already implemented within this gotu class, instead of creating redundant validations.

@dalssoft
Copy link
Member

There could be a method to force parse values.

For example:

const AnEntity = entity('An entity', { field1: field(Number) })

const instance = AnEntity.fromJSON({ "field1": "1" })

instance.field1 // is a String

instance.parseFields()

instance.field1 // should be an Number

In this case we should think what would be the behavior for invalid parses.

@italojs
Copy link
Member

italojs commented Sep 21, 2021

I think this comportment is a little bit weird

@m7vicente in your example, why not just explicitly convert the values?

[...]
const instance = AnEntity.fromJSON({
"field1": parseInt("1"),
"field2":  parseInt("1"),
"field3": new Date("2019-09-30T23:45:00.000Z"),
"field4": myValue === 'true'
})

@dalssoft
Copy link
Member

Another suggestion:

const AnEntity = entity('An entity', { field1: field(Number), field2: field(Date) })

const instance = AnEntity.fromJSON({ "field1": "1", "field2": "2" })
instance.field1 // is a String
instance.field2 // is a String

const instance = AnEntity.tryParse({ "field1": "1", "field2": "2" })
instance.field1 // should be an Number
instance.field2 // still a String since it could not parse to Date

@jhomarolo jhomarolo added blocked Something is blocked help wanted Extra attention is needed severity-major Item is very important and removed blocked Something is blocked labels Dec 24, 2021
@m7vicente-vortx
Copy link
Contributor

m7vicente-vortx commented Jan 15, 2022

If the tryParse return the instance we can execute after an fromJSON not adding a lot of complexy to use.

const values = ({ "field1": "1", "field2": "2" } const instance = AnEntity.fromJSON(values).tryParse()

@m7vicente-vortx
Copy link
Contributor

Work in Progress

@jhomarolo jhomarolo added the wip Work in progress label Jan 15, 2022
dalssoft added a commit that referenced this issue Mar 29, 2023
@dalssoft
Copy link
Member

dalssoft commented Mar 29, 2023

@m7vicente @jhomarolo @italojs please, review this commit 2efb440 (branch tryParse).

doc: herbsjs/herbsjs.github.io@8f730d4

The idea was to create a conservative tryParse, not relying completely on the messy js type/data parser. The idea is to balance the best effort of a parser and but also try to avoid surprises for the developer (ex: a field(Date) with value 1 being parsed to '1969-12-01').

Please, give me your feedback asap. I need this feature to use in a glue improvement.

@jhomarolo
Copy link
Contributor

Are there any known test battery scenarios where this method will fail?
2efb440#diff-92f07723b5f0b9daa54ffc2a317a02a8a7f45e5b860a1b1b96604d1984ce0e8c

github-actions bot pushed a commit that referenced this issue Mar 29, 2023
# [1.3.0-beta.1](v1.2.0...v1.3.0-beta.1) (2023-03-29)

### Features

* **entity:** try Parse - a method to cast the data loaded to a entity to the correct type ([2efb440](2efb440)), closes [#29](#29)
github-actions bot pushed a commit that referenced this issue Nov 22, 2023
# [1.3.0](v1.2.0...v1.3.0) (2023-11-22)

### Bug Fixes

* 🐛 update eslint ([903f7da](903f7da))
* 🐛 update suma ([de458fd](de458fd))
* **base entity:** fix bug on schema ids ([06b6f9b](06b6f9b))

### Features

* **entity:** try Parse - a method to cast the data loaded to a entity to the correct type ([2efb440](2efb440)), closes [#29](#29)
* **validation:** onlyIDs - validate only ID fields ([cc08622](cc08622))
* **validation:** reference Validation - Validation option only for reference/entity types ([39faa00](39faa00))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest help wanted Extra attention is needed severity-major Item is very important wip Work in progress
Projects
None yet
Development

No branches or pull requests

5 participants