Skip to content

Test-Json has false positives when using anyof and allof statements in JSON schema #21471

@Dylan-Prins

Description

@Dylan-Prins

Prerequisites

Steps to reproduce

When i am using anyOf or allOf statements in my JSON schema I got false positives when the output is "False"
The issue appears on 7.4. it worked fine on 7.2

Schema

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Landingzone",
    "description": "Landingzone",
    "type": "object",
    "required": [
        "virtualNetwork"
    ],
    "additionalProperties": false,
    "properties": {
        "virtualNetwork": {
            "type": "array",
            "markdownDescription": "Wiki pagina: <br> [shit](https://dev.azure.com/CloudCompetenceCenter/Cloud%20CC/_wiki/wikis/Cloud-CC.wiki/2067/OTAP-JSON-Manual?anchor=virtual-networks) sdf ethe ",
            "description": "https://dev.azure.com/CloudCompetenceCenter/Cloud%20CC/_wiki/wikis/Cloud-CC.wiki/2067/OTAP-JSON-Manual?anchor=virtual-networks",
            "items": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "securityRules": {
                      "type": "array",
                        "items": {
                          "$ref": "#/$defs/securityRulesDetail"
                        }
                    }
                }
            }
        }
    },
    "$defs": {
        "securityRulesDetail": {
            "type": "object",
            "required": [
                "name",
                "port"
            ],
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1,
                    "$comment": "No spaces or special characters",
                    "pattern": "^([a-zA-Z0-9-\\._])+$"
                },
                "port": {
                    "type": "array",
                    "items": {
                        "oneOf": [
                            {
                                "type": "integer",
                                "minimum": 0,
                                "maximum": 66535
                            },
                            {
                                "type": "string",
                                "$comment": "range between 0 and 66535 or is *",
                                "pattern": "^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]|\\*)(-([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$"
                            }
                        ]
                    }
                }
            }
        }
    }
}

json

{
  "virtualNetwork": [
    {
      "securityRules": [
        {
          "name": "TestRule01/",
          "port": [
            443
          ]
        },
        {
          "name": "TestRule02",
          "port": [
            443
          ]
        },
        {
          "name": "TestRule03",
          "port": [
            443
          ]
        },
        {
          "name": "TestRule04",
          "port": [
            444
          ]
        },
        {
          "name": "TestRule05",
          "port": [
            443
          ]
        },
        {
          "name": "TestRule06",
          "port": [
            443
          ]
        },
        {
          "name": "Testrule07",
          "port": [
            8080
          ]
        },
        {
          "name": "Testrule08",
          "port": []
        },
        {
          "name": "Testrule09",
          "port": [
            5432
          ]
        }
      ]
    }
  ]
}

Expected behavior

`Test-Json -Schema $schema -Json $data`

Test-Json: The JSON is not valid with the schema: The string value is not a match for the indicated regular expression at '/virtualNetwork/0/securityRules/0/name'

Actual behavior

`Test-Json -Schema $schema -Json $data`

Test-Json: The JSON is not valid with the schema: The string value is not a match for the indicated regular expression at '/virtualNetwork/0/securityRules/0/name'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/0/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/1/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/2/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/3/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/4/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/5/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/6/port/0'
Test-Json: The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/8/port/0'

Error details

Exception             : 
    Type    : System.Exception
    Message : The JSON is not valid with the schema: Value is "integer" but should be "string" at '/virtualNetwork/0/securityRules/8/port/0'
    HResult : -2146233088
CategoryInfo          : InvalidData: (:) [Test-Json], Exception
FullyQualifiedErrorId : InvalidJsonAgainstSchemaDetailed,Microsoft.PowerShell.Commands.TestJsonCommand
InvocationInfo        : 
    MyCommand        : Test-Json
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 147
    Line             : Test-Json -Schema $schema -Json $data
    Statement        : Test-Json -Schema $schema -Json $data
    PositionMessage  : At line:1 char:1
                       + Test-Json -Schema $schema -Json $data
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Test-Json
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Darwin 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:25 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6030
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions