Skip to main content

Module schema

Module schema 

Source
Expand description

The Schema model – Record/Scalar/Ref, per docs/design/model.md (issue #6). Ported from ~/dev/omnist/omnist/schema.py.

  • Record – a closed set of fields, each (label, type, cardinality). Cardinality is the unordered number of times a label may appear.
  • Scalar – exactly one of seven predefined value types (string, integer, number, boolean, date, time, datetime), optionally nullable. There is no user-declared value-domain composition (no union/enum/literal) – see docs/design/model.md §2/§5 for why: a composable value-domain would make schema-directed deserialization ambiguous (a value could satisfy more than one candidate with no principled way to choose).
  • Ref – a pointer into the schema’s named environment (records only); enables reuse and recursion.
  • Any (FieldType::Any) – accepts every legal document value unchecked. Ported from Python’s AnyType/ANY singleton, which has been fully implemented and shipped there since v0.5.0 – not a speculative or deferred feature (the separate, still-unresolved question is whether any should be a permanent part of the spec long-term; that governance question is untouched by this port simply catching up to Python’s existing behavior, see omnist-rs issue #29).

A field’s type is a Ref, a Scalar, or Any. There are no inline records and no separate array type – “array” is a field with cardinality max > 1. Validation ignores order (per docs/design/model.md §7).

§Temporal shape-check

is_iso_date, is_iso_time, and is_iso_datetime are the single source of truth for “is this string shaped like (and a semantically valid) date/time/datetime,” pub(crate) so a future materialize/ infer module can reuse the exact same check instead of writing a second, independently-maintained copy (per the porting playbook’s pitfall list). Each check is stricter than a bare shape regex: the regex only rules out the wrong spelling (Python’s datetime.fromisoformat is deliberately wider – it also accepts ISO-8601 basic format (20240101), week dates, and other spellings this crate’s docs never promise) – the calendar/clock fields are additionally range-checked (e.g. a syntactically-shaped "2024-02-30" is still rejected).

Structs§

Field
One named, cardinality-bound field slot of a record: label of type, occurring [min, max] times (max = None is unbounded).
Record
A closed set of named fields (constrained by its child labels).
Ref
A reference to a named record in a Schema’s environment.
Scalar
One of the seven predefined value types, optionally nullable.
Schema
A schema: a root reference plus an environment of named records.
ValidationError
One validation failure: where, what, and a stable code.
ValidationResult
The outcome of Schema::validate: empty on success, one entry per problem found (validation collects every error, not just the first).

Enums§

ErrorCode
A stable machine-readable validation failure code, mirroring the Python reference’s Error.code values.
ErrorFamily
Which top-level operation produced an ErrorCodevalidate and materialize share this one ErrorCode/ValidationResult mechanism (see ValidationResult::add’s doc comment), but omnist-spec §8.3.1 namespaces error codes per call-site family (validate.* vs. materialize.*), not per underlying check, so the family has to be threaded through at the point a code is stringified rather than baked into ErrorCode itself.
FieldType
A field’s type: a Ref to a named record, a Scalar, or Any (accepts every legal document value – ported from Python’s AnyType/ANY singleton, shipped there since v0.5.0). Any is not a Scalar (it has no kind and no nullable flag – null is already included) and not a Ref (it names nothing), so it gets its own unit variant rather than being folded into either.
Resolved
A resolved field type: a record (via a Ref), a bare Scalar, or Any.
ScalarKind
One of the seven predefined value kinds a Scalar can hold (spec §2.2).

Constants§

BOOLEAN
Non-nullable boolean scalar constant (spec §2.2, §3).
DATE
Non-nullable date scalar constant (spec §2.2, §3).
DATETIME
Non-nullable datetime scalar constant (spec §2.2, §3).
INTEGER
Non-nullable integer scalar constant (spec §2.2, §3).
NUMBER
Non-nullable number scalar constant (spec §2.2, §3).
STRING
Non-nullable string scalar constant (spec §2.2, §3).
TIME
Non-nullable time scalar constant (spec §2.2, §3).

Functions§

matches_kind
Does value match scalar kind kind? Mirrors Python’s matches_kind – validation only checks, it never converts (see docs/design/model.md §10).
nullable
A copy of scalar that also accepts null (the ? form).