Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass validation for format when instance is incompatible type #156

Merged
merged 2 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions JsonSchema.Generation/JsonSchema.Net.Generation.xml

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

24 changes: 12 additions & 12 deletions JsonSchema/Formats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,44 +159,44 @@ public static void Register(Format format)

private static bool CheckAbsoluteUri(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return System.Uri.TryCreate(element.GetString(), UriKind.Absolute, out _);
}

private static bool CheckUri(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return System.Uri.TryCreate(element.GetString(), UriKind.RelativeOrAbsolute, out _);
}

private static bool CheckUriTemplate(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

throw new NotSupportedException("The UriTemplate class has not been ported to .Net Standard/Core yet.");
//return System.UriTemplate.Match(element.GetString());
}

private static bool CheckJsonPointer(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return Pointer.JsonPointer.TryParse(element.GetString(), out var p) && !p.IsUriEncoded;
}

private static bool CheckRelativeJsonPointer(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return Pointer.RelativeJsonPointer.TryParse(element.GetString(), out _);
}

// source: https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format
private static bool CheckEmail(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

var email = element.GetString();

Expand Down Expand Up @@ -243,7 +243,7 @@ private static string DomainMapper(Match match)

private static bool CheckUuid(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return Guid.TryParseExact(element.GetString(), "D", out _);
}
Expand All @@ -265,14 +265,14 @@ private static bool CheckDateTime(JsonElement element)

private static bool CheckDateFormat(JsonElement element, params string[] formats)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return System.DateTime.TryParseExact(element.GetString().ToUpperInvariant(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
}

private static bool CheckHostName(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

var type = System.Uri.CheckHostName(element.GetString());

Expand All @@ -291,7 +291,7 @@ private static bool CheckIpv6(JsonElement element)

private static bool CheckHostName(JsonElement element, UriHostNameType type)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

var actualType = System.Uri.CheckHostName(element.GetString());

Expand All @@ -300,14 +300,14 @@ private static bool CheckHostName(JsonElement element, UriHostNameType type)

private static bool CheckDuration(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

return Schema.Duration.TryParse(element.GetString(), out _);
}

private static bool CheckRegex(JsonElement element)
{
if (element.ValueKind != JsonValueKind.String) return false;
if (element.ValueKind != JsonValueKind.String) return true;

try
{
Expand Down
4 changes: 2 additions & 2 deletions JsonSchema/JsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
<RepositoryUrl>https://github.com/gregsdennis/json-everything</RepositoryUrl>
<PackageTags>json-schema validation schema json</PackageTags>
<Version>1.11.0</Version>
<FileVersion>1.11.0.0</FileVersion>
<Version>1.11.1</Version>
<FileVersion>1.11.1.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AssemblyName>JsonSchema.Net</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion TryJsonEverything/wwwroot/js/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async function validate() {
const options = {
validateAs: transformDraft(draftElement.value),
outputFormat: transformOutputFormat(outputFormatElement.value),
defaultBaseUri: baseUriElement.value === '' ? 'https://json-everything.net' : baseUriElement.value,
defaultBaseUri: baseUriElement.value === '' ? null : baseUriElement.value,
requireFormatValidation: requireFormatElement.checked
};

Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-schema.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.11.1](https://github.com/gregsdennis/json-everything/pull/156)

Some `format` validations were incorrectly failing validations for incompatible types. For example, `uri` would fail on integers, but should pass (ignore non-strings).

# [1.11.0](https://github.com/gregsdennis/json-everything/pull/155)

`format` incorporates error messages from custom format validation, when provided.
Expand Down