Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Added support to validate Json against string format attribute from JSON Schema#169

Merged
michaelcfanning merged 5 commits into
microsoft:mainfrom
riteshksriv:users/riteshksriv/string-format-validation
Feb 15, 2023
Merged

Added support to validate Json against string format attribute from JSON Schema#169
michaelcfanning merged 5 commits into
microsoft:mainfrom
riteshksriv:users/riteshksriv/string-format-validation

Conversation

@riteshksriv

@riteshksriv riteshksriv commented Feb 3, 2023

Copy link
Copy Markdown
Contributor

Added string format validation for date-time, uri, uri-reference and uuid
Added error-messages and localization keys in English
Updated test cases to test validation for the format attribute
Tagging @shaopeng-gh and @michaelcfanning for review
For example - The following schema validates the date-time format string value.
"String: format: valid with date-time format constraints",
@"{
""type"": ""string"",
""format"": ""date-time""
}",
""2023-02-03T12:34:56.789Z""

@riteshksriv

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company="Microsoft"

Comment thread src/Json.Schema.Validation.UnitTests/ValidatorTests.cs
}
else if(string.Compare(format, FormatAttributes.Uri) == 0)
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!Uri.IsWellFormedUriString(value, UriKind.Absolute)

will this .net method about equivalent to what is valid in json schema

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API actually tells you whether the literal string is well-formed in the sense of requiring further escaping (and some other syntactic validations).

What you actually want to use here is Uri.TryCreate, if it passes, all is well.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, based on our offline discussion, you actually think (and this seems reasonable) that the expression of a URI in JSON should be normalized. This is contrary to my suggestion, which is to ensure that a specific URI parsing type (the .NET URI class) can initialize an instance based on the data that's provided.

I think you are probably correct. I'm not sure what the behavior of 'is well formed' is for issues other than encoding. Does it check for dot segments, for example?

In any case, I'm fine accepting this change as you've authored it and continuing the discussion.

@riteshksriv riteshksriv Feb 20, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked it validates the dot segment paths as well. There are issues with this API but using TryCreate succeeds for every possible values that I tried. There is an open github issue for the same. So I tried Uri.IsWellFormedUriString and it works for most cases compared to TryCreate

@shaopeng-gh shaopeng-gh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@michaelcfanning michaelcfanning left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants