-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(slug): extract validation logic out of schema definition
- Loading branch information
Showing
3 changed files
with
90 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import type { SanityDocument } from "@sanity/types"; | ||
|
||
export const DRAFTS_PREFIX = "drafts."; | ||
|
||
export function isPublished(document: SanityDocument) { | ||
return !isDraft(document); | ||
} | ||
|
||
export function isDraft(document: SanityDocument) { | ||
return document._id.startsWith("drafts.") || document._rev === undefined; | ||
return isDraftId(document._id) || document._rev === undefined; | ||
} | ||
|
||
export function buildDraftId(documentId: string): string { | ||
return isDraftId(documentId) ? documentId : `${DRAFTS_PREFIX}${documentId}`; | ||
} | ||
|
||
export function buildPublishedId(documentId: string): string { | ||
return isPublishedId(documentId) | ||
? documentId | ||
: documentId.slice(DRAFTS_PREFIX.length); | ||
} | ||
|
||
function isPublishedId(documentId: string) { | ||
return !isDraftId(documentId); | ||
} | ||
|
||
function isDraftId(documentId: string) { | ||
return documentId.startsWith(DRAFTS_PREFIX); | ||
} |