Skip to content

Commit d8ae7b4

Browse files
wing328MikailBag
authored andcommitted
[PS][Experimental] Add validations to model (OpenAPITools#5842)
* add validations to model * better error message * improve validation
1 parent 7b79f81 commit d8ae7b4

File tree

6 files changed

+96
-21
lines changed

6 files changed

+96
-21
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,4 +1019,9 @@ private String toMethodName(String operationId) {
10191019
// not using powershell verb
10201020
return "Invoke-" + apiNamePrefix + methodName;
10211021
}
1022+
1023+
@Override
1024+
public String toRegularExpression(String pattern) {
1025+
return escapeText(pattern);
1026+
}
10221027
}

modules/openapi-generator/src/main/resources/powershell-experimental/model.mustache

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
2424
[CmdletBinding()]
2525
Param (
2626
{{#allVars}}
27-
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true{{#required}}, Mandatory = $true{{/required}})]
27+
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)]
28+
{{#pattern}}
29+
[ValidatePattern("{{{.}}}")]
30+
{{/pattern}}
31+
{{#isEnum}}
32+
{{#allowableValues}}
33+
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})]
34+
{{/allowableValues}}
35+
{{/isEnum}}
2836
[{{vendorExtensions.x-powershell-data-type}}]
2937
{{=<% %>=}}
3038
${<%name%>}<%^-last%>,<%/-last%>
@@ -36,6 +44,54 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
3644
'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug
3745
$PSBoundParameters | Out-DebugParameter | Write-Debug
3846
47+
{{#vars}}
48+
{{^isNullable}}
49+
{{#required}}
50+
if (!${{{name}}}) {
51+
throw "invalid value for '{{{name}}}', '{{{name}}}' cannot be null."
52+
}
53+
54+
{{/required}}
55+
{{/isNullable}}
56+
{{#hasValidation}}
57+
{{#maxLength}}
58+
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxLength}}}) {
59+
throw "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
60+
}
61+
62+
{{/maxLength}}
63+
{{#minLength}}
64+
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minLength}}}) {
65+
throw "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
66+
}
67+
68+
{{/minLength}}
69+
{{#maximum}}
70+
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMaximum}}-ge{{/exclusiveMaximum}}{{^exclusiveMaximum}}-gt{{/exclusiveMaximum}} {{{maximum}}}) {
71+
throw "invalid value for '{{{name}}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}."
72+
}
73+
74+
{{/maximum}}
75+
{{#minimum}}
76+
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMinimum}}-le{{/exclusiveMinimum}}{{^exclusiveMinimum}}-lt{{/exclusiveMinimum}} {{{minimum}}}) {
77+
throw "invalid value for '{{{name}}}', must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}."
78+
}
79+
80+
{{/minimum}}
81+
{{#maxItems}}
82+
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxItems}}}) {
83+
throw "invalid value for '{{{name}}}', number of items must be less than or equal to {{{maxItems}}}."
84+
}
85+
86+
{{/maxItems}}
87+
{{#minItems}}
88+
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minItems}}}) {
89+
throw "invalid value for '{{{name}}}', number of items must be greater than or equal to {{{minItems}}}."
90+
}
91+
92+
{{/minItems}}
93+
{{/hasValidation}}
94+
{{/vars}}
3995
$PSO = [PSCustomObject]@{
4096
{{=<< >>=}}
4197
<<#allVars>>

samples/client/petstore/powershell-experimental/Test1.ps1

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,36 @@ try {
2929
#Write-Host $pet
3030
$Result = Add-PSPet -Pet $pet
3131
Set-PSConfigurationApiKey -Id "api_key" -ApiKey "zzZZZZZZZZZZZZZ"
32-
$result = Get-PSPetById -petId $Id -Verbose #-testHeader "testing only" -testQuery "testing something here"
32+
$Result2 = Get-PSPetById -petId ($Id + 10) -Verbose -WithHttpInfo #-testHeader "testing only" -testQuery "testing something here"
33+
Write-Host $Result2.GetType()
3334
} catch {
34-
Write-Host ("Exception occured when calling '': {0}" -f ($_.ErrorDetails | ConvertFrom-Json))
35-
Write-Host ("Response headers: {0}" -f ($_.Exception.Response.Headers | ConvertTo-Json))
35+
#Write-Host ("Exception occured when calling '': {0}" -f ($_.ErrorDetails | ConvertFrom-Json))
36+
#Write-Host ("Response headers: {0}" -f ($_.Exception.Response.Headers | ConvertTo-Json))
3637
}
3738

39+
Write-Host "Before exit $($Result2.GetType())"
40+
3841
#$result | Write-Host
3942

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

4346

44-
$pet2 = Initialize-PSPet -Id 20129 -Name '2foo' -Category (
45-
Initialize-PSCategory -Id 20129 -Name '2bar'
46-
) -PhotoUrls @(
47-
'http://example.com/2foo',
48-
'http://example.com/2bar'
49-
) -Tags (
50-
Initialize-PSTag -Id 3 -Name 'baz'
51-
) -Status Available
52-
53-
#Write-Host $pet
54-
$Result = Add-PSPet -Pet $pet2
55-
56-
$Result = Find-PSPetsByTags 'baz'
57-
Write-Host $Result.GetType().Name
58-
Write-Host $Result
47+
#$pet2 = Initialize-PSPet -Id 20129 -Name '2foo' -Category (
48+
# Initialize-PSCategory -Id 20129 -Name '2bar'
49+
#) -PhotoUrls @(
50+
# 'http://example.com/2foo',
51+
# 'http://example.com/2bar'
52+
#) -Tags (
53+
# Initialize-PSTag -Id 3 -Name 'baz'
54+
#) -Status Available
55+
#
56+
##Write-Host $pet
57+
#$Result = Add-PSPet -Pet $pet2
58+
#
59+
#$Result = Find-PSPetsByTags 'baz'
60+
#Write-Host $Result.GetType().Name
61+
#Write-Host $Result
5962

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

samples/client/petstore/powershell-experimental/src/PSPetstore/Model/Category.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function Initialize-PSCategory {
3232
[System.Nullable[Int64]]
3333
${Id},
3434
[Parameter(Position = 1, ValueFromPipelineByPropertyName = $true)]
35+
[ValidatePattern("^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")]
3536
[String]
3637
${Name}
3738
)

samples/client/petstore/powershell-experimental/src/PSPetstore/Model/Order.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function Initialize-PSOrder {
5353
[System.Nullable[System.DateTime]]
5454
${ShipDate},
5555
[Parameter(Position = 4, ValueFromPipelineByPropertyName = $true)]
56+
[ValidateSet("placed", "approved", "delivered")]
5657
[String]
5758
${Status},
5859
[Parameter(Position = 5, ValueFromPipelineByPropertyName = $true)]

samples/client/petstore/powershell-experimental/src/PSPetstore/Model/Pet.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ function Initialize-PSPet {
4646
[Parameter(Position = 1, ValueFromPipelineByPropertyName = $true)]
4747
[PSCustomObject]
4848
${Category},
49-
[Parameter(Position = 2, ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
49+
[Parameter(Position = 2, ValueFromPipelineByPropertyName = $true)]
5050
[String]
5151
${Name},
52-
[Parameter(Position = 3, ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
52+
[Parameter(Position = 3, ValueFromPipelineByPropertyName = $true)]
5353
[String[]]
5454
${PhotoUrls},
5555
[Parameter(Position = 4, ValueFromPipelineByPropertyName = $true)]
5656
[PSCustomObject[]]
5757
${Tags},
5858
[Parameter(Position = 5, ValueFromPipelineByPropertyName = $true)]
59+
[ValidateSet("available", "pending", "sold")]
5960
[String]
6061
${Status}
6162
)
@@ -64,6 +65,14 @@ function Initialize-PSPet {
6465
'Creating PSCustomObject: PSPetstore => PSPet' | Write-Debug
6566
$PSBoundParameters | Out-DebugParameter | Write-Debug
6667

68+
if (!$Name) {
69+
throw "invalid value for 'Name', 'Name' cannot be null."
70+
}
71+
72+
if (!$PhotoUrls) {
73+
throw "invalid value for 'PhotoUrls', 'PhotoUrls' cannot be null."
74+
}
75+
6776
$PSO = [PSCustomObject]@{
6877
"id" = ${Id}
6978
"category" = ${Category}

0 commit comments

Comments
 (0)