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

Date-time format should use ISO 8601-1:2019 (same as System.Text.Json) #439

Closed
cdecroix opened this issue Apr 24, 2023 · 8 comments
Closed
Labels
pkg:schema-generation question Further information is requested

Comments

@cdecroix
Copy link

cdecroix commented Apr 24, 2023

Environment

  • Nuget Package: JsonSchema.Net
  • Nuget Version: 4.0.3
  • OS: Windows
  • .Net Target: .Net 6.0

Describe the bug
Values like "2023-03-20 05:41:23.3881075" ("T" missing between the date and time) are valid in JsonSchema but it can't be deserialized by System.Text.Json.

According to the Microsoft documentation, they parse date using the ISO 8601-1:2019 format.
The regex added to JsonSchema in v4.0.1 is based on the previous version of that ISO.

Expected behavior
Validation should return an error.

@cdecroix cdecroix added the bug Something isn't working label Apr 24, 2023
@gregsdennis
Copy link
Collaborator

The JSON Schema spec, section 7.3.1 states RFC 3339. That's what this library uses.

@cdecroix
Copy link
Author

Is it possible to override the format validation to match with System.Text.Json?

@gregsdennis
Copy link
Collaborator

You can create your own format implementation and override the default implementation.

The instructions are here.

Also, you may want to read that whole section, as it describes how to enable format validation.

Lastly, please be aware that providing date/time in a different format probably won't be compatible with other implementations, and you'll need to do this modification wherever you use this schema.

@gregsdennis gregsdennis added question Further information is requested pkg:schema and removed bug Something isn't working labels Apr 24, 2023
@gregsdennis
Copy link
Collaborator

@gregsdennis
Copy link
Collaborator

gregsdennis commented Apr 28, 2023

This issue led me to look at the regex that was contributed to evaluate high-precision formats, and I discovered that that actually uses an ISO 8601 regex (which is what you were saying), which is wrong. I'll be updating that to support RFC3339.

I'm also opening an issue with .Net for deserialization support. I wouldn't be surprised if they come back with, "You can create a custom converter for DateTime." I'm not sure if that solves your issue, though.

@cdecroix
Copy link
Author

cdecroix commented May 9, 2023

Thanks for the clarification on which spec should be used!

For now, I overrode the validation in this lib to keep the same behavior as before (pre v4.0.1).

I did it by creating a ISchemaRefiner class that replace the format intent to use my custom validation method.
I didn't manage to create my own format class. The Validate was never called.

@gregsdennis
Copy link
Collaborator

I did it by creating a ISchemaRefiner class

This was reported as a JsonSchema.Net issue, however ISchemaRefiner is part of JsonSchema.Net.Generation. Had I known that you needed help with schema generation, my advice would have been different.

Regardless, while I'm glad that you got something working for your case, I'm not sure that this is fully resolved since there is a disparity between .Net's date-time format expectations and JSON Schema's date-time requirements.

@gregsdennis
Copy link
Collaborator

@cdecroix please see the latest comment in the dotnet/runtime discussion linked above. It appears there is some format crossover that you might be able to take advantage of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:schema-generation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants