Skip to content

Commit

Permalink
add strict flag to validate_core_schema (#984)
Browse files Browse the repository at this point in the history
adriangb authored Sep 21, 2023
1 parent 916d909 commit 0a3f2b2
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use _pydantic_core::{validate_core_schema, SchemaValidator};

fn build_schema_validator_with_globals(py: Python, code: &str, globals: Option<&PyDict>) -> SchemaValidator {
let mut schema: &PyDict = py.eval(code, globals, None).unwrap().extract().unwrap();
schema = validate_core_schema(py, schema).unwrap().extract().unwrap();
schema = validate_core_schema(py, schema, None).unwrap().extract().unwrap();
SchemaValidator::py_new(py, schema, None).unwrap()
}

@@ -446,7 +446,7 @@ fn complete_model(bench: &mut Bencher) {

let complete_schema = py.import("complete_schema").unwrap();
let mut schema = complete_schema.call_method0("schema").unwrap();
schema = validate_core_schema(py, schema).unwrap().extract().unwrap();
schema = validate_core_schema(py, schema, None).unwrap().extract().unwrap();
let validator = SchemaValidator::py_new(py, schema, None).unwrap();

let input = complete_schema.call_method0("input_data_lax").unwrap();
2 changes: 1 addition & 1 deletion python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
@@ -838,7 +838,7 @@ class TzInfo(datetime.tzinfo):
def fromutc(self, dt: datetime.datetime) -> datetime.datetime: ...
def __deepcopy__(self, _memo: dict[Any, Any]) -> 'TzInfo': ...

def validate_core_schema(schema: CoreSchema) -> CoreSchema:
def validate_core_schema(schema: CoreSchema, *, strict: bool | None = None) -> CoreSchema:
"""Validate a CoreSchema
This currently uses lax mode for validation (i.e. will coerce strings to dates and such)
but may use strict mode in the future.
10 changes: 5 additions & 5 deletions src/validators/mod.rs
Original file line number Diff line number Diff line change
@@ -367,10 +367,10 @@ impl<'py> SelfValidator<'py> {
Ok(Self { validator })
}

pub fn validate_schema(&self, py: Python<'py>, schema: &'py PyAny) -> PyResult<&'py PyAny> {
pub fn validate_schema(&self, py: Python<'py>, schema: &'py PyAny, strict: Option<bool>) -> PyResult<&'py PyAny> {
let mut recursion_guard = RecursionGuard::default();
let mut state = ValidationState::new(
Extra::new(None, None, None, None, InputType::Python),
Extra::new(strict, None, None, None, InputType::Python),
&self.validator.definitions,
&mut recursion_guard,
);
@@ -408,10 +408,10 @@ impl<'py> SelfValidator<'py> {
}
}

#[pyfunction]
pub fn validate_core_schema<'a>(py: Python<'a>, schema: &'a PyAny) -> PyResult<&'a PyAny> {
#[pyfunction(signature = (schema, *, strict = None))]
pub fn validate_core_schema<'a>(py: Python<'a>, schema: &'a PyAny, strict: Option<bool>) -> PyResult<&'a PyAny> {
let self_validator = SelfValidator::new(py)?;
self_validator.validate_schema(py, schema)
self_validator.validate_schema(py, schema, strict)
}

pub trait BuildValidator: Sized {

0 comments on commit 0a3f2b2

Please sign in to comment.