herbsjs / gotu

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

ID Fields #48

Closed thgpdr closed 2 years ago

thgpdr commented 3 years ago

Fixes #46

Proposed Changes

Adding ID field option on metadata.

Took the liberty to add a type validation for ID fields (only Number and String), if the wrong type is sent, gotu will ignore the isID option. Hope it's consensus among all that we need something like that, but I did not implemented nothing fancy for now.

Any feedbacks? Thanks

Readiness Checklist

Author/Contributor

Reviewing Maintainer

codecov[bot] commented 3 years ago

Codecov Report

Merging #48 (59a1d2b) into master (91c62d1) will not change coverage. The diff coverage is 100.00%.

:exclamation: Current head 59a1d2b differs from pull request most recent head ffce3da. Consider uploading reports for the commit ffce3da to get more accurate results Impacted file tree graph

@@            Coverage Diff            @@
##            master       #48   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         4    +1     
  Lines          103       108    +5     
=========================================
+ Hits           103       108    +5     
Impacted Files Coverage Δ
src/field.js 100.00% <100.00%> (ø)
src/id.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 91c62d1...ffce3da. Read the comment docs.

dalssoft commented 3 years ago

I think we are going in the right direction. One point: is the PR name using the correct semantics? https://github.com/herbsjs/gotu/blob/master/.github/CONTRIBUTING.md

dalssoft commented 3 years ago

Something to think about before we put the idea of IDs in the lib: What should the behavior of .validate() be? Ex: If a field is an ID should it allow null (allowNull: false)?

jhomarolo commented 3 years ago

Something to think about before we put the idea of IDs in the lib: What should the behavior of .validate() be? Ex: If a field is an ID should it allow null (allowNull: false)?

For me it's not a problem that the ID is blank/null, because sometimes, in an insertion case, the ID only exists after an insertion in the database.

dalssoft commented 3 years ago

For me it's not a problem that the ID is blank/null, because sometimes, in an insertion case, the ID only exists after an insertion in the database.

Good point. So what should be the DX here?

Scenario 1 - ID should not be null .validate() will fail for new entities (it shouldn't) and it will also fail for others UCs like update for entities with no IDs (it's ok)

Scenario 2 - ID can be null .validate() will not fail for new entities (it's ok) but it will also not fail for others UCs like update for entities with no IDs (it shouldn't)

So, should we have many validators (ex: .validateForUpdate())? Any ideas?

thgpdr commented 3 years ago

I dont know about many validators like.validateForUpdate(). Maybe the responsibility of checking the id presence for a create/update scenario should be more manual, because its very specific.

What about .validate({ checkId: true })?

dalssoft commented 3 years ago

What about .validate({ checkId: true })?

Suggestion: .validate({ exceptIDs: true }) - exceptions should be a opt-in

@thgpdr can you try implement it on this PR?

thgpdr commented 3 years ago

@dalssoft

Suggestion: .validate({ exceptIDs: true }) - exceptions should be a opt-in

nice, I like this one better! What about other fields? Should we give them the exception option too?

@thgpdr can you try implement it on this PR?

sure, no problem!

jhomarolo commented 3 years ago

@thgpdr maybe like this:

.validate().except([ 'id', 'createdAt', 'myOtherProperty']) or .validate( except : [ 'id', 'createdAt', 'myOtherProperty'] )

what do you think?

dalssoft commented 3 years ago

@jhomarolo the reason I like .validate({ exceptIDs: true }) more is because it has a clear use case: Create XYZ use cases. The CLI just need to create the file once and don't have to change it even if the entity changes.

.validate( except : [ 'id', 'createdAt', 'myOtherProperty'] ) might be more flexible, but less declarative. However, It might be useful in the future for more advanced scenarios. For now I would go for exceptIDs

jhomarolo commented 3 years ago

@thgpdr any news about this PR?