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;pub struct StructureA 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).
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;pub struct StructureOptionsUndocumented.
Default.structure::formats
Own pageThe 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;pub struct FormatA registered format.
use utopia_database::validator::structure::formats::add;pub fn add(name: &str, factory: Factory, kind: &str)Registers (or replaces) a format.
use utopia_database::validator::structure::formats::all;pub fn all() -> Vec<Format>Every format, in registration order.
use utopia_database::validator::structure::formats::get;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;pub fn has(name: &str, kind: &str) -> boolUndocumented.
use utopia_database::validator::structure::formats::remove;pub fn remove(name: &str)Undocumented.
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, ...).