Skip to main content

ValueRedactionBuilder

Struct ValueRedactionBuilder 

Source
pub struct ValueRedactionBuilder { /* private fields */ }
Available on crate feature redaction only.
Expand description

Builder for redacting arbitrary JSON values.

Unlike RedactionBuilder, this builder works with any serde_json::Value without requiring HTTP response context or OpenAPI collection.

§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 - ~1 escapes /
  • /field~0with~0tildes - ~0 escapes ~

§JSONPath (starts with $)

  • $.field - top-level field
  • $.items[*].id - all id fields in array
  • $..id - all id fields anywhere (recursive)
  • $[0:3] - array slice

§Example

use clawspec_core::redact_value;
use serde_json::json;

let openapi_json = json!({
    "paths": {
        "/users": {
            "get": {
                "responses": {
                    "200": {
                        "content": {
                            "application/json": {
                                "example": {
                                    "id": "real-uuid",
                                    "created_at": "2024-12-28T10:30:00Z"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
});

let stabilized = redact_value(openapi_json)
    .redact("$..example.id", "ENTITY_ID").unwrap()
    .redact("$..example.created_at", "TIMESTAMP").unwrap()
    .finish();

Implementations§

Source§

impl ValueRedactionBuilder

Source

pub fn new(value: Value) -> Self

Create a new builder for the given JSON value.

Source

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" or serde_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
use clawspec_core::redact_value;
use serde_json::json;

let value = json!({"id": "uuid-123", "name": "Test"});

// Static value
let redacted = redact_value(value)
    .redact("/id", "stable-uuid").unwrap()
    .finish();

assert_eq!(redacted["id"], "stable-uuid");
Source

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 apply
  • options - Configuration options
§Example
use clawspec_core::{redact_value, RedactOptions};
use serde_json::json;

let value = json!({"id": "test"});

// Allow empty matches for optional fields
let options = RedactOptions { allow_empty_match: true };

let redacted = redact_value(value)
    .redact_with_options("$.optional_field", "value", options).unwrap()
    .finish();
Source

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
use clawspec_core::redact_value;
use serde_json::json;

let value = json!({"id": "keep", "secret": "remove"});

let redacted = redact_value(value)
    .redact_remove("/secret").unwrap()
    .finish();

assert!(redacted.get("secret").is_none());
assert_eq!(redacted["id"], "keep");
Source

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 remove
  • options - Configuration options
§Example
use clawspec_core::{redact_value, RedactOptions};
use serde_json::json;

let value = json!({"id": "test"});

// Allow empty matches for optional fields
let options = RedactOptions { allow_empty_match: true };

let redacted = redact_value(value)
    .redact_remove_with("$.optional_field", options).unwrap()
    .finish();
Source

pub fn finish(self) -> Value

Finalize and return the redacted value.

This consumes the builder and returns the modified JSON value.

Trait Implementations§

Source§

impl Clone for ValueRedactionBuilder

Source§

fn clone(&self) -> ValueRedactionBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ValueRedactionBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more