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

Enable format validation and fix bugs. #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions src/JsonSchemaMapper/JsonSchemaMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,8 @@ public SimpleTypeJsonSchema(JsonSchemaType schemaType, string? format = null, st
public bool IsIeeeFloatingPoint { get; }
}

private const string Iso8601DateTimeRegex = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$";

private static readonly Dictionary<Type, SimpleTypeJsonSchema> s_simpleTypeInfo = new()
{
[typeof(object)] = new(JsonSchemaType.Any),
Expand Down Expand Up @@ -867,7 +869,7 @@ public SimpleTypeJsonSchema(JsonSchemaType schemaType, string? format = null, st
[typeof(byte[])] = new(JsonSchemaType.String),
[typeof(Memory<byte>)] = new(JsonSchemaType.String),
[typeof(ReadOnlyMemory<byte>)] = new(JsonSchemaType.String),
[typeof(DateTime)] = new(JsonSchemaType.String, format: "date-time"),
[typeof(DateTime)] = new(JsonSchemaType.String, pattern: Iso8601DateTimeRegex),
[typeof(DateTimeOffset)] = new(JsonSchemaType.String, format: "date-time"),

// TimeSpan is represented as a string in the format "[-][d.]hh:mm:ss[.fffffff]".
Expand All @@ -878,7 +880,7 @@ public SimpleTypeJsonSchema(JsonSchemaType schemaType, string? format = null, st
#endif
[typeof(Guid)] = new(JsonSchemaType.String, format: "uuid"),
[typeof(Uri)] = new(JsonSchemaType.String, format: "uri"),
[typeof(Version)] = new(JsonSchemaType.String, format: @"^\d+(\.\d+){1,3}$"),
[typeof(Version)] = new(JsonSchemaType.String, pattern: @"^\d+(\.\d+){1,3}$"),
[typeof(JsonDocument)] = new(JsonSchemaType.Any),
[typeof(JsonElement)] = new(JsonSchemaType.Any),
[typeof(JsonNode)] = new(JsonSchemaType.Any),
Expand Down
2 changes: 1 addition & 1 deletion tests/JsonSchemaMapper.Tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Instance JSON document matches the specified schema.
private static EvaluationResults EvaluateSchemaCore(JsonNode schema, JsonNode? instance)
{
JsonSchema jsonSchema = JsonSerializer.Deserialize(schema, Context.Default.JsonSchema)!;
EvaluationOptions options = new() { OutputFormat = OutputFormat.List };
EvaluationOptions options = new() { OutputFormat = OutputFormat.List, RequireFormatValidation = true };
return jsonSchema.Evaluate(instance, options);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/JsonSchemaMapper.Tests/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public static IEnumerable<ITestData> GetTestDataCore()
yield return new TestData<ReadOnlyMemory<byte>>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string"}""");
yield return new TestData<DateTime>(
Value: new(2021, 1, 1),
AdditionalValues: [DateTime.MinValue, DateTime.MaxValue]);
AdditionalValues: [DateTime.MinValue, DateTime.MaxValue],
ExpectedJsonSchema: """{"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d\u002B)?(?:Z|[\u002B-]\\d{2}:\\d{2})?$"}""");

yield return new TestData<DateTimeOffset>(
Value: new(new DateTime(2021, 1, 1), TimeSpan.Zero),
AdditionalValues: [DateTimeOffset.MinValue, DateTimeOffset.MaxValue],
ExpectedJsonSchema: """{"type":"string","format": "date-time"}""");
ExpectedJsonSchema: """{"type":"string","format":"date-time"}""");

yield return new TestData<TimeSpan>(
Value: new(hours: 5, minutes: 13, seconds: 3),
Expand All @@ -74,7 +75,7 @@ public static IEnumerable<ITestData> GetTestDataCore()
#endif
yield return new TestData<Guid>(Guid.Empty);
yield return new TestData<Uri>(new("http://example.com"));
yield return new TestData<Version>(new(1, 2, 3, 4), ExpectedJsonSchema: """{"type":"string","format":"^\\d+(\\.\\d+){1,3}$"}""");
yield return new TestData<Version>(new(1, 2, 3, 4), ExpectedJsonSchema: """{"type":"string","pattern":"^\\d+(\\.\\d+){1,3}$"}""");
yield return new TestData<JsonDocument>(JsonDocument.Parse("""[{ "x" : 42 }]"""), ExpectedJsonSchema: "{}");
yield return new TestData<JsonElement>(JsonDocument.Parse("""[{ "x" : 42 }]""").RootElement, ExpectedJsonSchema: "{}");
yield return new TestData<JsonNode>(JsonNode.Parse("""[{ "x" : 42 }]"""));
Expand Down
Loading