Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 170 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,171 @@
# Lottie Validator
# Lottie Tools

A Rust library providing tools for validating and sanitizing Lottie animation assets. This library is designed to help ensure Lottie files are properly formatted, secure, and optimized for use in applications.

## Overview

Lottie Tools provides two main modules:

- **Sanitizer**: Removes unnecessary or potentially problematic properties from Lottie JSON files
- **Validator**: Validates Lottie JSON files against JSON schemas


## Features

### Sanitization

The sanitizer removes various properties that are not essential for Lottie playback:

- **Root-level cleanup**: Removes version info (`v`) and properties (`props`) from the root object
- **General cleanup**: Removes index properties (`ix`), MIME names (`mn`), composition indices (`cix`), and other metadata
- **Conditional cleanup**: Intelligently removes properties based on context:
- Layer-specific properties are only removed from non-layer objects
- Pre-composition properties are removed from non-precomp layers
- Zero-value properties like `ddd`, `bm`, and `ao` are removed when set to 0
- Asset-specific properties are handled appropriately


### Validation

The validator provides JSON Schema validation capabilities:

- Validates Lottie JSON against provided schemas
- Built specifically for Lottie schema requirements
- Supports both string and JSON Value inputs
- Comprehensive error handling and reporting


## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
lottie-tools = "0.1.0"
```


## Usage

### Sanitizing Lottie Files

```rust
use lottie_tools::sanitizer::sanitize_lottie;
use serde_json::Value;

// Load your Lottie JSON
let mut lottie_json: Value = serde_json::from_str(lottie_string)?;

// Sanitize the Lottie file
sanitize_lottie(&mut lottie_json);

// The lottie_json is now sanitized and optimized
```


### Validating Lottie Files

```rust
use lottie_tools::validator::{validate, Schema};
use serde_json::Value;

// Validate using string inputs
let is_valid = validate(schema_string, lottie_string);

// Or validate using JSON Values
let schema: Value = serde_json::from_str(schema_string)?;
let lottie: Value = serde_json::from_str(lottie_string)?;

let schema_obj = Schema::from_json(&schema)?;
let validation_result = schema_obj.validate(&lottie);
```


## API Reference

### Sanitizer Module

- `sanitize_lottie(lottie: &mut Value)`: Main function to sanitize a Lottie JSON object


### Validator Module

- `validate<S: AsRef<str>>(schema: S, asset: S) -> bool`: Validates a Lottie string against a schema string
- `validate_json(schema: &Value, asset: &Value) -> bool`: Validates Lottie JSON against a schema JSON
- `Schema::from_json(input: &Value) -> Result<Self, SchemaError>`: Creates a schema object from JSON
- `Schema::validate(instance: &Value) -> Result<(), ValidationError>`: Validates an instance against the schema


## Implementation Details

### Sanitization Rules

The sanitizer follows specific rules based on the [official Lottie documentation](https://lottie.github.io) and [community documentation](https://lottiefiles.github.io/lottie-docs/):

1. **Always removed**: `ix`, `mn`, `cix`, `np`, `l`, `td`, `cl`, `ct`
2. **Root-only removal**: `v`, `props`
3. **Non-layer removal**: `ind`
4. **Asset-specific removal**: `fr` (when `id` is present)
5. **Non-precomp removal**: `sr`, `st` (when layer type ≠ 0)
6. **Zero-value removal**: `ddd`, `bm`, `ao` (when value is 0)

### Schema Validation

The validator implements a JSON Schema validator specifically optimized for Lottie files. It supports:

- Reference resolution within schemas
- Type validation
- Constraint validation
- Comprehensive error reporting


## Contributing

We welcome contributions! Please ensure your code:

- Follows Rust best practices and idioms
- Includes comprehensive tests
- Maintains backward compatibility
- Is properly documented


### Running Tests

```bash
cargo test
```


### Code Coverage

```bash
cargo tarpaulin --out Html
```


## License

Licensed under the Apache License, Version 2.0. See the [LICENSE](https://www.apache.org/licenses/LICENSE-2.0) file for details.

## Copyright

Copyright 2025 Google LLC

## Dependencies

- `serde_json`: For JSON parsing and manipulation
- `thiserror`: For error handling
- `regex`: For pattern matching in validation


## Future Improvements

The codebase includes several TODOs for future enhancements:

- Better error handling in validation (avoiding panics)
- Full failure result reporting
- Enhanced handling of user-defined keys in slots
- Verification of array length matches for certain properties

This library is actively maintained and optimized for production use in Lottie animation processing pipelines.

This library allows fast verification of Lottie assets against [Lottie spec](https://lottie.github.io/lottie-spec/1.0) and other profiles (such as Wear OS low power profile).