Skip to content

feat: add template validation and static analysis #104#105

Open
Ananya-vastare wants to merge 1 commit into
accordproject:mainfrom
Ananya-vastare:feature/template-validation
Open

feat: add template validation and static analysis #104#105
Ananya-vastare wants to merge 1 commit into
accordproject:mainfrom
Ananya-vastare:feature/template-validation

Conversation

@Ananya-vastare
Copy link
Copy Markdown

#104
This feature introduces a validation step for TemplateMark templates before rendering.
It performs static analysis to detect missing variables referenced in the template and fields defined in the data model but unused in the template.

The goal is to improve developer experience by catching template errors early in the development process.
This validation helps ensure better consistency between TemplateMark templates and their associated Concerto data models.

Signed-off-by: Ananya Vastare <116643029+Ananya-vastare@users.noreply.github.com>
@Ananya-vastare Ananya-vastare force-pushed the feature/template-validation branch from 3ae7eda to 30c21df Compare March 10, 2026 12:15
@Ananya-vastare
Copy link
Copy Markdown
Author

@ekarademir @jamieshorten @dselman @martinhalford please look into this pr thank you

@mttrbrts
Copy link
Copy Markdown
Member

Thanks for the contribution! However, this approach doesn't align with the project's design philosophy.

The Accord Project libraries are designed so that validation is handled by Concerto (specifically @accordproject/concerto-core), and applications can choose whether to use validation or not. This separation of concerns is intentional.

A few issues with the current approach:

  1. The regex-based variable detection ({{variable}}) doesn't account for TemplateMark's full syntax (blocks like {{#if}}, {{#optional}}, {{#with}}, etc.)
  2. Checking variables against a plain Record<string, any> model misses the type information that Concerto provides
  3. This duplicates what Concerto's validation layer already handles

If you're interested in validation improvements, I'd suggest looking at how Concerto validation is used elsewhere in the codebase — the goal would be to leverage existing Concerto capabilities rather than adding a parallel validation system.

This comment was generated by AI on behalf of @mttrbrts.

@Ananya-vastare
Copy link
Copy Markdown
Author

Thanks for the feedback — that makes sense, and I see the issue with duplicating Concerto’s responsibilities.

I’ve updated the approach to align with the design philosophy:

  1. Removed regex-based template parsing entirely (no {{variable}} scanning)
  2. Removed manual Record<string, any> validation logic
  3. Delegated all data validation fully to @accordproject/concerto-core using ModelManager + Serializer
  4. Treated TemplateMark purely as a rendering concern, not a validation source
  5. Any cross-checking between template and data is now optional and treated as warnings only (not validation errors), and does not replicate Concerto’s type system

So now:

  1. Concerto is the single source of truth for validation
  2. TemplateMark is only used for template structure/rendering
  3. The previous parallel validation layer has been removed

Let me know if you'd like me to align further with how other parts of the codebase are handling Concerto validation — happy to adjust.
@mttrbrts

@mttrbrts
Copy link
Copy Markdown
Member

mttrbrts commented May 9, 2026

@Ananya-vastare Did you forget to push a new commit with the changes described in your message?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an initial TemplateValidator intended to statically analyze TemplateMark templates against provided data/model inputs to report missing variables and unused fields (Issue #104), plus a basic Jest test and dependency updates.

Changes:

  • Introduces src/TemplateValidator.ts with a validate() API returning errors/warnings.
  • Adds test/TemplateValidator.test.ts for missing-variable detection.
  • Updates dependencies/lockfile (including adding @accordproject/template-engine).

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 9 comments.

File Description
src/TemplateValidator.ts New validator implementation for missing/unused variable analysis.
test/TemplateValidator.test.ts New unit test covering missing variable detection.
package.json Dependency formatting + adds @accordproject/template-engine.
package-lock.json Lockfile updates reflecting dependency changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread package.json
"@accordproject/concerto-util": "3.25.7",
"@accordproject/markdown-common": "0.17.2",
"@accordproject/markdown-template": "0.17.2",
"@accordproject/template-engine": "^2.8.0",
Comment thread src/TemplateValidator.ts
Comment on lines +1 to +5
export interface ValidationResult {
errors: string[];
warnings: string[];
}

Comment thread src/TemplateValidator.ts
const variableRegex = /{{\s*([a-zA-Z0-9_]+)\s*}}/g;

const foundVariables = new Set<string>();
let match;
Comment thread src/TemplateValidator.ts
Comment on lines +13 to +15
// find variables like {{variable}}
const variableRegex = /{{\s*([a-zA-Z0-9_]+)\s*}}/g;

Comment thread src/TemplateValidator.ts

// check if variables exist in model
foundVariables.forEach(variable => {
if (!(variable in model)) {
Comment thread src/TemplateValidator.ts
Comment on lines +8 to +9
static validate(template: string, model: Record<string, any>): ValidationResult {

Comment on lines +16 to +19
expect(result.errors[0]).toContain("age");

});

@@ -0,0 +1,20 @@
import { TemplateValidator } from '../src/TemplateValidator';

describe('TemplateValidator', () => {

it('should detect missing variables', () => {
@github-actions github-actions Bot added the maintainer-engaged A maintainer has commented or reviewed this item label May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer-engaged A maintainer has commented or reviewed this item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants