question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Property parameter to Test-Json

See original GitHub issue

Summary of the new feature / enhancement

On the back of #12272 in which since that was raised both Path and SchemaPath parameters have been added this request is to add a Property parameter that accepts a string array of properties to test for.

This is so that

Test-Json -Path .\test.json -SchemaPath .\schema.json -Property Time,Date,Location 

would perhaps return True if all 3 exist False if all 3 do not exist An object that that shows which exist and which do not exist that can be easily passed to any other downstream cmdlet or application

Proposed technical implementation details (optional)

No response

Issue Analytics

  • State:open
  • Created 3 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gregsdenniscommented, Jul 9, 2023

If you only want to validate a subset of properties, you need to create a new schema that only contains those properties. Partial schema validation isn’t something that’s supported (by JSON Schema, not just this cmdlet).

For example, given the schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/my-model",
  "type": "object",
  "properties": {
    "Time": { /* ... */ },
    "Date": { /* ... */ },
    "Location": { /* ... */ },
    "Foo": { /* ... */ }
  }
}

This will validate all four properties, always. There’s no way to perform a partial validation by ignoring Foo.

If you want to only validate Time, Date, and Location, then you’ll need to create a new schema that validates just those.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/my-selective-model",
  "type": "object",
  "properties": {
    "Time": { /* ... */ },
    "Date": { /* ... */ },
    "Location": { /* ... */ }
  }
}

If you still need a schema that validates the entire thing, you can reference this new one in the other:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/my-new-model",
  "$ref": "https://example.com/my-selective-model",
  "properties": {
    "Foo": { /* ... */ }
  }
}

This new schema will validate everything in my-selective-model with the addition of the Foo property.

0reactions
gregsdenniscommented, Jul 11, 2023

if support in other tooling chains offer partial validation in future or not

You’re welcome to perform a survey of the implementations, but I doubt you’ll find one that supports partial validation specifically because it’s not a feature of JSON Schema. Implementations tend to stick as closely to the spec as their language allows.

You’re also welcome to raise a proposal, but I can say with some confidence (because I’m one of the authors) that it probably won’t be. We aim to honor the intent of the schema author. If the schema author has elected to include/require properties, then that’s what needs to be validated. If something else needs to be validated, a different schema needs to be used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add property in existing json using System.Text. ...
In the above sample JSON I want to add a property called "Name" with Value "John" inside property "TestData". How can I achieve...
Read more >
Test-Json (Microsoft.PowerShell.Utility)
The SchemaFile parameter accepts literal path to the JSON schema file and allows JSON files to be validated against such schemas. In this...
Read more >
How to Test JSON Properties in Postman
In this blog post, we'll walk through an overview of JSON architecture, structure, and its examples. Then, we'll check out some code snippets ......
Read more >
InputPath, Parameters and ResultSelector
Use the Parameters field to create a collection of key-value pairs that are passed as input. The values of each can either be...
Read more >
Documentation - Object Types
Each property in an object type can specify a couple of things: the type, whether the property is optional, and whether the property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found