Rust

structure

Module

[Structure]: documents against their collection's attributes; and the registry of attribute formats.

Contents

1 submodule, and what the module defines itself.

use utopia_database::validator::structure::Structure;
Rust
pub struct Structure

A document valid for its collection: it names a collection, has every required attribute (unless the current document holds null there), has no unknown attribute (with a schema), and each value suits its attribute: type, size, signedness, array-ness and format; operators suit their attribute (OperatorValidator).

Rust
use utopia_database::Document;
use utopia_database::validator::structure::Structure;
use utopia_database::value::{Value, list, string};

let title = Document::from_pairs([
    ("$id", string("title")), ("type", string("string")), ("size", Value::Int(10)),
    ("required", Value::Bool(true)), ("array", Value::Bool(false)),
]).unwrap();
let collection = Document::from_pairs([
    ("$id", string("movies")), ("$collection", string("_metadata")), ("attributes", list([title.into()])),
]).unwrap();
let structure = Structure::new(collection, "integer");
let mut movie = Document::from_pairs([
    ("$collection", string("movies")), ("$createdAt", string("2024-01-01 00:00:00")),
    ("$updatedAt", string("2024-01-01 00:00:00")), ("title", string("Alien")),
]).unwrap();
assert!(structure.validate(&mut movie).unwrap());
movie.set("title", string("The Empire Strikes Back"));
assert!(!structure.validate(&mut movie).unwrap());
assert_eq!(
    structure.description(),
    "Invalid document structure: Attribute \"title\" has invalid type. Value must be a valid string and no longer than 10 chars"
);
use utopia_database::validator::structure::StructureOptions;
Rust
pub struct StructureOptions

Undocumented.

Also implements Default.

structure::formats

Own page

The registry of attribute formats: named validators an attribute's format selects, for one attribute type (an email format of string attributes, an enum of string, a range of integer).

use utopia_database::validator::structure::formats::Format;
Rust
pub struct Format

A registered format.

use utopia_database::validator::structure::formats::add;
Rust
pub fn add(name: &str, factory: Factory, kind: &str)

Registers (or replaces) a format.

use utopia_database::validator::structure::formats::all;
Rust
pub fn all() -> Vec<Format>

Every format, in registration order.

use utopia_database::validator::structure::formats::get;
Rust
pub fn get(name: &str, kind: &str) -> Result<Format>

Fails (crate::Kind::Database) with Format "..." not available for attribute type "..." or Unknown format validator "...".

use utopia_database::validator::structure::formats::has;
Rust
pub fn has(name: &str, kind: &str) -> bool

Undocumented.

use utopia_database::validator::structure::formats::remove;
Rust
pub fn remove(name: &str)

Undocumented.

Rust
pub type Factory = Arc<dyn Fn(&Document) -> Box<dyn utopia_validators::Validator> + Send + Sync>

Builds a format's validator for an attribute (its definition: size, formatOptions, ...).