feat(shared): Add workspace slug validation and errors (#3009)

This commit is contained in:
andrewwallacespeckle 2024-09-17 08:09:19 +01:00 коммит произвёл GitHub
Родитель bd8da2e4bf
Коммит f893ba29a2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 39 добавлений и 0 удалений

Просмотреть файл

@ -4,3 +4,4 @@ export * as SpeckleViewer from './viewer/index.js'
// export * as Environment from './environment/index.js' // Import from @speckle/shared/dist/...
export * as Automate from './automate/index.js'
export * from './core/index.js'
export * from './workspaces/index.js'

Просмотреть файл

@ -0,0 +1,37 @@
export const VALID_SLUG_CHARACTERS_REGEX = /^[a-z0-9-]+$/
export const VALID_SLUG_BOUNDARY_REGEX = /^[a-z0-9].*[a-z0-9]$/
const MIN_SLUG_LENGTH = 3
const MAX_SLUG_LENGTH = 30
export class InvalidWorkspaceSlugError extends Error {
constructor(message: string) {
super(message)
this.name = 'InvalidWorkspaceSlugError'
}
}
export function validateWorkspaceSlug(slug: string): void {
if (slug.length < MIN_SLUG_LENGTH) {
throw new InvalidWorkspaceSlugError(
`Workspace slug must be at least ${MIN_SLUG_LENGTH} characters long.`
)
}
if (slug.length > MAX_SLUG_LENGTH) {
throw new InvalidWorkspaceSlugError(
`Workspace slug must not exceed ${MAX_SLUG_LENGTH} characters.`
)
}
if (!VALID_SLUG_CHARACTERS_REGEX.test(slug)) {
throw new InvalidWorkspaceSlugError(
'Workspace slug must contain only lowercase letters, numbers, and hyphens.'
)
}
if (!VALID_SLUG_BOUNDARY_REGEX.test(slug)) {
throw new InvalidWorkspaceSlugError(
'Workspace slug cannot start or end with a hyphen.'
)
}
}

Просмотреть файл

@ -0,0 +1 @@
export * from './errors/index.js'