Skip to main content

objectiveai_sdk/functions/check/
check_scalar_fields.rs

1//! Validation of scalar function fields (input_schema only).
2//!
3//! Verifies that the input schema produces enough diverse example inputs.
4
5use rand::SeedableRng;
6use rand::rngs::StdRng;
7use serde::Deserialize;
8
9use super::check_input_schema::check_input_schema;
10use super::example_inputs;
11use crate::functions::expression::InputSchema;
12use schemars::JsonSchema;
13
14/// The fields needed to validate a scalar function's input behavior.
15#[derive(Debug, Clone, Deserialize, JsonSchema)]
16#[schemars(rename = "functions.check.ScalarFieldsValidation")]
17pub struct ScalarFieldsValidation {
18    pub input_schema: InputSchema,
19}
20
21/// Validate that the scalar fields are correct.
22///
23/// Generates example inputs from the `input_schema` and verifies that at least
24/// one input can be produced.
25pub fn check_scalar_fields(
26    fields: ScalarFieldsValidation,
27    seed: Option<i64>,
28) -> Result<(), String> {
29    check_input_schema(&fields.input_schema)?;
30
31    let rng = match seed {
32        Some(s) => StdRng::seed_from_u64(s as u64),
33        None => StdRng::from_os_rng(),
34    };
35
36    let mut count = 0usize;
37    for (_, ref _input) in
38        example_inputs::generate_seeded(&fields.input_schema, rng).enumerate()
39    {
40        count += 1;
41    }
42
43    if count == 0 {
44        return Err(
45            "SF01: Failed to generate any example inputs from input_schema"
46                .to_string(),
47        );
48    }
49
50    Ok(())
51}