Skip to content
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

Document id / url type guards can accept unknown #362

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/automerge-repo/src/AutomergeUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ export const stringifyAutomergeUrl = (
* Given a string, returns true if it is a valid Automerge URL. This function also acts as a type
* discriminator in Typescript.
*/
export const isValidAutomergeUrl = (
str: string | undefined | null
): str is AutomergeUrl => {
export const isValidAutomergeUrl = (str: unknown): str is AutomergeUrl => {
if (typeof str !== "string") return false
if (!str || !str.startsWith(urlPrefix)) return false
const automergeUrl = str as AutomergeUrl
try {
Expand All @@ -70,7 +69,8 @@ export const isValidAutomergeUrl = (
}
}

export const isValidDocumentId = (str: string): str is DocumentId => {
export const isValidDocumentId = (str: unknown): str is DocumentId => {
if (typeof str !== "string") return false
// try to decode from base58
const binaryDocumentID = documentIdToBinary(str as DocumentId)
if (binaryDocumentID === undefined) return false // invalid base58check encoding
Expand All @@ -80,8 +80,8 @@ export const isValidDocumentId = (str: string): str is DocumentId => {
return Uuid.validate(documentId)
}

export const isValidUuid = (str: string): str is LegacyDocumentId =>
Uuid.validate(str)
export const isValidUuid = (str: unknown): str is LegacyDocumentId =>
typeof str === "string" && Uuid.validate(str)

/**
* Returns a new Automerge URL with a random UUID documentId. Called by Repo.create(), and also used by tests.
Expand Down
Loading