ketzal_validation 0.1.1

Declarative models and validation for the Ketzal framework
Documentation
use crate::model::schema_model::{Address, UserSchema};
use ketzal_validation::openapi::{Components, OpenApi};

fn user_schema() -> ketzal_validation::openapi::schema::Schema {
    UserSchema::schema()
}

#[test]
fn test_schema_type_from_rust_type() {
    let s = user_schema();
    assert_eq!(s.schema_type.as_deref(), Some("object"));
    assert_eq!(s.properties["name"].schema_type.as_deref(), Some("string"));
    assert_eq!(s.properties["age"].schema_type.as_deref(), Some("integer"));
    assert_eq!(s.properties["score"].schema_type.as_deref(), Some("number"));
    assert_eq!(
        s.properties["active"].schema_type.as_deref(),
        Some("boolean")
    );
}

#[test]
fn test_required_at_object_level() {
    let s = user_schema();
    assert_eq!(
        s.required,
        vec![
            "name", "email", "age", "role", "slug", "active", "address", "tags", "aliases"
        ]
    );
}

#[test]
fn test_option_not_required_and_nullable() {
    let s = user_schema();
    assert!(!s.required.contains(&"middle_name".to_string()));
    assert_eq!(s.properties["middle_name"].nullable, Some(true));
    assert_eq!(s.properties["score"].nullable, Some(true));
    assert_eq!(s.properties["name"].nullable, None);
}

#[test]
fn test_min_max_string_maps_to_length() {
    let s = user_schema();
    let name = &s.properties["name"];
    assert_eq!(name.min_length, Some(3));
    assert_eq!(name.max_length, Some(10));
    assert_eq!(name.minimum, None);
    assert_eq!(name.maximum, None);
}

#[test]
fn test_min_max_numeric_maps_to_minimum_maximum() {
    let s = user_schema();
    let age = &s.properties["age"];
    assert_eq!(age.minimum, Some(18.0));
    assert_eq!(age.maximum, Some(99.0));
    assert_eq!(age.min_length, None);
    assert_eq!(age.max_length, None);
}

#[test]
fn test_between_on_string_maps_to_length() {
    let s = user_schema();
    assert_eq!(s.properties["middle_name"].min_length, Some(2));
    assert_eq!(s.properties["middle_name"].max_length, Some(20));
}

#[test]
fn test_email_sets_format() {
    let s = user_schema();
    assert_eq!(s.properties["email"].format.as_deref(), Some("email"));
}

#[test]
fn test_in_sets_enum() {
    let s = user_schema();
    assert_eq!(
        s.properties["role"].enum_values,
        vec!["admin", "user", "guest"]
    );
}

#[test]
fn test_regex_sets_pattern() {
    let s = user_schema();
    assert_eq!(s.properties["slug"].pattern.as_deref(), Some("^[a-z]+$"));
}

#[test]
fn test_nested_struct_emits_reference() {
    let s = user_schema();
    assert_eq!(
        s.properties["address"].reference.as_deref(),
        Some("#/components/schemas/Address")
    );
    assert_eq!(s.properties["address"].schema_type, None);
}

#[test]
fn test_vec_of_primitive_emits_items() {
    let s = user_schema();
    let tags = &s.properties["tags"];
    assert_eq!(tags.schema_type.as_deref(), Some("array"));
    assert_eq!(
        tags.items.as_ref().unwrap().schema_type.as_deref(),
        Some("string")
    );
}

#[test]
fn test_vec_of_struct_emits_items_reference() {
    let s = user_schema();
    let aliases = &s.properties["aliases"];
    assert_eq!(aliases.schema_type.as_deref(), Some("array"));
    assert_eq!(
        aliases.items.as_ref().unwrap().reference.as_deref(),
        Some("#/components/schemas/Address")
    );
}

#[test]
fn test_serde_json_keyword_names() {
    let json = serde_json::to_value(user_schema()).unwrap();
    assert_eq!(json["type"], serde_json::json!("object"));
    assert_eq!(json["title"], serde_json::json!("UserSchema"));
    assert_eq!(
        json["properties"]["name"]["type"],
        serde_json::json!("string")
    );
    assert_eq!(
        json["properties"]["name"]["minLength"],
        serde_json::json!(3)
    );
    assert_eq!(
        json["properties"]["role"]["enum"],
        serde_json::json!(["admin", "user", "guest"])
    );
}

#[test]
fn test_serde_omits_unset_keywords() {
    let json = serde_json::to_value(user_schema()).unwrap();
    assert!(json.get("minimum").is_none());
    assert!(json["properties"]["active"].get("pattern").is_none());
    assert!(json["properties"]["active"].get("minLength").is_none());
}

#[test]
fn test_schema_reference_helper() {
    let s = ketzal_validation::openapi::schema::Schema::reference("Address");
    assert_eq!(s.reference.as_deref(), Some("#/components/schemas/Address"));
}

#[test]
fn test_components_registration() {
    let mut components = Components::new();
    components
        .register::<UserSchema>("User")
        .register::<Address>("Address");
    assert!(components.get("User").is_some());
    assert!(components.get("Address").is_some());
    assert!(components.get("Missing").is_none());
}

#[test]
fn test_openapi_document_json() {
    let mut api = OpenApi::new("Users API", "1.0.0");
    api.description("API de usuarios")
        .register::<UserSchema>("User")
        .register::<Address>("Address");

    let json: serde_json::Value = serde_json::from_str(&api.to_json().unwrap()).unwrap();
    assert_eq!(json["openapi"], serde_json::json!("3.0.3"));
    assert_eq!(json["info"]["title"], serde_json::json!("Users API"));
    assert_eq!(json["info"]["version"], serde_json::json!("1.0.0"));
    assert_eq!(
        json["info"]["description"],
        serde_json::json!("API de usuarios")
    );

    let user = &json["components"]["schemas"]["User"];
    assert_eq!(user["type"], serde_json::json!("object"));
    assert_eq!(
        user["properties"]["name"]["type"],
        serde_json::json!("string")
    );
    assert_eq!(
        user["properties"]["address"]["$ref"],
        serde_json::json!("#/components/schemas/Address")
    );
    assert_eq!(
        json["components"]["schemas"]["Address"]["type"],
        serde_json::json!("object")
    );
}

#[test]
fn test_openapi_document_yaml() {
    let mut api = OpenApi::new("Users API", "1.0.0");
    api.register::<UserSchema>("User");

    let yaml = api.to_yaml().unwrap();
    assert!(yaml.contains("openapi: 3.0.3"));
    assert!(yaml.contains("$ref"));
    assert!(yaml.contains("schemas:"));
}