cddl-cat is a library for validating encoded data against a CDDL document that describes the expected structure of the data.

CDDL is a text document described by [RFC8610] that describes data structures. CDDL is not tied to any specific serialization or encoding method; it can be used to validate data that is in [CBOR] or JSON format.

The goal of this library is to make CBOR or JSON data easy to validate against a CDDL schema description.

cddl-cat supports Rust 1.37 and later.

Implementation Details

Examples

This example validates JSON-encoded data against a CDDL schema:

```rust use cddlcat::validatejson_str;

let cddlinput = "person = {name: tstr, age: int}"; let jsonstr = r#"{ "name": "Bob", "age": 43 }"#;

validatejsonstr("person", cddlinput, &jsonstr).unwrap(); ```

If the JSON data doesn't have the expected structure, an error will result: ```rust use cddlcat::validatejson_str;

let cddlinput = "person = {name: tstr, age: int}"; let jsonstr = r#"{ "name": "Bob", "age": "forty three" }"#;

assert!(validatejsonstr("person", cddlinput, &jsonstr).is_err()); ```

A similar example, verifying CBOR-encoded data against a CDDL schema: ```rust use cddlcat::validatecbor_bytes; use serde::Serialize;

[derive(Serialize)]

struct PersonStruct { name: String, age: u32, }

let input = PersonStruct { name: "Bob".tostring(), age: 43, }; let cborbytes = serdecbor::tovec(&input).unwrap(); let cddlinput = "person = {name: tstr, age: int}"; validatecborbytes("person", cddlinput, &cbor_bytes).unwrap(); ```

Supported prelude types: - any, uint, nint, int, bstr, bytes, tstr, text - float, float16, float32, float64, float16-32, float32-64 \ Note: float sizes are not validated.

Supported CDDL features: - Basic prelude types (integers, floats, bool, nil, text strings, byte strings) - Literal int, float, bool, UTF-8 text strings - Byte strings in UTF-8 or hex - Arrays and maps - Rule lookups by name - Groups - Choices (using / or // syntax) - Occurrences (?, *, +, or m*n) - Ranges (e.g. 1..7 or 1...8) - Unwrapping (~) - Map keys with cut syntax (^ =>)

Unimplemented features: - Generics - Extend type with /= - Extend group with //= - Type sockets with $ - Group sockets with $$ - Control operators, e.g. .size, .bits, ... - Group enumeration with & - Tagged data with # - Hexfloat literals (e.g. 0x1.921fb5p+1) - Base64 bytestring literals (b64'...') - Prelude types that invoke CBOR tags (e.g. tdate or biguint)