Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/Json.Schema.Validation.UnitTests/ValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,82 @@ public void Serialize(IXunitSerializationInfo info)
}
}",
MakeErrorMessage(2, 8, "a", ErrorNumber.DependentPropertyMissing, "x", "\"y\"", "\"y\"")
),
new TestCase(
"String: format: valid with date-time format constraints",
@"{
""type"": ""string"",
""format"": ""date-time""
}",
"\"2023-02-03T12:34:56.789Z\""
),
new TestCase(
"String: format: invalid date-time format string",
@"{
""type"": ""string"",
""format"": ""date-time""
}",
"\"failure-string\"",
MakeErrorMessage(1, 16, string.Empty, ErrorNumber.StringDoesNotMatchFormat, FormatAttributes.DateTime, "failure-string")
),
new TestCase(
"String: format: valid with uri format constraint",
@"{
""type"": ""string"",
""format"": ""uri""
}",
"\"https://www.example.com\""
Comment thread
riteshksriv marked this conversation as resolved.
),
new TestCase(
"String: format: Invalid uri value",
@"{
""type"": ""string"",
""format"": ""uri""
}",
"\"sites/files/images/picture.png\"",
MakeErrorMessage(1, 32, string.Empty, ErrorNumber.StringDoesNotMatchFormat, FormatAttributes.Uri, "sites/files/images/picture.png")
),
new TestCase(
"String: format: valid with uri-reference format constraint",
@"{
""type"": ""string"",
""format"": ""uri-reference""
}",
"\"sites/files/images/picture.png\""
),
new TestCase(
"String: format: Invalid with uri-reference format constraint",
@"{
""type"": ""string"",
""format"": ""uri-reference""
}",
"\"````\"",
MakeErrorMessage(1, 6, string.Empty, ErrorNumber.StringDoesNotMatchFormat, FormatAttributes.UriReference, "````")
),
new TestCase(
"String: format: valid with uuid format constraint - lowercase",
@"{
""type"": ""string"",
""format"": ""uuid""
}",
"\"8b524014-4666-449e-afc4-1b82fbfb27ad\""
),
new TestCase(
"String: format: valid with uuid format constraint - uppercase",
@"{
""type"": ""string"",
""format"": ""uuid""
}",
"\"6303D187-07A8-4164-864F-0C01D6F8E5C4\""
),
new TestCase(
"String: format: Invalid with uuid format constraint",
@"{
""type"": ""string"",
""format"": ""uuid""
}",
"\"4014-4666-449e-afc4-1b82fbfb27ad\"",
MakeErrorMessage(1, 34, string.Empty, ErrorNumber.StringDoesNotMatchFormat, FormatAttributes.Uuid, "4014-4666-449e-afc4-1b82fbfb27ad")
)
};

Expand Down
7 changes: 6 additions & 1 deletion src/Json.Schema.Validation/RuleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ private static ReportingDescriptor MakeRule(ErrorNumber errorNumber, string full
[ErrorNumber.DependentPropertyMissing] = MakeRule(
ErrorNumber.DependentPropertyMissing,
RuleResources.RuleDescriptionDependentPropertyMissing,
RuleResources.ErrorDependentPropertyMissing)
RuleResources.ErrorDependentPropertyMissing),

[ErrorNumber.StringDoesNotMatchFormat] = MakeRule(
ErrorNumber.StringDoesNotMatchFormat,
RuleResources.RuleDescriptionStringDoesNotMatchFormat,
RuleResources.ErrorStringDoesNotMatchFormat)
});

public static ReportingDescriptor GetRuleFromRuleId(string ruleId)
Expand Down
24 changes: 23 additions & 1 deletion src/Json.Schema.Validation/RuleResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Json.Schema.Validation/RuleResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@
<data name="ErrorWrongType" xml:space="preserve">
<value>The schema requires one of the types [{1}], but a token of type '{2}' was found.</value>
</data>
<data name="ErrorStringDoesNotMatchFormat" xml:space="preserve">
<value>The schema requires string value of the format '{1}', but a token value '{2}' was found.</value>
</data>
<data name="RuleDescriptionAdditionalPropertiesProhibited" xml:space="preserve">
<value>An object contains a property not defined by the schema, and the schema does not permit additional properties.</value>
</data>
Expand Down Expand Up @@ -313,4 +316,7 @@
<data name="RuleDescriptionWrongType" xml:space="preserve">
<value>An instance has a type that is not permitted by the schema's 'type' property.</value>
</data>
<data name="RuleDescriptionStringDoesNotMatchFormat" xml:space="preserve">
<value>A string value has a format that is not permitted by the schema's 'format' property.</value>
</data>
</root>
38 changes: 38 additions & 0 deletions src/Json.Schema.Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

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

{
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
Expand Down
22 changes: 21 additions & 1 deletion src/Json.Schema/ErrorNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,27 @@ public enum ErrorNumber
/// The instance contains a property specified by "dependencies", but it does
/// not contain all the properties specified by the corresponding property dependency.
/// </summary>
DependentPropertyMissing = 1023
DependentPropertyMissing = 1023,


/// <summary>
/// A string instance does not match the required format.
/// </summary>
/// <example>
/// Schema:
/// <code>
/// {
/// "type": "string",
/// "format": "date-time"
/// }
/// </code>
///
/// Instance:
/// <code>
/// "2023-02-03:T12:00:00Z"
/// </code>
/// </example>
StringDoesNotMatchFormat = 1024,

#endregion Errors in instance document
}
Expand Down