-
Notifications
You must be signed in to change notification settings - Fork 247
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
fix inconsistency with strict mode of date validation #870
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,16 +47,12 @@ impl Validator for DateValidator { | |
_definitions: &'data Definitions<CombinedValidator>, | ||
_recursion_guard: &'s mut RecursionGuard, | ||
) -> ValResult<'data, PyObject> { | ||
let date = match input.validate_date(extra.strict.unwrap_or(self.strict)) { | ||
let strict = extra.strict.unwrap_or(self.strict); | ||
let date = match input.validate_date(strict) { | ||
Ok(date) => date, | ||
// if the date error was an internal error, return that immediately | ||
Err(ValError::InternalErr(internal_err)) => return Err(ValError::InternalErr(internal_err)), | ||
Err(date_err) => match self.strict { | ||
// if we're in strict mode, we doing try coercing from a date | ||
true => return Err(date_err), | ||
// otherwise, try creating a date from a datetime input | ||
false => date_from_datetime(input, date_err), | ||
}?, | ||
// if the error was a parsing error, in lax mode we allow datetimes at midnight | ||
Err(line_errors @ ValError::LineErrors(..)) if !strict => date_from_datetime(input)?.ok_or(line_errors)?, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With apologies I couldn't help golfing this line, with the result of modifying |
||
Err(otherwise) => return Err(otherwise), | ||
}; | ||
if let Some(constraints) = &self.constraints { | ||
let raw_date = date.as_raw()?; | ||
|
@@ -122,35 +118,31 @@ impl Validator for DateValidator { | |
|
||
/// In lax mode, if the input is not a date, we try parsing the input as a datetime, then check it is an | ||
/// "exact date", e.g. has a zero time component. | ||
fn date_from_datetime<'data>( | ||
input: &'data impl Input<'data>, | ||
date_err: ValError<'data>, | ||
) -> ValResult<'data, EitherDate<'data>> { | ||
/// | ||
/// Ok(None) means that this is not relevant to dates (the input was not a datetime nor a string) | ||
fn date_from_datetime<'data>(input: &'data impl Input<'data>) -> Result<Option<EitherDate<'data>>, ValError<'data>> { | ||
let either_dt = match input.validate_datetime(false, speedate::MicrosecondsPrecisionOverflowBehavior::Truncate) { | ||
Ok(dt) => dt, | ||
Err(dt_err) => { | ||
return match dt_err { | ||
ValError::LineErrors(mut line_errors) => { | ||
// if we got a errors while parsing the datetime, | ||
// convert DateTimeParsing -> DateFromDatetimeParsing but keep the rest of the error unchanged | ||
for line_error in &mut line_errors { | ||
match line_error.error_type { | ||
ErrorType::DatetimeParsing { ref error, .. } => { | ||
line_error.error_type = ErrorType::DateFromDatetimeParsing { | ||
error: error.to_string(), | ||
context: None, | ||
}; | ||
} | ||
_ => { | ||
return Err(date_err); | ||
} | ||
} | ||
} | ||
Err(ValError::LineErrors(line_errors)) | ||
// if the error was a parsing error, update the error type from DatetimeParsing to DateFromDatetimeParsing | ||
// and return it | ||
Err(ValError::LineErrors(mut line_errors)) => { | ||
if line_errors.iter_mut().fold(false, |has_parsing_error, line_error| { | ||
if let ErrorType::DatetimeParsing { error, .. } = &mut line_error.error_type { | ||
line_error.error_type = ErrorType::DateFromDatetimeParsing { | ||
error: std::mem::take(error), | ||
context: None, | ||
}; | ||
true | ||
} else { | ||
has_parsing_error | ||
} | ||
other => Err(other), | ||
}; | ||
}) { | ||
return Err(ValError::LineErrors(line_errors)); | ||
} | ||
return Ok(None); | ||
} | ||
// for any other error, don't return it | ||
Err(_) => return Ok(None), | ||
}; | ||
let dt = either_dt.as_raw()?; | ||
let zero_time = Time { | ||
|
@@ -161,7 +153,7 @@ fn date_from_datetime<'data>( | |
tz_offset: dt.time.tz_offset, | ||
}; | ||
if dt.time == zero_time { | ||
Ok(EitherDate::Raw(dt.date)) | ||
Ok(Some(EitherDate::Raw(dt.date))) | ||
} else { | ||
Err(ValError::new(ErrorTypeDefaults::DateFromDatetimeInexact, input)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so strange 😅 what was the original reasoning for allowing this?
Its also inconsistent with Python pydantic/pydantic#7039 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither its valid json for a
date
property in a json schema.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this behaviour is a bit surprising. It originates from #77 so we will have to ask @samuelcolvin for the motivation and if we can remove it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup we need to evaluate that but it'll be in a separate discussion / PR