Skip to content

Fix: Type checker doesn't detect usage of optional properties #11 - #238

Closed
Marvinrose wants to merge 1 commit into
accordproject:mainfrom
Marvinrose:fix-issue-11
Closed

Fix: Type checker doesn't detect usage of optional properties #11#238
Marvinrose wants to merge 1 commit into
accordproject:mainfrom
Marvinrose:fix-issue-11

Conversation

@Marvinrose

@Marvinrose Marvinrose commented Mar 14, 2025

Copy link
Copy Markdown

This PR addresses a runtime error encountered when an optional variable is used in a template (including outside of TypeScript expressions) but is missing from the provided data. This issue can lead to a "No values found" error during template rendering.

The template engine currently throws a runtime error when an optional variable is referenced in a template and is not present in the data. This behaviour is observed even when the variable is not used within a TypeScript expression, but its presence in the template context (especially alongside other TypeScript expressions) seems to trigger the error.

Steps to Reproduce:

  1. Define the following Model (e.g., hello@1.0.0.cto):
    cto
    namespace hello@1.0.0

    @template
    concept HelloWorld {
    o String name
    o String last optional
    }
    Define the following Template (e.g., grammar.tem.md):

Markdown

Hello {{name}}!

Today is {{% return now.toISOString() %}}.

{{last}}
Provide Data that is missing the optional last field:

JSON

{
"$class": "hello@1.0.0.HelloWorld",
"name": "John Doe"
}
Attempt to render the template using the TemplateMarkInterpreter.

Actual Behaviour: A runtime error "Error: No values found for path '$['last']' in data [object Object]." occurs.

Solution/Workaround Implemented in Code:

A workaround is to use a TypeScript if statement within the template to conditionally render content based on the presence of the optional variable in the data.

Example:

Markdown

Hello {{name}}!

Today is {{% return now.toISOString() %}}.

{{% if (data.last) { return data.last; } else { return ''; } %}}

Possible Fix:

To address this issue within the template engine itself, the TemplateMarkInterpreter should be modified to check for the existence of optional variables in the data before attempting to access their values. This could involve returning a default value (like undefined or an empty string) or simply skipping the rendering of the missing optional variable without throwing a fatal error.

My Environment:

Version used - ESLint Version: v9.21.0
Environment name and version (e.g. Chrome 39, node.js 5.4) - NPM Version: v10.8.2, Node.js Version: v18.20.7
Operating System and version (desktop or mobile) - Operating System: Windows 11 Home Single Language (Build 22631)
Link to your project - Template Playground: https://github.com/accordproject/template-playground

Screenshot (559)
Screenshot (558)

…roject#11

Signed-off-by: Chibuezem Marvinrose <rozzeymarvin32@gmail.com>
@Marvinrose
Marvinrose requested a review from a team as a code owner March 14, 2025 01:47
@netlify

netlify Bot commented Mar 14, 2025

Copy link
Copy Markdown

Deploy Preview for ap-template-playground ready!

Name Link
🔨 Latest commit aca39b8
🔍 Latest deploy log https://app.netlify.com/sites/ap-template-playground/deploys/67d38acc773dfa0008bde922
😎 Deploy Preview https://deploy-preview-238--ap-template-playground.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@DianaLease
DianaLease requested review from dselman and mttrbrts March 14, 2025 13:59

@mttrbrts mttrbrts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @Marvinrose .

Unfortunately, this PR doesn't fix the root cause of #11

This fix is likely to need a change in accordproject/template-engine to improve type-checking for all examples of the same issue. Instead your change removes the error by changing the template.

The value of the template-engine is to catch error at compile time, rather than at run-time. This gives us confidence that templates won't fail unexpectedly at runtime.

@Marvinrose

Copy link
Copy Markdown
Author

Hi @mttrbrts,

Thank you for your review and feedback on my previous attempt. I’ve reworked this PR to directly address the root cause of Issue #11 by enhancing type checking within accordproject/template-engine, rather than modifying the template itself.

here accordproject/template-engine#36

kindly review please.

@ekarademir ekarademir closed this Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants