Added support to validate Json against string format attribute from JSON Schema#169
Conversation
|
@microsoft-github-policy-service agree company="Microsoft" |
| } | ||
| else if(string.Compare(format, FormatAttributes.Uri) == 0) | ||
| { | ||
| if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) |
There was a problem hiding this comment.
As per the documentation https://learn.microsoft.com/en-us/dotnet/api/system.uri.iswellformedoriginalstring?redirectedfrom=MSDN&view=net-7.0#System_Uri_IsWellFormedOriginalString, it should follow the uri validation spec RFC3986/RFC3987
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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""