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.

Test-Json string format not matching

See original GitHub issue

Prerequisites

Steps to reproduce

Let’s create a very simple json schema that use the string format duration :

{
	"type": "object",
	"additionalProperties":false,
	"properties": {
		"duration": {
			"type": "string",
			"format": "duration"
		}
	}
}

the format duration was added in the json schema “draft 2019-09” current version is “draft 2020-12”. The goal of the duration format is to match ISO 8601 (see: json schema documentation )

and save it in a file named schema.json

and run the following command to test:

> Test-Json -SchemaFile .\schema.json -Json '{"duration":"test123"}'

or

> Test-Json -SchemaFile .\schema.json -Json '{"duration":"PT1D"}'

the result will be True when expecting False

Expected behavior

PS> Test-Json -SchemaFile .\schema.json -Json '{"duration":"test123"}'
False
PS> Test-Json -SchemaFile .\schema.json -Json '{"duration":"PT1D"}'
False


PS> Test-Json -SchemaFile .\schema.json -Json '{"duration":"P1D"}'
True

Actual behavior

PS> Test-Json -SchemaFile .\schema.json -Json '{"duration":"test123"}'
True
PS> Test-Json -SchemaFile .\schema.json -Json '{"duration":"PT1D"}'
True


PS> Test-Json -SchemaFile .\schema.json -Json '{"duration":"P1D"}'
True

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.4
PSEdition                      Core
GitCommitId                    7.4.0-preview.4
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Issue Analytics

  • State:open
  • Created 3 months ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
gregsdenniscommented, Jul 9, 2023

In JSON Schema, format is an annotation by default. That means it doesn’t validate. So the current behavior is actually more correct, per the spec.

The two solutions I see here are:

  1. Just internally configure JsonSchema.Net to validate format by default. This may align with expected behavior, but you wouldn’t be able to turn it off.
  2. Add a new PowerShell parameter that enables that configuration, e.g. -ValidateFormat.

The fix is pretty easy; we just need to decide which to do.

0reactions
iSazonovcommented, Jul 8, 2023

@gregsdennis Thanks! I hope WG will do a conclusion. /cc @SteveL-MSFT

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test if a string is JSON or not?
I think something like following method should do the job, it returns the parsed JSON (in case of valid JSON), so you don't...
Read more >
Check Whether a String Is Valid JSON in Java
The most lightweight and simple library is the JSON API. The common approach for checking if a String is a valid JSON is...
Read more >
How to Validate Your JSON Using JSON Schema
JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You...
Read more >
JSON Online Validator and Formatter - JSON Lint
JSONLint is the free online validator, json formatter, and json beautifier tool for JSON, a lightweight data-interchange format. You can format json, ...
Read more >
Validate JSON data using Python
In this article, we will see how to validate JSON data using Python. ... Here we are validating the Python dictionary in a...
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