valid

Function valid 

Source
pub fn valid(value: &Value) -> Result<bool, Option<String>>
Expand description

Validate a value according to Gun’s rules and detect soul references

This is the main validation function matching Gun.js valid(). It checks if a value is valid for storage in Gun and detects if it’s a soul reference.

§Arguments

  • value - The JSON value to validate

§Returns

  • Ok(true) - Valid simple value (null, string, boolean, or valid number)
  • Err(Some(soul)) - It’s a soul reference object {"#": soul}
  • Ok(false) - Invalid value (Infinity, NaN, array, or complex object)

§Example

use gun::valid::valid;
use serde_json::json;

// Valid simple value
assert_eq!(valid(&json!("hello")), Ok(true));

// Valid number
assert_eq!(valid(&json!(42)), Ok(true));

// Invalid number
assert_eq!(valid(&json!(f64::INFINITY)), Ok(false));

// Soul reference
match valid(&json!({"#": "user_123"})) {
    Err(Some(soul)) => println!("Found soul reference: {}", soul),
    _ => {}
}