mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
doc/hook: additional example for rejecting op (#2407)
This commit is contained in:
@@ -246,11 +246,25 @@ func (SomeMixin) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
// Execute "HookA" only for the UpdateOne and DeleteOne operations.
|
||||
hook.On(HookA(), ent.OpUpdateOne|ent.OpDeleteOne),
|
||||
|
||||
// Don't execute "HookB" on Create operation.
|
||||
hook.Unless(HookB(), ent.OpCreate),
|
||||
|
||||
// Execute "HookC" only if the ent.Mutation is changing the "status" field,
|
||||
// and clearing the "dirty" field.
|
||||
hook.If(HookC(), hook.And(hook.HasFields("status"), hook.HasClearedFields("dirty"))),
|
||||
|
||||
// Disallow changing the "password" field on Update (many) operation.
|
||||
hook.If(
|
||||
hook.FixedError(errors.New("password cannot be edited on update many")),
|
||||
hook.And(
|
||||
hook.HasOp(ent.OpUpdate),
|
||||
hook.Or(
|
||||
hook.HasFields("password"),
|
||||
hook.HasClearedFields("password"),
|
||||
),
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user