Skip to content

Commit

Permalink
Merge pull request #230 from PlagueHO/Issue-227
Browse files Browse the repository at this point in the history
Improved validation on Document Id parameter on *-CosmosDBDocument* functions - Fixes #227
  • Loading branch information
PlagueHO authored Nov 4, 2018
2 parents 9f548ca + 27b381c commit cbc2906
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
`*-CosmosDBUser*` functions - fixes [Issue #217](https://github.com/PlagueHO/CosmosDB/issues/217).
- Improved validation on Account parameter on `*-CosmosDBUser*` functions.
- Improved validation on Database parameter on `*-CosmosDBUser*` functions.
- Improved validation on Document Id parameter on
`*-CosmosDBDocument*` functions - fixes [Issue #227](https://github.com/PlagueHO/CosmosDB/issues/227).
- Improved validation on Account parameter on `*-CosmosDBDocument*` functions.
- Improved validation on Database parameter on `*-CosmosDBDocument*` functions.
- Improved validation on Collection parameter on `*-CosmosDBDocument*` functions.

## 2.1.12.137

Expand Down
1 change: 1 addition & 0 deletions src/en-US/CosmosDB.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ ConvertFrom-StringData -StringData @'
TriggerIdInvalid = The Trigger Id '{0}' is invalid. A Trigger Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
UserDefinedFunctionIdInvalid = The User Defined Function Id '{0}' is invalid. A User Defined Function Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
UserIdInvalid = The User Id '{0}' is invalid. A User Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
DocumentIdInvalid = The Document Id '{0}' is invalid. A Document Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
'@
25 changes: 25 additions & 0 deletions src/lib/documents/Assert-CosmosDbDocumentIdValid.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<#
.SYNOPSIS
Helper function that asserts a Cosmos DB Document Id is valid.
#>
function Assert-CosmosDbDocumentIdValid
{

[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Id
)

$matches = [regex]::Match($Id,"[^\\/#?]{1,255}(?<!\s)")
if ($matches.value -ne $Id)
{
Throw $($LocalizedData.DocumentIdInvalid -f $Id)
}

return $true
}
8 changes: 4 additions & 4 deletions src/lib/documents/Get-CosmosDbDocument.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Get-CosmosDbDocument
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

Expand All @@ -27,17 +27,17 @@ function Get-CosmosDbDocument
$KeyType = 'master',

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbCollectionIdValid -Id $_ })]
[System.String]
$CollectionId,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDocumentIdValid -Id $_ })]
[System.String]
$Id,

Expand Down
6 changes: 3 additions & 3 deletions src/lib/documents/Get-CosmosDbDocumentResourcePath.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ function Get-CosmosDbDocumentResourcePath
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbCollectionIdValid -Id $_ })]
[System.String]
$CollectionId,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDocumentIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/documents/New-CosmosDbDocument.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function New-CosmosDbDocument
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

Expand All @@ -27,12 +27,12 @@ function New-CosmosDbDocument
$Key,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbCollectionIdValid -Id $_ })]
[System.String]
$CollectionId,

Expand Down
8 changes: 4 additions & 4 deletions src/lib/documents/Remove-CosmosDbDocument.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function Remove-CosmosDbDocument
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

Expand All @@ -31,12 +31,12 @@ function Remove-CosmosDbDocument
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbCollectionIdValid -Id $_ })]
[System.String]
$CollectionId,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDocumentIdValid -Id $_ })]
[System.String]
$Id,

Expand Down
8 changes: 4 additions & 4 deletions src/lib/documents/Set-CosmosDbDocument.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ function Set-CosmosDbDocument
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

Expand All @@ -32,12 +32,12 @@ function Set-CosmosDbDocument
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbCollectionIdValid -Id $_ })]
[System.String]
$CollectionId,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDocumentIdValid -Id $_ })]
[System.String]
$Id,

Expand Down
44 changes: 44 additions & 0 deletions test/Unit/CosmosDB.documents.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,50 @@ InModuleScope CosmosDB {
Headers = $script:testHeaders
}

Describe 'Assert-CosmosDbDocumentIdValid' -Tag 'Unit' {
It 'Should exist' {
{ Get-Command -Name Assert-CosmosDbDocumentIdValid -ErrorAction Stop } | Should -Not -Throw
}

Context 'When called with a valid Id' {
It 'Should return $true' {
Assert-CosmosDbDocumentIdValid -Id 'This is a valid document ID..._-99!' | Should -Be $true
}
}

Context 'When called with a 256 character Id' {
It 'Should throw expected exception' {
{
Assert-CosmosDbDocumentIdValid -Id ('a' * 256)
} | Should -Throw ($LocalizedData.DocumentIdInvalid -f ('a' * 256))
}
}

Context 'When called with an Id containing invalid characters' {
$testCases = @{ Id = 'a\b' }, @{ Id = 'a/b' }, @{ Id = 'a#b' }, @{ Id = 'a?b' }

It 'Should throw expected exception when called with "<Id>"' -TestCases $testCases {
param
(
[System.String]
$Id
)

{
Assert-CosmosDbDocumentIdValid -Id $Id
} | Should -Throw ($LocalizedData.DocumentIdInvalid -f $Id)
}
}

Context 'When called with an Id ending with a space' {
It 'Should throw expected exception' {
{
Assert-CosmosDbDocumentIdValid -Id ('a ')
} | Should -Throw ($LocalizedData.DocumentIdInvalid -f ('a '))
}
}
}

Describe 'Get-CosmosDbDocumentResourcePath' -Tag 'Unit' {
It 'Should exist' {
{ Get-Command -Name Get-CosmosDbDocumentResourcePath -ErrorAction Stop } | Should -Not -Throw
Expand Down

0 comments on commit cbc2906

Please sign in to comment.