[PS] check if JSON properties is defined (#6419)

* check if json properties not defined

* add new files
This commit is contained in:
William Cheng
2020-05-25 21:06:59 +08:00
committed by GitHub
parent c000eaef73
commit f4897ea4a4
10 changed files with 105 additions and 0 deletions

View File

@@ -85,6 +85,14 @@ function ConvertFrom-PSJsonToApiResponse {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSApiResponse
$AllProperties = ("code", "type", "message")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "code"))) { #optional property not found
$Code = $null
} else {

View File

@@ -79,6 +79,14 @@ function ConvertFrom-PSJsonToCategory {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSCategory
$AllProperties = ("id", "name")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
$Id = $null
} else {

View File

@@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToInlineObject {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSInlineObject
$AllProperties = ("name", "status")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "name"))) { #optional property not found
$Name = $null
} else {

View File

@@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToInlineObject1 {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSInlineObject1
$AllProperties = ("additionalMetadata", "file")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "additionalMetadata"))) { #optional property not found
$AdditionalMetadata = $null
} else {

View File

@@ -107,6 +107,14 @@ function ConvertFrom-PSJsonToOrder {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSOrder
$AllProperties = ("id", "petId", "quantity", "shipDate", "status", "complete")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
$Id = $null
} else {

View File

@@ -115,6 +115,14 @@ function ConvertFrom-PSJsonToPet {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSPet
$AllProperties = ("id", "category", "name", "photoUrls", "tags", "status")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
If ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") { # empty json
throw "Error! Empty JSON cannot be serialized due to the required property `name` missing."
}

View File

@@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToTag {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSTag
$AllProperties = ("id", "name")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
$Id = $null
} else {

View File

@@ -121,6 +121,14 @@ function ConvertFrom-PSJsonToUser {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in PSUser
$AllProperties = ("id", "username", "firstName", "lastName", "email", "password", "phone", "userStatus")
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
$Id = $null
} else {