| Crates.io | dog-schema |
| lib.rs | dog-schema |
| version | 0.1.4 |
| created_at | 2026-01-23 03:51:59.321356+00 |
| updated_at | 2026-01-25 03:36:29.58348+00 |
| description | Schema definition and validation utilities for DogRS - JSON schema, validation, and type safety |
| homepage | |
| repository | https://github.com/Jitpomi/dogrs |
| max_upload_size | |
| id | 2063350 |
| size | 13,051 |
Schema definition and validation utilities for DogRS - JSON schema, validation, and type safety
dog-schema provides powerful schema definition and validation capabilities for the DogRS ecosystem, enabling type-safe data handling with compile-time and runtime validation.
Add to your Cargo.toml:
[dependencies]
dog-schema = "0.1.3"
use dog_schema::{Schema, Validate};
use serde::{Deserialize, Serialize};
#[derive(Schema, Serialize, Deserialize)]
struct User {
#[schema(min_length = 1, max_length = 100)]
name: String,
#[schema(email)]
email: String,
#[schema(range(min = 0, max = 150))]
age: u8,
}
// Generate JSON schema
let schema = User::json_schema();
// Validate data
let user_data = serde_json::json!({
"name": "Alice",
"email": "alice@example.com",
"age": 30
});
let user: User = User::from_json(&user_data)?;
#[derive(Schema)]
struct TextData {
#[schema(min_length = 5, max_length = 50)]
title: String,
#[schema(pattern = r"^[A-Z][a-z]+$")]
name: String,
#[schema(email)]
email: String,
#[schema(url)]
website: String,
}
#[derive(Schema)]
struct NumericData {
#[schema(range(min = 0, max = 100))]
percentage: f64,
#[schema(minimum = 1)]
count: u32,
#[schema(multiple_of = 5)]
step: i32,
}
#[derive(Schema)]
struct CollectionData {
#[schema(min_items = 1, max_items = 10)]
tags: Vec<String>,
#[schema(unique_items)]
categories: Vec<String>,
}
Create custom validation logic:
use dog_schema::{Schema, ValidationError, Validator};
struct PasswordValidator;
impl Validator<String> for PasswordValidator {
fn validate(&self, value: &String) -> Result<(), ValidationError> {
if value.len() < 8 {
return Err(ValidationError::new("Password must be at least 8 characters"));
}
if !value.chars().any(|c| c.is_uppercase()) {
return Err(ValidationError::new("Password must contain uppercase letter"));
}
Ok(())
}
}
#[derive(Schema)]
struct Account {
username: String,
#[schema(validator = "PasswordValidator")]
password: String,
}
Use schemas with dog-core services:
use dog_core::{DogService, TenantContext};
use dog_schema::Schema;
#[derive(Schema)]
struct CreateUserRequest {
#[schema(min_length = 1)]
name: String,
#[schema(email)]
email: String,
}
struct UserService;
#[async_trait]
impl DogService<CreateUserRequest, ()> for UserService {
type Output = User;
async fn create(&self, tenant: TenantContext, data: CreateUserRequest) -> Result<User> {
// Data is already validated by the schema
// Implement your business logic here
Ok(User {
id: generate_id(),
name: data.name,
email: data.email,
})
}
}
Generate standard JSON schemas for API documentation:
use dog_schema::Schema;
#[derive(Schema)]
struct ApiResponse {
success: bool,
data: Option<serde_json::Value>,
error: Option<String>,
}
// Generate JSON Schema
let schema = ApiResponse::json_schema();
println!("{}", serde_json::to_string_pretty(&schema)?);
Output:
{
"type": "object",
"properties": {
"success": {"type": "boolean"},
"data": {"type": ["object", "null"]},
"error": {"type": ["string", "null"]}
},
"required": ["success"]
}
Comprehensive error reporting:
match User::from_json(&invalid_data) {
Ok(user) => println!("Valid user: {:?}", user),
Err(errors) => {
for error in errors {
println!("Validation error at {}: {}", error.path, error.message);
}
}
}
dog-schema integrates with the DogRS ecosystem:
┌─────────────────┐
│ Your App │ ← Business logic with validated types
└─────────────────┘
│
┌────┴────┐
│ │
┌───▼───┐ ┌──▼──────┐
│dog- │ │dog- │ ← Adapters
│axum │ │schema │
└───────┘ └─────────┘
│ │
└────┬────┘
▼
┌─────────────────┐
│ dog-core │ ← Core abstractions
└─────────────────┘
See dog-examples/ for complete applications using schema validation:
MIT OR Apache-2.0
Made by Jitpomi