ketzal_validation 0.1.1

Declarative models and validation for the Ketzal framework
Documentation
use serde::Serialize;
use std::collections::HashMap;

#[derive(Debug, Clone, Default, Serialize)]
pub struct Schema {
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub schema_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub example: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<serde_json::Value>,
    #[serde(rename = "readOnly", skip_serializing_if = "Option::is_none")]
    pub read_only: Option<bool>,
    #[serde(rename = "writeOnly", skip_serializing_if = "Option::is_none")]
    pub write_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nullable: Option<bool>,
    #[serde(rename = "$ref", skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub properties: HashMap<String, Schema>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub required: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<Schema>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum: Option<f64>,
    #[serde(rename = "minLength", skip_serializing_if = "Option::is_none")]
    pub min_length: Option<u64>,
    #[serde(rename = "maxLength", skip_serializing_if = "Option::is_none")]
    pub max_length: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pattern: Option<String>,
    #[serde(rename = "minItems", skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u64>,
    #[serde(rename = "maxItems", skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u64>,
    #[serde(rename = "enum", skip_serializing_if = "Vec::is_empty")]
    pub enum_values: Vec<String>,
}

pub trait ToSchema {
    fn schema() -> Schema;
}

impl Schema {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn string() -> Self {
        Self {
            schema_type: Some("string".to_string()),
            ..Self::default()
        }
    }

    pub fn integer() -> Self {
        Self {
            schema_type: Some("integer".to_string()),
            ..Self::default()
        }
    }

    pub fn number() -> Self {
        Self {
            schema_type: Some("number".to_string()),
            ..Self::default()
        }
    }

    pub fn boolean() -> Self {
        Self {
            schema_type: Some("boolean".to_string()),
            ..Self::default()
        }
    }

    pub fn object() -> Self {
        Self {
            schema_type: Some("object".to_string()),
            ..Self::default()
        }
    }

    pub fn array(items: Schema) -> Self {
        Self {
            schema_type: Some("array".to_string()),
            items: Some(Box::new(items)),
            ..Self::default()
        }
    }

    pub fn reference(name: impl Into<String>) -> Self {
        Self {
            reference: Some(format!("#/components/schemas/{}", name.into())),
            ..Self::default()
        }
    }
}

pub type OpenApiSchema = Schema;