Docs / Rust
OATS for Rust
The oatschema crate compiles the OATS schemas into your binary, so nothing touches the network or the filesystem, at build time or at run time.
cargo add oatschema
crates.iooatschema/oatschema-rsdocs.rsRust 1.85 or later
Get a schema
use oatschema::{SchemaName, schema_json};let text = schema_json(SchemaName::Classification, None)?; // the file, exactly as releasedassert!(text.contains(r#""$id": "https://oatschema.org/0.8.0/classification""#));assert_eq!(oatschema::SCHEMA_VERSION, "0.8.0"); // used when you don't pass a versionlet name: SchemaName = "llm-output".parse()?; // or SchemaName::LlmOutputlet pinned = schema_json(name, Some("0.8.0"))?;
Schema names are a SchemaName enum, so typos fail at compile time. Each schema is byte-identical to its release on this site, and the crate's tests check every file against a checksum manifest.
Validate a record
The crate has no validator of its own; pass a schema to any JSON Schema draft 2020-12 validator. With the jsonschema crate and the serde_json feature:
cargo add oatschema --features serde_jsoncargo add jsonschema serde_json
use oatschema::SchemaName;use serde_json::json;fn main() -> Result<(), Box<dyn std::error::Error>> { let schema = oatschema::schema(SchemaName::Classification, None)?; let validator = jsonschema::options().should_validate_formats(true).build(&schema)?; assert!(validator.is_valid(&json!({"labels": [{"label": "cat", "score": 0.92}]}))); for error in validator.iter_errors(&json!({"labels": [{"label": "cat", "score": 1.5}]})) { println!("{}: {error}", error.instance_path()); // /labels/0/score: 1.5 is greater than the maximum of 1 } Ok(())}
Build each validator once and reuse it. Turn on format checking (uri, date-time and so on), as above, to match the OATS packages for other languages. x-license and x-version are the only non-standard keywords, and draft 2020-12 validators treat them as annotations.
Pin a schema version
Before OATS 1.0, a new release may reject records an older one accepted, so pass the version a record was written for: schema_json(name, Some("0.8.0")).
versionis anOption<&str>;NonemeansSCHEMA_VERSION. It must be exact:"0.8.0", never"0.8"or"latest".- Each
$idincludes its version, so several versions can share one validator registry. - The crate bundles OATS 0.8.0, the first public release, and every later one. For 0.4.0 to 0.7.0, use the Python package, or fetch them from this site.
More on releases in Versions and URLs.
API and features
schema_json(name, version) |
The schema file's text, exactly as released, as a &'static str. |
|---|---|
schema(name, version) |
The schema as a new serde_json::Value on every call. Needs the serde_json feature. |
available_versions() |
Every bundled schema version, oldest first. |
SCHEMA_VERSION |
The default schema version, the newest bundled. |
SchemaName |
The schema names, with ALL, as_str, Display and FromStr. |
Error |
UnknownName, UnknownVersion or NotInVersion. |
SchemaName and Error are #[non_exhaustive], so new schemas and error cases don't break your matches. The only feature is serde_json, off by default, which adds schema().
Crate versions
The crate has its own semantic version, which never encodes a schema version. Upgrading never removes a schema version. Before 1.0, a release that bundles a new OATS release, and so changes the default SCHEMA_VERSION, is a minor bump, so Cargo's default 0.x.y requirement keeps your default fixed. Raising the minimum supported Rust version is also a minor bump.
Other languages: Python and JavaScript. Schema changes belong in oatschema/oats; package issues go to oatschema/oatschema-rs.