Astro's Content Collections: Support for Custom Object Transformations #1119
FlorianFlatscher
started this conversation in
Proposal
Replies: 1 comment
-
Another goal of this could be to allow custom content loaders to transform the schema. Currently the content schema (input) and the content collection (output) need to match. To allow adding new attributes to a schema within a custom content loader would be another great goal, so a content loader can calculate new properties and add them to the output. An example would be reading the locale from the file path. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Summary
Enable Astro's content collections to support custom object transformations, such as converting dates using libraries like Luxon, by allowing configuration of the serialization process.
Background & Motivation
Currently, when using
z.transform()
in content collection schemas to construct custom objects (e.g., transforming dates with Luxon), Astro's serialization process encounters issues. This is because Astro serializes the result of the schema using thedevalue
library, which doesn't recognize custom object types by default. Althoughdevalue
supports registering custom types, Astro doesn't currently expose a way to modify its configuration. This limitation hinders developers from effectively utilizing powerful libraries like Luxon for data transformation within content collections. A related issue has been discussed in the Astro GitHub repository: withastro/astro#6764Goals
z.transform()
in content collection schemas to create custom objects without serialization errors.devalue
library within Astro's configuration API to register custom types.Example
Consider a scenario where a developer wants to transform a date string into a Luxon
DateTime
object within a content collection schema:Currently, this transformation results in a serialization error because
devalue
cannot handle theDateTime
object. By allowing developers to configuredevalue
to recognize custom types, this transformation would work seamlessly, enabling more powerful and flexible data handling within Astro's content collections.Implementing this proposal would significantly improve the developer experience by allowing the use of advanced data transformation libraries within Astro's content collections, thereby enhancing the framework's flexibility and capability.
Beta Was this translation helpful? Give feedback.
All reactions