-
Notifications
You must be signed in to change notification settings - Fork 19
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
Comments
Could you give a example of a input and the desired output? |
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 |
What if there was a new |
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 |
Maybe the value parse itself, can be coupled with the I keep wondering, if the 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. |
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. |
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'
}) |
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 |
If the
|
Work in Progress |
@m7vicente @jhomarolo @italojs please, review this commit 2efb440 (branch 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. |
Are there any known test battery scenarios where this method will fail? |
# [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)
# [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))
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 byfromJSON()
, 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.The text was updated successfully, but these errors were encountered: