-
Notifications
You must be signed in to change notification settings - Fork 26
Added support to validate Json against string format attribute from JSON Schema #169
Changes from 4 commits
9d1c93d
21634cc
1abc5e7
02fc8bf
3986105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,6 +181,44 @@ private void ValidateString(JValue jValue, JsonSchema schema) | |
| AddResult(jValue, ErrorNumber.StringDoesNotMatchPattern, value, schema.Pattern); | ||
| } | ||
| } | ||
|
|
||
| if(!string.IsNullOrEmpty(schema.Format)) | ||
| { | ||
| ValidateStringFormat(jValue, schema.Format); | ||
| } | ||
| } | ||
|
|
||
| private void ValidateStringFormat(JValue jValue, string format) | ||
| { | ||
| string value = jValue.Value<string>(); | ||
| if (string.Compare(format, FormatAttributes.DateTime) == 0) | ||
| { | ||
| if (!DateTime.TryParse(value, out DateTime _)) | ||
| { | ||
| AddResult(jValue, ErrorNumber.StringDoesNotMatchFormat, format, value); | ||
| } | ||
| } | ||
| else if(string.Compare(format, FormatAttributes.Uri) == 0) | ||
| { | ||
| if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| AddResult(jValue, ErrorNumber.StringDoesNotMatchFormat, format, value); | ||
| } | ||
| } | ||
| else if(string.Compare(format, FormatAttributes.UriReference) == 0) | ||
| { | ||
| if (!Uri.IsWellFormedUriString(value, UriKind.RelativeOrAbsolute)) | ||
| { | ||
| AddResult(jValue, ErrorNumber.StringDoesNotMatchFormat, format, value); | ||
| } | ||
| } | ||
| else if (string.Compare(format, FormatAttributes.Uuid) == 0) | ||
| { | ||
| if (!Guid.TryParse(value, out Guid _)) | ||
| { | ||
| AddResult(jValue, ErrorNumber.StringDoesNotMatchFormat, format, value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Compute the length of a string, counting surrogate pairs as one | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.