Skip to main content

ModelValidate

Trait ModelValidate 

Source
pub trait ModelValidate: Sized {
    // Required method
    fn model_validate(
        input: impl Into<ValidateInput>,
        options: ValidateOptions,
    ) -> ValidateResult<Self>;

    // Provided methods
    fn model_validate_json(json: &str) -> ValidateResult<Self> { ... }
    fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self> { ... }
}
Expand description

Trait for models that support model_validate().

This is typically implemented via derive macro or blanket impl for models that implement Deserialize.

Required Methods§

Source

fn model_validate( input: impl Into<ValidateInput>, options: ValidateOptions, ) -> ValidateResult<Self>

Create and validate a model from input.

§Arguments
  • input - The input to validate (Dict, Json, or JsonValue)
  • options - Validation options
§Returns

The validated model or validation errors.

§Example
use sqlmodel_core::validate::{ModelValidate, ValidateInput, ValidateOptions};

let user = User::model_validate(
    r#"{"name": "Alice", "age": 30}"#,
    ValidateOptions::default()
)?;

Provided Methods§

Source

fn model_validate_json(json: &str) -> ValidateResult<Self>

Create and validate a model from JSON string with default options.

Source

fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self>

Create and validate a model from a HashMap with default options.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: DeserializeOwned> ModelValidate for T

Blanket implementation of ModelValidate for types that implement DeserializeOwned.

This provides model_validate() for any model that can be deserialized from JSON.