feat: Add XsdDate type and codec for XSD-compliant date handling.#75
feat: Add XsdDate type and codec for XSD-compliant date handling.#75mark-dropbear merged 1 commit intomainfrom
XsdDate type and codec for XSD-compliant date handling.#75Conversation
Summary of ChangesHello @mark-dropbear, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the library's XSD datatype support by introducing a dedicated Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new XsdDate type and its corresponding codec, which is a great addition for XSD compliance. The implementation is solid and well-tested. I've found one high-severity issue regarding missing validation for timezone offset ranges in the XsdDate.parse method, which could lead to accepting invalid date strings. I've provided a detailed comment with a code suggestion to fix this and improve the clarity of the parsing logic. I've also suggested adding a few more test cases to cover this scenario and prevent future regressions. Overall, great work on this feature.
| Duration? offset; | ||
| if (tzStr == 'Z') { | ||
| offset = Duration.zero; | ||
| } else { | ||
| final tzMatch = _offsetRegex.firstMatch(tzStr); | ||
| if (tzMatch != null) { | ||
| final sign = tzMatch.group(1) == '+' ? 1 : -1; | ||
| final hours = int.parse(tzMatch.group(2)!); | ||
| final minutes = int.parse(tzMatch.group(3)!); | ||
| offset = Duration(hours: hours, minutes: minutes) * sign; | ||
| } | ||
| } |
There was a problem hiding this comment.
The logic for parsing the timezone can be improved for clarity and correctness.
- Correctness: The current implementation does not validate the range of the timezone offset. According to the XSD 1.1 specification, the offset
hhmust be between 00-14 andmmbetween 00-59 (withmmbeing 00 ifhhis 14). An invalid offset like+15:00would be incorrectly parsed. - Clarity: Since
_dateRegexensures thattzStr(if not null and not 'Z') will be in the[+-]HH:mmformat, the_offsetRegexwill always find a match. The null check ontzMatchis redundant.
The suggested change addresses both points by adding the validation and simplifying the logic.
final Duration offset;
if (tzStr == 'Z') {
offset = Duration.zero;
} else {
// _dateRegex ensures tzStr is in [+-]HH:mm format, so a match is guaranteed.
final tzMatch = _offsetRegex.firstMatch(tzStr)!;
final sign = tzMatch.group(1) == '+' ? 1 : -1;
final hours = int.parse(tzMatch.group(2)!);
final minutes = int.parse(tzMatch.group(3)!);
if (hours > 14 || minutes > 59 || (hours == 14 && minutes != 0)) {
throw FormatException('Invalid timezone offset range', input);
}
offset = Duration(hours: hours, minutes: minutes) * sign;
}| () => XsdDate.parse('2002-02-30'), | ||
| throwsFormatException, | ||
| ); // Invalid day | ||
| }); |
There was a problem hiding this comment.
This test group is great for format validation! To make it even more robust, could you also add test cases for invalid timezone offsets? This will help prevent future regressions for the timezone validation logic you're adding.
For example:
expect(
() => XsdDate.parse('2002-10-10+15:00'), // Hour > 14
throwsFormatException,
);
expect(
() => XsdDate.parse('2002-10-10-14:01'), // Minute > 00 when hour is 14
throwsFormatException,
);
No description provided.