-
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
omit entity properties into usecase #49
Comments
I like the idea of a mix a little bit your sugestions:
What do you think? |
If the goal is to have a |
@dalssoft I think when we use the herbsjs/buchu#53 approach, we create a strong coupling between the glues, I think if we implements a I liked the @jhomarolo suggestion |
How so? IMO, the suggestions on this issue creates a undesired coupling Entity -> Usecase by creating features on We should invert this dependency (Usecase -> Entity), as suggested in herbsjs/buchu#53 , so a usecase can read metadata from a entity. BTW, it would be ok to create a feature in |
I think we could to starting just avoiding the ID const User = entity('User',{
id: id(Number),
name: field(String),
password: field(String)
}
[...]
User.toValueObject()
/* return new entity
nickName
password
*/ |
@italojs do you think to start with |
|
i guess the #57 PR is only for metadata, not a feature |
@jhomarolo I implemented your suggestion #59 |
Guys, I don't recommend putting energy on this feature until we have a clear use case for it |
beside the use in usecase, that will impact the herbs2Gql and herbs2rest, sometime we need to remove the const User = entity('User', {
id: id(Number, { presence: true }),
name: field(String, { presence: true })),
email: field(String, { presence: true, validation: { email: true } })),
createdAt: field(Date, { presence: true }))
})
[...]
request: {
name: String,
email: String
}
[...]
'validate the received data': step( ctx => {
const user = User.fromJSON(ctx.req)
if(!user.isValid()) {
// I can't do this validation becase the id and createdAt is required
// to do it works, today I need to remove the "presence: true" from fields id and createdAt, it's not a good ideia
}
} with the request: User.asValueObject()
[...]
'validate the received data': step( ctx => {
const user = User.fromJSON(ctx.req)
if(!user.isValid({ avoidOmitableFields: true })){
}
} |
for this use case I can think in several approaches: (1) Check for error return Without extra implementation, it would be possible to check the returned error code: if(!user.isValid()){
if (user.error...) {
// check if the only error came from `createdAt`, regarding `presence`.
// we could create a helper function to do that
}
} (2) Validation Ignore or Override if(!user.isValid({ ignore: { createdAt: "presence"} })){ or if(!user.isValid({ override: { createdAt: { presence: false } } })){ with that there is no need to have any extra metadata on the entity (as suggested: (3) Validation Contexts Their could have be a extra metadata on the entity for validation scenarios, like: const User = entity('User', {
id: id(Number, { presence: true }),
name: field(String, { presence: true })),
email: field(String, { presence: true, validation: { email: true } })),
createdAt: field(Date, { presence: true }))
create: validation({
{ override: { createdAt: { presence: false } } }
}),
}) and if(!user.isValid({ context: 'create' })){ |
approach (1) We cant check only the approach (2) approach (3) This feature is useful, it's so necessary that exists a lib to do it (lodash.omit) and the typescript implements it as a native feature as well, we cant imagine when the developer will use it and it must to be easy to use. |
@italojs lodash's |
I'm working on this issue through herbsjs/buchu#53 |
@VictorTBX already have an pr for this #59 |
@italojs I'm following @dalssoft idea commented on herbsjs/buchu#53 Using the approach: |
This issue started as a feature request to improve converting a entity to a request object with the possibility to remove some fields. This was implemented here: herbsjs/buchu#53 After some discussion, other request appeared: the option to have some partial or custom validation for a entity. In order to keep the discussion focused, I've created a new issue to discuss this: #70 Given that, I'm closing this issue here and we can continue the discussion in the new issue. |
I would like to have a simlilar feature like ZOD omit feature
The idea is to avoid create multi entities when I need a valueobject for my usecase, e.g
My propose is:
The text was updated successfully, but these errors were encountered: