pub struct RedactionBuilder<T> { /* private fields */ }redaction only.Expand description
Builder for applying redactions to JSON responses.
This builder allows you to chain multiple redaction operations before finalizing the result. Paths can use either JSON Pointer (RFC 6901) or JSONPath (RFC 9535) syntax.
§Path Syntax
The syntax is auto-detected based on the path prefix:
§JSON Pointer (starts with /)
/field- top-level field/field/subfield- nested field/array/0- array index/field~1with~1slashes-~1escapes//field~0with~0tildes-~0escapes~
§JSONPath (starts with $)
$.field- top-level field$.items[*].id- allidfields in array$..id- allidfields anywhere (recursive)$[0:3]- array slice
Implementations§
Source§impl<T> RedactionBuilder<T>
impl<T> RedactionBuilder<T>
Sourcepub fn redact<R: Redactor>(
self,
path: &str,
redactor: R,
) -> Result<Self, ApiClientError>
pub fn redact<R: Redactor>( self, path: &str, redactor: R, ) -> Result<Self, ApiClientError>
Redacts values at the specified path using a redactor.
The path can be either JSON Pointer (RFC 6901) or JSONPath (RFC 9535). The syntax is auto-detected based on the prefix:
$...→ JSONPath (supports wildcards)/...→ JSON Pointer (exact path)
The redactor can be:
- A static value:
"replacement"orserde_json::json!(...) - A closure:
|path, val| transform(path, val)
§Arguments
path- Path expression (e.g.,/id,$.items[*].id)redactor- The redactor to apply (static value or closure)
§Errors
Returns an error if:
- The path is invalid
- The path matches no values
§Example
// Static value
let result = client
.get("/api/users/123")?
.await?
.as_json_redacted::<serde_json::Value>().await?
.redact("/id", "test-uuid")?
.finish()
.await;
// Closure for index-based IDs
let result = client
.get("/api/users")?
.await?
.as_json_redacted::<Vec<serde_json::Value>>().await?
.redact("$[*].id", |path, _val| {
let idx = path.split('/').nth(1).unwrap_or("0");
serde_json::json!(format!("user-{idx}"))
})?
.finish()
.await;Sourcepub fn redact_with_options<R: Redactor>(
self,
path: &str,
redactor: R,
options: RedactOptions,
) -> Result<Self, ApiClientError>
pub fn redact_with_options<R: Redactor>( self, path: &str, redactor: R, options: RedactOptions, ) -> Result<Self, ApiClientError>
Redacts values at the specified path with configurable options.
This is like redact but allows customizing
behavior through RedactOptions.
§Arguments
path- Path expression (e.g.,/id,$.items[*].id)redactor- The redactor to applyoptions- Configuration options
§Example
use clawspec_core::RedactOptions;
// Allow empty matches for optional fields
let options = RedactOptions { allow_empty_match: true };
builder
.redact_with_options("$.optional[*].field", "value", options)?
.finish()
.await;Sourcepub fn redact_remove(self, path: &str) -> Result<Self, ApiClientError>
pub fn redact_remove(self, path: &str) -> Result<Self, ApiClientError>
Removes values at the specified path.
This completely removes the field from objects or the element from arrays,
unlike setting it to null.
The path can be either JSON Pointer (RFC 6901) or JSONPath (RFC 9535).
§Arguments
path- Path expression to remove
§Errors
Returns an error if:
- The path is invalid
- The path matches no values
§Example
// Remove specific field
let result = client
.get("/api/users/123")?
.await?
.as_json_redacted::<serde_json::Value>().await?
.redact_remove("/password")?
.finish()
.await;
// Remove field from all array elements
let result = client
.get("/api/users")?
.await?
.as_json_redacted::<Vec<serde_json::Value>>().await?
.redact_remove("$[*].password")?
.finish()
.await;Sourcepub fn redact_remove_with(
self,
path: &str,
options: RedactOptions,
) -> Result<Self, ApiClientError>
pub fn redact_remove_with( self, path: &str, options: RedactOptions, ) -> Result<Self, ApiClientError>
Removes values at the specified path with configurable options.
This is like redact_remove but allows customizing
behavior through RedactOptions.
§Arguments
path- Path expression to removeoptions- Configuration options
§Example
use clawspec_core::RedactOptions;
// Allow empty matches for optional fields
let options = RedactOptions { allow_empty_match: true };
builder
.redact_remove_with("$.optional[*].field", options)?
.finish()
.await;Sourcepub async fn finish(self) -> RedactedResult<T>where
T: ToSchema + 'static,
pub async fn finish(self) -> RedactedResult<T>where
T: ToSchema + 'static,
Finalizes the redaction and returns the result.
This consumes the builder and returns a RedactedResult containing
both the original value and the redacted JSON.
The redacted JSON value is recorded as an example in both the OpenAPI
schema for type T and in the response content for this operation.