chevron icon indicating copy to clipboard operation
chevron copied to clipboard

Support error raising on missing variables and partials

Open tzoiker opened this issue 3 years ago • 3 comments

Due to security issues we decided to migrate from jinja to mustache. Jinja supports error raising for missing variables, which is good for data validation purposes. Proposition:

  • Add error flag to render (incompatible with keep, warn);
  • Raise UndefinedError only for variable, no escape, partial.

tzoiker avatar Nov 17 '21 11:11 tzoiker

When will this get merged?

lekhnath avatar Jan 26 '23 06:01 lekhnath

I'm using the following logic to validate if there are missing variables:

rendered_content = chevron.render(template, context)

# Find all variables in the template
variables = re.findall(r'{{\s*([\w.]+)\s*}}', template)

# Check if any variable resulted in an empty string
for var in variables:
    var_rendered = chevron.render("{{" + var + "}}", context)
    if var_rendered == "":
        raise Exception(f"Variable '{var}' is missing from context or resulted in an empty string.")

Cayan avatar Aug 23 '23 18:08 Cayan

@noahmorrison Any indication if this can/will get merged? We are in need of this behavior as well to be able to catch an exception when a there is a missing variables as that indicates there's a bug and we do not want to silently continue

seittema avatar Mar 28 '24 19:03 seittema