Commit f470699
fix: draft doc validation when duplicating docs (#15816)
### What?
This PR defaults the `draft` value to true when it is not set during a
duplicate operation.
### Why?
It validates the required fields even if the user is trying to duplicate
a draft doc.
The regression was introduced when
[`packages/payload/src/collections/endpoints/duplicate.ts`](https://github.com/payloadcms/payload/blob/v3.77.0/packages/payload/src/collections/endpoints/duplicate.ts)
was refactored to use the shared `parseParams` utility instead of
manually parsing query parameters.
**v3.62.0 (working)** — In [v3.62.0 of
`duplicate.ts`](https://github.com/payloadcms/payload/blob/v3.62.0/packages/payload/src/collections/endpoints/duplicate.ts),
the `draft` parameter was explicitly defaulted to `true`:
```typescript
// draft defaults to true, unless explicitly set requested as false
// to prevent the newly duplicated document from being published
const draft = searchParams.get('draft') !== 'false'
```
**v3.77.0 (broken)** — In [v3.77.0 of
`duplicate.ts`](https://github.com/payloadcms/payload/blob/v3.77.0/packages/payload/src/collections/endpoints/duplicate.ts),
the endpoint uses
[`parseParams`](https://github.com/payloadcms/payload/blob/v3.77.0/packages/payload/src/utilities/parseParams/index.ts):
```typescript
const { depth, draft, populate, select, selectedLocales } = parseParams(req.query)
```
`parseParams` only sets `draft` to `true` if the query string explicitly
contains `?draft=true`. The admin UI's
[`DuplicateDocument`](https://github.com/payloadcms/payload/blob/v3.77.0/packages/ui/src/elements/DuplicateDocument/index.tsx)
component does **not** send `?draft=true` as a query parameter, so
`draft` is `undefined`.
The UI does send `{ _status: 'draft' }` in the request body, but that
does not influence the `draft` flag. In
[`createOperation`](https://github.com/payloadcms/payload/blob/v3.77.0/packages/payload/src/collections/operations/create.ts),
the validation skip logic depends entirely on the `draft` argument:
```typescript
const isSavingDraft = Boolean(draft && hasDraftsEnabled(collectionConfig) && !publishAllLocales)
// ...
skipValidation: isSavingDraft && !hasDraftValidationEnabled(collectionConfig),
```
Since `draft` is `undefined`, `isSavingDraft` is `false`,
`skipValidation` is `false`, and full validation runs — rejecting the
duplicate when any required fields are empty.
### How?
By setting the `draft` value as `true` by default when it is not set
during a duplicate operation:
Fixes #15815
---------
Co-authored-by: German Jablonski <GermanJablo@users.noreply.github.com>1 parent f0498f2 commit f470699
File tree
2 files changed
+58
-1
lines changed- packages/payload/src/collections/endpoints
- test/versions
2 files changed
+58
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
476 | 476 | | |
477 | 477 | | |
478 | 478 | | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
479 | 536 | | |
480 | 537 | | |
481 | 538 | | |
| |||
0 commit comments