Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,24 @@ describe('extractLiquidTemplateVariables', () => {
expect(invalidVariables[0].name).to.equal('invalid');
});

it('should handle variables with hyphens in if conditions', () => {
const template = '{% if steps.digest-step.events[0].id %}Hello{% endif %}';
const variableSchema: JSONSchemaDto = {
type: JsonSchemaTypeEnum.OBJECT,
properties: {
steps: {
type: JsonSchemaTypeEnum.OBJECT,
additionalProperties: true,
},
},
};
const { validVariables, invalidVariables } = extractLiquidTemplateVariables({ template, variableSchema });

expect(validVariables).to.have.lengthOf(1);
expect(invalidVariables).to.have.lengthOf(0);
expect(validVariables[0].name).to.equal('steps.digest-step.events[0].id');
});

it('should handle unless statements with invalid condition', () => {
const template = '{% unless user.banned %}Show content{{invalid}}{% endunless %}';
const { validVariables, invalidVariables } = extractLiquidTemplateVariables({ template });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ function extractVariablesFromCondition(condition: string): string[] {
processedCondition = processedCondition.replace(/\|\s*[a-zA-Z_][a-zA-Z0-9_]*(?:\s*:[^|%}]*)?/g, '');

// Now match variable patterns from the processed condition
const variableMatches = processedCondition.match(/[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*(?:\[\d+\])*/g);
const variableMatches = processedCondition.match(/[a-zA-Z_][a-zA-Z0-9_[\].-]+/g);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

More permissive regex, since we later have a validation of extracted variables against schema, its safer approach to rather extract even potentially invalid ones like "steps.events..[abc]" and then disallow them later as opposed to do complicated regex and risk missing extraction of even valid ones.


if (variableMatches) {
variables.push(
Expand Down
Loading