You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: clarify beforeValidate and beforeChange hook data behavior (#15300)
- Updated originalDoc descriptions in both beforeValidate and
beforeChange hooks
- Added warning banners explaining partial data behavior
- Improved beforeChange example showing practical validation with
operation checking
|**`collection`**| The [Collection](../configuration/collections) in which this Hook is running against. |
87
87
|**`context`**| Custom context passed between Hooks. [More details](./context). |
88
88
|**`operation`**| The name of the operation that this hook is running within. |
@@ -117,20 +117,40 @@ The following arguments are provided to the `beforeValidate` hook:
117
117
|**`context`**| Custom context passed between Hooks. [More details](./context). |
118
118
|**`data`**| The incoming data passed through the operation. |
119
119
|**`operation`**| The name of the operation that this hook is running within. |
120
-
|**`originalDoc`**| The Document before changes are applied. |
120
+
|**`originalDoc`**| The full document before changes are applied. Present on updates; undefined on creates. Use this to read the document id and any unchanged fields.|
121
121
|**`req`**| The [Web Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. This is mocked for [Local API](../local-api/overview) operations. |
122
122
123
+
**Why isn’t id in data?**
124
+
125
+
`data` only represents the delta you’re saving. Read `originalDoc.id` during updates, and prefer `afterChange` if you need the `id` during creates
126
+
127
+
<Bannertype="warning">
128
+
On update operations, data contains only the fields being changed. It may omit
129
+
id and any unchanged fields. Use originalDoc to read existing values. On
130
+
create, data is the new submission and the document id is not yet available at
131
+
this stage.
132
+
</Banner>
133
+
123
134
### beforeChange
124
135
125
136
Immediately before validation, beforeChange hooks will run during create and update operations. At this stage, the data should be treated as unvalidated user input. There is no guarantee that required fields exist or that fields are in the correct format. As such, using this data for side effects requires manual validation. You can optionally modify the shape of the data to be saved.
originalDoc, // Full doc before changes (defined on update)
144
+
operation, // 'create' | 'update'
133
145
}) => {
146
+
// Need the id? Don't expect it in `data`.
147
+
const id =operation==='update'?originalDoc.id:undefined
148
+
149
+
// Example: enforce that title cannot be cleared on update
150
+
if (operation==='update'&&'title'in (data?? {}) &&!data.title) {
151
+
thrownewError(`Document ${id} must have a title`)
152
+
}
153
+
134
154
returndata
135
155
}
136
156
```
@@ -143,9 +163,20 @@ The following arguments are provided to the `beforeChange` hook:
143
163
|**`context`**| Custom context passed between hooks. [More details](./context). |
144
164
|**`data`**| The incoming data passed through the operation. |
145
165
|**`operation`**| The name of the operation that this hook is running within. |
146
-
|**`originalDoc`**| The Document before changes are applied. |
166
+
|**`originalDoc`**| The full document before changes are applied. Present on updates; undefined on creates. Use this to read the document id and any unchanged fields.|
147
167
|**`req`**| The [Web Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. This is mocked for [Local API](../local-api/overview) operations. |
148
168
169
+
**Why isn’t id in data?**
170
+
171
+
`data` only represents the delta you’re saving. Read `originalDoc.id` during updates, and prefer `afterChange` if you need the `id` during creates
172
+
173
+
<Bannertype="warning">
174
+
On update operations, data contains only the fields being changed. It may omit
175
+
id and any unchanged fields. Use originalDoc to read existing values. On
176
+
create, data is the new submission and the document id is not yet available at
177
+
this stage.
178
+
</Banner>
179
+
149
180
### afterChange
150
181
151
182
After a document is created or updated, the `afterChange` hook runs. This hook is helpful to recalculate statistics such as total sales within a global, syncing user profile changes to a CRM, and more.
Copy file name to clipboardExpand all lines: docs/hooks/globals.mdx
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,9 +104,16 @@ The following arguments are provided to the `beforeValidate` hook:
104
104
|**`global`**| The [Global](../configuration/globals) in which this Hook is running against. |
105
105
|**`context`**| Custom context passed between Hooks. [More details](./context). |
106
106
|**`data`**| The incoming data passed through the operation. |
107
-
|**`originalDoc`**| The Document before changes are applied. |
107
+
|**`originalDoc`**| The full document before changes are applied. Present on updates; undefined on creates. Use this to read the document id and any unchanged fields.|
108
108
|**`req`**| The [Web Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. This is mocked for [Local API](../local-api/overview) operations. |
109
109
110
+
<Bannertype="warning">
111
+
On update operations, data contains only the fields being changed. It may omit
112
+
id and any unchanged fields. Use originalDoc to read existing values. On
113
+
create, data is the new submission and the document id is not yet available at
114
+
this stage.
115
+
</Banner>
116
+
110
117
### beforeChange
111
118
112
119
Immediately following validation, `beforeChange` hooks will run within the `update` operation. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.
@@ -131,9 +138,16 @@ The following arguments are provided to the `beforeChange` hook:
131
138
|**`global`**| The [Global](../configuration/globals) in which this Hook is running against. |
132
139
|**`context`**| Custom context passed between hooks. [More details](./context). |
133
140
|**`data`**| The incoming data passed through the operation. |
134
-
|**`originalDoc`**| The Document before changes are applied. |
141
+
|**`originalDoc`**| The full document before changes are applied. Present on updates; undefined on creates. Use this to read the document id and any unchanged fields.|
135
142
|**`req`**| The [Web Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. This is mocked for [Local API](../local-api/overview) operations. |
136
143
144
+
<Bannertype="warning">
145
+
On update operations, data contains only the fields being changed. It may omit
146
+
id and any unchanged fields. Use originalDoc to read existing values. On
147
+
create, data is the new submission and the document id is not yet available at
148
+
this stage.
149
+
</Banner>
150
+
137
151
### afterChange
138
152
139
153
After a global is updated, the `afterChange` hook runs. Use this hook to purge caches of your applications, sync site data to CRMs, and more.
0 commit comments