-
Notifications
You must be signed in to change notification settings - Fork 291
Validate bytes based on ser_json_bytes #1308
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f8addc1
Implement validation based on ser_json_bytes to support round trip
josh-newman c1c84e3
Merge remote-tracking branch 'refs/remotes/upstream/main' into valida…
josh-newman 1e71ab7
Switch to val_json_bytes config key
josh-newman c415a20
Merge remote-tracking branch 'upstream/main' into validate-base64
josh-newman 171a45b
Raise new ValidationError type for encoding
josh-newman c5930cb
Implement hex
josh-newman 409a077
Switch to hex crate
josh-newman 8018a84
Update pyi
josh-newman 5870e0b
Avoid nested f-string quotes, for older Pythons
josh-newman c5b4f7f
Merge remote-tracking branch 'refs/remotes/upstream/main' into valida…
josh-newman 62f3b20
Merge remote-tracking branch 'refs/remotes/upstream/main' into valida…
josh-newman 9c08a00
pub(crate) mode types only, not modules
josh-newman c1e6402
Make example error messages more realistic
josh-newman 428f479
Merge branch 'main' into validate-base64
josh-newman 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::borrow::Cow; | ||
use std::str::FromStr; | ||
|
||
use base64::Engine; | ||
use pyo3::types::{PyDict, PyString}; | ||
use pyo3::{intern, prelude::*}; | ||
|
||
use crate::errors::ErrorType; | ||
use crate::input::EitherBytes; | ||
use crate::serializers::BytesMode; | ||
use crate::tools::SchemaDict; | ||
|
||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct ValBytesMode { | ||
pub ser: BytesMode, | ||
} | ||
|
||
impl ValBytesMode { | ||
pub fn from_config(config: Option<&Bound<'_, PyDict>>) -> PyResult<Self> { | ||
let Some(config_dict) = config else { | ||
return Ok(Self::default()); | ||
}; | ||
let raw_mode = config_dict.get_as::<Bound<'_, PyString>>(intern!(config_dict.py(), "val_json_bytes"))?; | ||
let ser_mode = raw_mode.map_or_else(|| Ok(BytesMode::default()), |raw| BytesMode::from_str(&raw.to_cow()?))?; | ||
Ok(Self { ser: ser_mode }) | ||
} | ||
|
||
pub fn deserialize_string<'py>(self, s: &str) -> Result<EitherBytes<'_, 'py>, ErrorType> { | ||
match self.ser { | ||
BytesMode::Utf8 => Ok(EitherBytes::Cow(Cow::Borrowed(s.as_bytes()))), | ||
BytesMode::Base64 => match base64::engine::general_purpose::URL_SAFE.decode(s) { | ||
Ok(bytes) => Ok(EitherBytes::from(bytes)), | ||
Err(err) => Err(ErrorType::BytesInvalidEncoding { | ||
encoding: "base64".to_string(), | ||
encoding_error: err.to_string(), | ||
context: None, | ||
}), | ||
}, | ||
BytesMode::Hex => match hex::decode(s) { | ||
Ok(vec) => Ok(EitherBytes::from(vec)), | ||
Err(err) => Err(ErrorType::BytesInvalidEncoding { | ||
encoding: "hex".to_string(), | ||
encoding_error: err.to_string(), | ||
context: None, | ||
}), | ||
}, | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.