Field Name Type Checking / Formatting in Rendering Contents Resolvers
Overview
Transport of key value pairs is a new challenge in the world of headless Sitecore. Specifically, wherever the worlds of front end and back end collide in the form of APIs that transmit JSON data. The top considerations are:
- Type checking / safety
- Developer experience
- Maintainability
- Consistency
- Following naming conventions for different areas of the codebase
One example of the changing paradigm is that in the past, spaces were common in template field names. These days, not so much; especially with the rise of JSS, where field names are camel cased. In JS, its much easier to work with camel cased field names (JSON object keys) as compared to ones with spaces.
This was something I brought up on SSE a while back: https://sitecore.stackexchange.com/questions/33790/jss-template-field-names-how-to-automatically-serialize-field-names-in-camelcas
Type Checking / Formatting Example
Here is a useful example of how to type check field names when specifying a key for a key value pair in a Rendering Contents Resolver. This is useful when you want to ensure that the field name you're using is correct and that it matches the expected JSON format.
By employing nameof()
, we create a direct reference to the field. This means if the field's name ever changes, the compiler will notify us immediately of any discrepancies. This is okay if you have full control over both and if your coding standards match the expected JSON naming conventions (CamelCase in this case). If not, you may end up with JSON that doesn't match the expected format.
Keep cruising,
MG