Skip to content

implementing validate_strings #883

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 2 commits into from
Sep 19, 2023
Merged
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
29 changes: 27 additions & 2 deletions python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ _recursion_limit: int

_T = TypeVar('_T', default=Any, covariant=True)

_StringInput: TypeAlias = 'dict[str, _StringInput]'

@final
class Some(Generic[_T]):
"""
Expand Down Expand Up @@ -168,6 +170,29 @@ class SchemaValidator:
ValidationError: If validation fails or if the JSON data is invalid.
Exception: Other error types maybe raised if internal errors occur.

Returns:
The validated Python object.
"""
def validate_strings(
self, input: _StringInput, *, strict: bool | None = None, context: 'dict[str, Any] | None' = None
) -> Any:
"""
Validate a string against the schema and return the validated Python object.

This is similar to `validate_json` but applies to scenarios where the input will be a string but not
JSON data, e.g. URL fragments, query parameters, etc.

Arguments:
input: The input as a string, or bytes/bytearray if `strict=False`.
strict: Whether to validate the object in strict mode.
If `None`, the value of [`CoreConfig.strict`][pydantic_core.core_schema.CoreConfig] is used.
context: The context to use for validation, this is passed to functional validators as
[`info.context`][pydantic_core.core_schema.ValidationInfo.context].

Raises:
ValidationError: If validation fails or if the JSON data is invalid.
Exception: Other error types maybe raised if internal errors occur.

Returns:
The validated Python object.
"""
Expand Down Expand Up @@ -680,7 +705,7 @@ class ValidationError(ValueError):
def from_exception_data(
title: str,
line_errors: list[InitErrorDetails],
error_mode: Literal['python', 'json'] = 'python',
input_type: Literal['python', 'json'] = 'python',
hide_input: bool = False,
) -> ValidationError:
"""
Expand All @@ -693,7 +718,7 @@ class ValidationError(ValueError):
title: The title of the error, as used in the heading of `str(validation_error)`
line_errors: A list of [`InitErrorDetails`][pydantic_core.InitErrorDetails] which contain information
about errors that occurred during validation.
error_mode: Whether the error is for a Python object or JSON.
input_type: Whether the error is for a Python object or JSON.
hide_input: Whether to hide the input value in the error message.
"""
@property
Expand Down
5 changes: 3 additions & 2 deletions src/build_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList, PyString};
use pyo3::{intern, FromPyObject, PyErrArguments};

use crate::errors::{ErrorMode, ValError};
use crate::errors::ValError;
use crate::input::InputType;
use crate::tools::SchemaDict;
use crate::ValidationError;

