Building and reading a schema in Python
Construct a Schema from Python instead of OSD text — and inspect one already in hand.
The same schema can be built from Python instead of parsed from OSD text — useful when a schema is generated programmatically rather than hand-written:
from omnist import schema, record, field, ref, nullable, t
address = record(field("street", t.string),
field("city", t.string))
user = record(
field("name", t.string),
field("emails", t.string, min=1, max=None), # [1,]
field("address", ref("Address")),
field("note", nullable(t.string)), # nullable scalar
)
s = schema(ref("User"), User=user, Address=address)
print(s.to_osd())Output
record User {
"name": string,
"emails" [1,]: string,
"address": Address,
"note": string?,
}
record Address {
"street": string,
"city": string,
}
root UserA schema already in hand — whether built this way or parsed from OSD text — can be walked the same way at runtime: .root is the entry-point reference, .env is the full name→record map, and each Record exposes its .fields:
print(s.root)
print(list(s.env.keys()))
for f in s.env["User"].fields:
print(f.label, f.type, f.min, f.max)Output
ref(User)
['Address', 'User']
name string 1 1
address ref(Address) 1 1This is how you'd introspect a schema at runtime — answering "what fields does this record declare, and what are their types and cardinalities?" — without re-parsing or guessing from the OSD text.