Skip to content

Commit b95a9f2

Browse files
committed
docs: Document panics for blocking code in async context
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 6f532dc commit b95a9f2

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

crates/jsonschema/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,11 @@ use serde_json::Value;
941941
/// # Panics
942942
///
943943
/// This function panics if an invalid schema is passed.
944+
///
945+
/// This function **must not** be called from within an async runtime if the schema contains
946+
/// external references that require network requests, or it will panic when attempting to block.
947+
/// Use `async_validator_for` for async contexts, or run this in a separate blocking thread
948+
/// via `tokio::task::spawn_blocking`.
944949
#[must_use]
945950
#[inline]
946951
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
@@ -968,6 +973,11 @@ pub fn is_valid(schema: &Value, instance: &Value) -> bool {
968973
/// # Panics
969974
///
970975
/// This function panics if an invalid schema is passed.
976+
///
977+
/// This function **must not** be called from within an async runtime if the schema contains
978+
/// external references that require network requests, or it will panic when attempting to block.
979+
/// Use `async_validator_for` for async contexts, or run this in a separate blocking thread
980+
/// via `tokio::task::spawn_blocking`.
971981
#[inline]
972982
pub fn validate<'i>(schema: &Value, instance: &'i Value) -> Result<(), ValidationError<'i>> {
973983
validator_for(schema)
@@ -994,6 +1004,11 @@ pub fn validate<'i>(schema: &Value, instance: &'i Value) -> Result<(), Validatio
9941004
/// # Panics
9951005
///
9961006
/// This function panics if an invalid schema is passed.
1007+
///
1008+
/// This function **must not** be called from within an async runtime if the schema contains
1009+
/// external references that require network requests, or it will panic when attempting to block.
1010+
/// Use `async_validator_for` for async contexts, or run this in a separate blocking thread
1011+
/// via `tokio::task::spawn_blocking`.
9971012
#[must_use]
9981013
#[inline]
9991014
pub fn evaluate(schema: &Value, instance: &Value) -> Evaluation {
@@ -1022,6 +1037,13 @@ pub fn evaluate(schema: &Value, instance: &Value) -> Evaluation {
10221037
/// # Errors
10231038
///
10241039
/// Returns an error if the schema is invalid or external references cannot be resolved.
1040+
///
1041+
/// # Panics
1042+
///
1043+
/// This function **must not** be called from within an async runtime if the schema contains
1044+
/// external references that require network requests, or it will panic when attempting to block.
1045+
/// Use `async_validator_for` for async contexts, or run this in a separate blocking thread
1046+
/// via `tokio::task::spawn_blocking`.
10251047
pub fn validator_for(schema: &Value) -> Result<Validator, ValidationError<'static>> {
10261048
Validator::new(schema)
10271049
}
@@ -1065,6 +1087,10 @@ pub async fn async_validator_for(schema: &Value) -> Result<Validator, Validation
10651087
/// options for JSON Schema validation. You can use this builder to specify
10661088
/// the draft version, set custom formats, and more.
10671089
///
1090+
/// **Note:** When calling [`ValidationOptions::build`], it **must not** be called from within
1091+
/// an async runtime if the schema contains external references that require network requests,
1092+
/// or it will panic. Use `async_options` for async contexts.
1093+
///
10681094
/// # Examples
10691095
///
10701096
/// Basic usage with draft specification:

crates/jsonschema/src/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,13 @@ impl ValidationOptions<Arc<dyn referencing::Retrieve>> {
537537
///
538538
/// Returns an error if `schema` is invalid for the selected draft or if referenced resources
539539
/// cannot be retrieved or resolved.
540+
///
541+
/// # Panics
542+
///
543+
/// This method **must not** be called from within an async runtime if the schema contains
544+
/// external references that require network requests, or it will panic when attempting to block.
545+
/// Use `async_options` and its async `build` method for async contexts, or run this
546+
/// in a separate blocking thread via `tokio::task::spawn_blocking`.
540547
pub fn build(&self, schema: &Value) -> Result<Validator, ValidationError<'static>> {
541548
compiler::build_validator(self, schema)
542549
}

0 commit comments

Comments
 (0)