Skip to content

Commit

Permalink
[PS][Experimental] Add validations to model (OpenAPITools#5842)
Browse files Browse the repository at this point in the history
* add validations to model

* better error message

* improve validation
  • Loading branch information
wing328 authored and michaelpro1 committed May 7, 2020
1 parent b32dda5 commit 046f366
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,9 @@ private String toMethodName(String operationId) {
// not using powershell verb
return "Invoke-" + apiNamePrefix + methodName;
}

@Override
public String toRegularExpression(String pattern) {
return escapeText(pattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
[CmdletBinding()]
Param (
{{#allVars}}
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true{{#required}}, Mandatory = $true{{/required}})]
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)]
{{#pattern}}
[ValidatePattern("{{{.}}}")]
{{/pattern}}
{{#isEnum}}
{{#allowableValues}}
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})]
{{/allowableValues}}
{{/isEnum}}
[{{vendorExtensions.x-powershell-data-type}}]
{{=<% %>=}}
${<%name%>}<%^-last%>,<%/-last%>
Expand All @@ -36,6 +44,54 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug
{{#vars}}
{{^isNullable}}
{{#required}}
if (!${{{name}}}) {
throw "invalid value for '{{{name}}}', '{{{name}}}' cannot be null."
}

{{/required}}
{{/isNullable}}
{{#hasValidation}}
{{#maxLength}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxLength}}}) {
throw "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
}

{{/maxLength}}
{{#minLength}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minLength}}}) {
throw "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
}

{{/minLength}}
{{#maximum}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMaximum}}-ge{{/exclusiveMaximum}}{{^exclusiveMaximum}}-gt{{/exclusiveMaximum}} {{{maximum}}}) {
throw "invalid value for '{{{name}}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}."
}

{{/maximum}}
{{#minimum}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMinimum}}-le{{/exclusiveMinimum}}{{^exclusiveMinimum}}-lt{{/exclusiveMinimum}} {{{minimum}}}) {
throw "invalid value for '{{{name}}}', must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}."
}

{{/minimum}}
{{#maxItems}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxItems}}}) {
throw "invalid value for '{{{name}}}', number of items must be less than or equal to {{{maxItems}}}."
}

{{/maxItems}}
{{#minItems}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minItems}}}) {
throw "invalid value for '{{{name}}}', number of items must be greater than or equal to {{{minItems}}}."
}

{{/minItems}}
{{/hasValidation}}
{{/vars}}
$PSO = [PSCustomObject]@{
{{=<< >>=}}
<<#allVars>>
Expand Down
39 changes: 21 additions & 18 deletions samples/client/petstore/powershell-experimental/Test1.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,36 @@ try {
#Write-Host $pet
$Result = Add-PSPet -Pet $pet
Set-PSConfigurationApiKey -Id "api_key" -ApiKey "zzZZZZZZZZZZZZZ"
$result = Get-PSPetById -petId $Id -Verbose #-testHeader "testing only" -testQuery "testing something here"
$Result2 = Get-PSPetById -petId ($Id + 10) -Verbose -WithHttpInfo #-testHeader "testing only" -testQuery "testing something here"
Write-Host $Result2.GetType()
} catch {
Write-Host ("Exception occured when calling '': {0}" -f ($_.ErrorDetails | ConvertFrom-Json))
Write-Host ("Response headers: {0}" -f ($_.Exception.Response.Headers | ConvertTo-Json))
#Write-Host ("Exception occured when calling '': {0}" -f ($_.ErrorDetails | ConvertFrom-Json))
#Write-Host ("Response headers: {0}" -f ($_.Exception.Response.Headers | ConvertTo-Json))
}

Write-Host "Before exit $($Result2.GetType())"

#$result | Write-Host

#$result | Select-Object -Property "photoUrls" | ConvertTo-Json | Write-Host
#Write-Host "result =" + $result.photoUrls


$pet2 = Initialize-PSPet -Id 20129 -Name '2foo' -Category (
Initialize-PSCategory -Id 20129 -Name '2bar'
) -PhotoUrls @(
'http://example.com/2foo',
'http://example.com/2bar'
) -Tags (
Initialize-PSTag -Id 3 -Name 'baz'
) -Status Available

#Write-Host $pet
$Result = Add-PSPet -Pet $pet2

$Result = Find-PSPetsByTags 'baz'
Write-Host $Result.GetType().Name
Write-Host $Result
#$pet2 = Initialize-PSPet -Id 20129 -Name '2foo' -Category (
# Initialize-PSCategory -Id 20129 -Name '2bar'
#) -PhotoUrls @(
# 'http://example.com/2foo',
# 'http://example.com/2bar'
#) -Tags (
# Initialize-PSTag -Id 3 -Name 'baz'
#) -Status Available
#
##Write-Host $pet
#$Result = Add-PSPet -Pet $pet2
#
#$Result = Find-PSPetsByTags 'baz'
#Write-Host $Result.GetType().Name
#Write-Host $Result

#$Result = Invoke-PetApiUpdatePetWithForm -petId $Id -Name "PowerShell Update" -Status "Pending"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function Initialize-PSCategory {
[System.Nullable[Int64]]
${Id},
[Parameter(Position = 1, ValueFromPipelineByPropertyName = $true)]
[ValidatePattern("^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")]
[String]
${Name}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function Initialize-PSOrder {
[System.Nullable[System.DateTime]]
${ShipDate},
[Parameter(Position = 4, ValueFromPipelineByPropertyName = $true)]
[ValidateSet("placed", "approved", "delivered")]
[String]
${Status},
[Parameter(Position = 5, ValueFromPipelineByPropertyName = $true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ function Initialize-PSPet {
[Parameter(Position = 1, ValueFromPipelineByPropertyName = $true)]
[PSCustomObject]
${Category},
[Parameter(Position = 2, ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
[Parameter(Position = 2, ValueFromPipelineByPropertyName = $true)]
[String]
${Name},
[Parameter(Position = 3, ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
[Parameter(Position = 3, ValueFromPipelineByPropertyName = $true)]
[String[]]
${PhotoUrls},
[Parameter(Position = 4, ValueFromPipelineByPropertyName = $true)]
[PSCustomObject[]]
${Tags},
[Parameter(Position = 5, ValueFromPipelineByPropertyName = $true)]
[ValidateSet("available", "pending", "sold")]
[String]
${Status}
)
Expand All @@ -64,6 +65,14 @@ function Initialize-PSPet {
'Creating PSCustomObject: PSPetstore => PSPet' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug

if (!$Name) {
throw "invalid value for 'Name', 'Name' cannot be null."
}

if (!$PhotoUrls) {
throw "invalid value for 'PhotoUrls', 'PhotoUrls' cannot be null."
}

$PSO = [PSCustomObject]@{
"id" = ${Id}
"category" = ${Category}
Expand Down

0 comments on commit 046f366

Please sign in to comment.