Schemas
Stof schemas and schemafy.
Last updated
Was this helpful?
Stof schemas and schemafy.
Last updated
Was this helpful?
Every object in Stof can be considered a schema, which can be applied to other objects with the "schemafy" function (in the ). This function uses a #[schema(...)] attribute on each field defined in the schema to manipulate and validate the corresponding fields found in the target object(s).
This might be useful in a variety of situations:
Transforming data into a different structure/schema than what was provided.
Alternative field names/locations
Subset of provided fields
Combination of fields
Filtering of data
Validate data against additional datasets, users, or configurations.
Protect data that gets sent to and from servers (access control/governance).
The most basic use case consists of two objects: a schema that contains the rules attached to each field we'd like to control and a target object that holds the data to which we want to apply our schema.
However, one can also apply schemas to itself and/or define schemas within Stof types.
In general, if you can think it, you can probably do it (or should be able to, so ).
Attributes get evaluated when parsed into a Stof value, and functions defined in a Stof type will get attached to a prototype object, not the object you might be expecting. However, you can always pass the expected "schema" as an argument to functions in the attribute (see for docs on the #[schema(...)]
attribute).