Expand Down Expand Up @@ -86,7 +87,7 @@ impl SchemaError {
ValError::LineErrors(raw_errors) => {
let line_errors = raw_errors.into_iter().map(|e| e.into_py(py)).collect();
let validation_error =
ValidationError::new(line_errors, "Schema".to_object(py), ErrorMode::Python, false);
ValidationError::new(line_errors, "Schema".to_object(py), InputType::Python, false);
let schema_error = SchemaError(SchemaErrorEnum::ValidationError(validation_error));
match Py::new(py, schema_error) {
Ok(err) => PyErr::from_value(err.into_ref(py)),
Expand Down
2 changes: 1 addition & 1 deletion src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod value_exception;

pub use self::line_error::{InputValue, ValError, ValLineError, ValResult};
pub use self::location::LocItem;
pub use self::types::{list_all_errors, ErrorMode, ErrorType, ErrorTypeDefaults, Number};
pub use self::types::{list_all_errors, ErrorType, ErrorTypeDefaults, Number};
pub use self::validation_exception::ValidationError;
pub use self::value_exception::{PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticUseDefault};

Expand Down
41 changes: 12 additions & 29 deletions src/errors/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,20 @@ use std::any::type_name;
use std::borrow::Cow;
use std::fmt;

use ahash::AHashMap;
use num_bigint::BigInt;
use pyo3::exceptions::{PyKeyError, PyTypeError, PyValueError};
use pyo3::exceptions::{PyKeyError, PyTypeError};
use pyo3::once_cell::GILOnceCell;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};

use crate::input::Int;
use crate::tools::{extract_i64, py_err, py_error_type};
use ahash::AHashMap;
use num_bigint::BigInt;
use strum::{Display, EnumMessage, IntoEnumIterator};
use strum_macros::EnumIter;

use super::PydanticCustomError;

#[derive(Clone, Debug)]
pub enum ErrorMode {
Python,
Json,
}

impl TryFrom<&str> for ErrorMode {
type Error = PyErr;
use crate::input::{InputType, Int};
use crate::tools::{extract_i64, py_err, py_error_type};

fn try_from(error_mode: &str) -> PyResult<Self> {
match error_mode {
"python" => Ok(Self::Python),
"json" => Ok(Self::Json),
s => py_err!(PyValueError; "Invalid error mode: {}", s),
}
}
}
use super::PydanticCustomError;

#[pyfunction]
pub fn list_all_errors(py: Python) -> PyResult<&PyList> {
Expand All @@ -45,12 +28,12 @@ pub fn list_all_errors(py: Python) -> PyResult<&PyList> {
d.set_item("message_template_python", message_template_python)?;
d.set_item(
"example_message_python",
error_type.render_message(py, &ErrorMode::Python)?,
error_type.render_message(py, InputType::Python)?,
)?;
let message_template_json = error_type.message_template_json();
if message_template_python != message_template_json {
d.set_item("message_template_json", message_template_json)?;
d.set_item("example_message_json", error_type.render_message(py, &ErrorMode::Json)?)?;
d.set_item("example_message_json", error_type.render_message(py, InputType::Json)?)?;
}
d.set_item("example_context", error_type.py_dict(py)?)?;
errors.push(d);
Expand Down Expand Up @@ -623,10 +606,10 @@ impl ErrorType {
}
}

pub fn render_message(&self, py: Python, error_mode: &ErrorMode) -> PyResult<String> {
let tmpl = match error_mode {
ErrorMode::Python => self.message_template_python(),
ErrorMode::Json => self.message_template_json(),
pub fn render_message(&self, py: Python, input_type: InputType) -> PyResult<String> {
let tmpl = match input_type {
InputType::Python => self.message_template_python(),
_ => self.message_template_json(),
};
match self {
Self::NoSuchAttribute { attribute, .. } => render!(tmpl, attribute),
Expand Down
47 changes: 23 additions & 24 deletions src/errors/validation_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ use serde_json::ser::PrettyFormatter;
use crate::build_tools::py_schema_error_type;
use crate::errors::LocItem;
use crate::get_pydantic_version;
use crate::input::InputType;
use crate::serializers::{SerMode, SerializationState};
use crate::tools::{safe_repr, SchemaDict};

use super::line_error::ValLineError;
use super::location::Location;
use super::types::{ErrorMode, ErrorType};
use super::types::ErrorType;
use super::value_exception::PydanticCustomError;
use super::{InputValue, ValError};

Expand All @@ -31,24 +32,24 @@ use super::{InputValue, ValError};
pub struct ValidationError {
line_errors: Vec<PyLineError>,
title: PyObject,
error_mode: ErrorMode,
input_type: InputType,
hide_input: bool,
}

impl ValidationError {
pub fn new(line_errors: Vec<PyLineError>, title: PyObject, error_mode: ErrorMode, hide_input: bool) -> Self {
pub fn new(line_errors: Vec<PyLineError>, title: PyObject, input_type: InputType, hide_input: bool) -> Self {
Self {
line_errors,
title,
error_mode,
input_type,
hide_input,
}
}

pub fn from_val_error(
py: Python,
title: PyObject,
error_mode: ErrorMode,
input_type: InputType,
error: ValError,
outer_location: Option<LocItem>,
hide_input: bool,
Expand All @@ -63,9 +64,7 @@ impl ValidationError {
.collect(),
None => raw_errors.into_iter().map(|e| e.into_py(py)).collect(),
};

let validation_error = Self::new(line_errors, title, error_mode, hide_input);

let validation_error = Self::new(line_errors, title, input_type, hide_input);
match Py::new(py, validation_error) {
Ok(err) => {
if validation_error_cause {
Expand All @@ -87,7 +86,7 @@ impl ValidationError {

pub fn display(&self, py: Python, prefix_override: Option<&'static str>, hide_input: bool) -> String {
let url_prefix = get_url_prefix(py, include_url_env(py));
let line_errors = pretty_py_line_errors(py, &self.error_mode, self.line_errors.iter(), url_prefix, hide_input);
let line_errors = pretty_py_line_errors(py, self.input_type, self.line_errors.iter(), url_prefix, hide_input);
if let Some(prefix) = prefix_override {
format!("{prefix}\n{line_errors}")
} else {
Expand Down Expand Up @@ -238,20 +237,20 @@ impl ValidationError {
#[pymethods]
impl ValidationError {
#[staticmethod]
#[pyo3(signature = (title, line_errors, error_mode="python", hide_input=false))]
#[pyo3(signature = (title, line_errors, input_type="python", hide_input=false))]
fn from_exception_data(
py: Python,
title: PyObject,
line_errors: &PyList,
error_mode: &str,
input_type: &str,
hide_input: bool,
) -> PyResult<Py<Self>> {
Py::new(
py,
Self {
line_errors: line_errors.iter().map(PyLineError::try_from).collect::<PyResult<_>>()?,
title,
error_mode: ErrorMode::try_from(error_mode)?,
input_type: InputType::try_from(input_type)?,
hide_input,
},
)
Expand Down Expand Up @@ -279,7 +278,7 @@ impl ValidationError {
if iteration_error.is_some() {
return py.None();
}
e.as_dict(py, url_prefix, include_context, &self.error_mode)
e.as_dict(py, url_prefix, include_context, self.input_type)
.unwrap_or_else(|err| {
iteration_error = Some(err);
py.None()
Expand Down Expand Up @@ -309,7 +308,7 @@ impl ValidationError {
url_prefix: get_url_prefix(py, include_url),
include_context,
extra: &extra,
error_mode: &self.error_mode,
input_type: &self.input_type,
};

let writer: Vec<u8> = Vec::with_capacity(self.line_errors.len() * 200);
Expand Down Expand Up @@ -387,13 +386,13 @@ macro_rules! truncate_input_value {

pub fn pretty_py_line_errors<'a>(
py: Python,
error_mode: &ErrorMode,
input_type: InputType,
line_errors_iter: impl Iterator<Item = &'a PyLineError>,
url_prefix: Option<&str>,
hide_input: bool,
) -> String {
line_errors_iter
.map(|i| i.pretty(py, error_mode, url_prefix, hide_input))
.map(|i| i.pretty(py, input_type, url_prefix, hide_input))
.collect::<Result<Vec<_>, _>>()
.unwrap_or_else(|err| vec![format!("[error formatting line errors: {err}]")])
.join("\n")
Expand Down Expand Up @@ -477,12 +476,12 @@ impl PyLineError {
py: Python,
url_prefix: Option<&str>,
include_context: bool,
error_mode: &ErrorMode,
input_type: InputType,
) -> PyResult<PyObject> {
let dict = PyDict::new(py);
dict.set_item("type", self.error_type.type_string())?;
dict.set_item("loc", self.location.to_object(py))?;
dict.set_item("msg", self.error_type.render_message(py, error_mode)?)?;
dict.set_item("msg", self.error_type.render_message(py, input_type)?)?;
dict.set_item("input", &self.input_value)?;
if include_context {
if let Some(context) = self.error_type.py_dict(py)? {
Expand All @@ -505,14 +504,14 @@ impl PyLineError {
fn pretty(
&self,
py: Python,
error_mode: &ErrorMode,
input_type: InputType,
url_prefix: Option<&str>,
hide_input: bool,
) -> Result<String, fmt::Error> {
let mut output = String::with_capacity(200);
write!(output, "{}", self.location)?;

let message = match self.error_type.render_message(py, error_mode) {
let message = match self.error_type.render_message(py, input_type) {
Ok(message) => message,
Err(err) => format!("(error rendering message: {err})"),
};
Expand Down Expand Up @@ -565,7 +564,7 @@ struct ValidationErrorSerializer<'py> {
url_prefix: Option<&'py str>,
include_context: bool,
extra: &'py crate::serializers::Extra<'py>,
error_mode: &'py ErrorMode,
input_type: &'py InputType,
}

impl<'py> Serialize for ValidationErrorSerializer<'py> {
Expand All @@ -581,7 +580,7 @@ impl<'py> Serialize for ValidationErrorSerializer<'py> {
url_prefix: self.url_prefix,
include_context: self.include_context,
extra: self.extra,
error_mode: self.error_mode,
input_type: self.input_type,
};
seq.serialize_element(&line_s)?;
}
Expand All @@ -595,7 +594,7 @@ struct PyLineErrorSerializer<'py> {
url_prefix: Option<&'py str>,
include_context: bool,
extra: &'py crate::serializers::Extra<'py>,
error_mode: &'py ErrorMode,
input_type: &'py InputType,
}

impl<'py> Serialize for PyLineErrorSerializer<'py> {
Expand All @@ -620,7 +619,7 @@ impl<'py> Serialize for PyLineErrorSerializer<'py> {
let msg = self
.line_error
.error_type
.render_message(py, self.error_mode)
.render_message(py, *self.input_type)
.map_err(py_err_json::<S>)?;
map.serialize_entry("msg", &msg)?;

Expand Down
5 changes: 2 additions & 3 deletions src/errors/value_exception.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::errors::ErrorMode;
use pyo3::exceptions::{PyException, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyString};

use crate::input::Input;
use crate::input::{Input, InputType};
use crate::tools::extract_i64;

use super::{ErrorType, ValError};
Expand Down Expand Up @@ -164,7 +163,7 @@ impl PydanticKnownError {
}

pub fn message(&self, py: Python) -> PyResult<String> {
self.error_type.render_message(py, &ErrorMode::Python)
self.error_type.render_message(py, InputType::Python)
}

fn __str__(&self, py: Python) -> PyResult<String> {
Expand Down
Loading