PowerShell / PowerShell/vscode-powershell

Powershell Code is working well on Powershell.exe and Powershell ISE but not working on VS Code

Open
#1,267 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area-Engine Issue-Enhancement
Dominant language
TypeScript
Stars
1.9k
Forks
547
PR merge metrics
No merged PRs in 30d

Description

I'm trying to validate Json against Schema using Powershell and Newtonsoft dlls. My script is working well as expected if I run it on Powershell.exe or Powershell ISE but it isn't working if I run it using VS Code (on same PC).

$Json_file = "D:\Json\file_sample.json"
$Json_file_wrong = "D:\Json\file_sample_wrong.json"
$Json_Schema_file = "D:\Json\schema_sample.json"

$Json = Get-Content $Json_file
$Json_wrong = Get-Content $Json_file_wrong
$SchemaJson = Get-Content $Json_Schema_file

$Json_dll = "D:\Json\Json110r1\Bin\net45\Newtonsoft.Json.dll"
$Json_Schema_dll = "D:\Json\JsonSchema30r9\Bin\net45\Newtonsoft.Json.Schema.dll"

Add-Type -Path $Json_dll
Add-Type -Path $Json_Schema_dll

$source = @'
    public class Validator
    {
        public static System.Collections.Generic.IList<string> Validate(Newtonsoft.Json.Linq.JToken token, Newtonsoft.Json.Schema.JSchema schema)
        {
            System.Collections.Generic.IList<string> messages;
            Newtonsoft.Json.Schema.SchemaExtensions.IsValid(token, schema, out messages);

            return messages;
        }
    }
'@
Add-Type -TypeDefinition $source -ReferencedAssemblies $Json_dll, $Json_Schema_dll

function Validate_Json_Against_Schema {
    param (
    [Parameter(Mandatory=$True)] $Json_param,
    [Parameter(Mandatory=$True)] $Schema_param
    )
$valid = $false

$Token = [Newtonsoft.Json.Linq.JToken]::Parse($Json_param)
$Schema = [Newtonsoft.Json.Schema.JSchema]::Parse($Schema_param)

$result = [Validator]::Validate($Token,$Schema)

if ($result.Count -eq 0)
    {
    $valid = $true
    }
return $valid
}

Validate_Json_Against_Schema $Json $SchemaJson
Validate_Json_Against_Schema $Json_wrong $SchemaJson

If I run it using VS Code (version 1.12.1, Powershell extension version 1.6.0) I have such error:

Cannot convert argument "token", with value: "{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  }
}", for "Validate" to type "Newtonsoft.Json.Linq.JToken": "Cannot convert the "{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  }
}" value of type "Newtonsoft.Json.Linq.JObject" to type "Newtonsoft.Json.Linq.JToken"."
At C:\Users\popovvg\Desktop\123.ps1:39 char:1
+ $result = [Validator]::Validate($Token,$Schema)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

My OS is fully updates Windows 10 x64.

$PSVersionTable

Name                           Value                                                                                                                               
----                           -----                                                                                                                               
PSVersion                      5.1.16299.98                                                                                                                        
PSEdition                      Desktop                                                                                                                             
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                             
BuildVersion                   10.0.16299.98                                                                                                                       
CLRVersion                     4.0.30319.42000                                                                                                                     
WSManStackVersion              3.0                                                                                                                                 
PSRemotingProtocolVersion      2.3                                                                                                                                 
SerializationVersion           1.1.0.1 

Json file:

{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC",
    "type": "business"
  }
}

Schema file:

{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },

  "type": "object",

  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": {
      "allOf": [
        { "$ref": "#/definitions/address" },
        { "properties":
          { "type": { "enum": [ "residential", "business" ] } },
          "required": ["type"]
        }
      ]
    }
  }
}

I tried to run it on Windows 7 x86 - same result. What am I doing wrong?

code -v
1.21.1
79b44aa704ce542d8ca4a3cc44cfca566e7720f1
x64
$pseditor.EditorServicesVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      6      0      0
code --list-extensions --show-versions
ms-vscode.PowerShell@1.6.0

Also I asked about my problem on stackoverflow:
https://stackoverflow.com/questions/49383121/powershell-code-is-working-well-on-powershell-exe-and-powershell-ise-but-not-wor

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing C:\Users\popovvg\Desktop\123.ps1 in the VS Code PowerShell session, focusing on Validate_Json_Against_Schema and the [Validator]::Validate call. Compare the PowerShell, Newtonsoft.Json, and Newtonsoft.Json.Schema runtime behavior with powershell.exe and ISE; done means the same script validates both JSON inputs successfully in VS Code or the incompatibility is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell, vscode
Domain
developer-experience, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.