guigrpa / docx-templates

Template-based docx report creation
MIT License
882 stars 145 forks source link

Template validation #335

Open daviatorstorm opened 10 months ago

daviatorstorm commented 10 months ago

Is it possible to make template validation before creating actual report?

jjhbw commented 10 months ago

The commands in the template are arbitrary JavaScript, so you cannot really validate them without executing the entire template. At least i havent found a convenient way to do so.

To validate user-created templates in my desktop app I just execute them and present any errors that occur to the user. Note that you should be aware of the security concerns related to untrusted/user-generated templates, but that's a different topic.

daviatorstorm commented 10 months ago

Maybe there is a way check template syntax? I mean to check if commands

+++ QUERI { data { title, description }} +++
    ^^^^^
Error: Syntax error near 1:1. Unknown identifier "QUERI"

are written in the right way but skip values accessing

jjhbw commented 10 months ago

You can use listCommands to get a list of all the commands, but you'll have to parse and validate them using your own custom validation routine.

But still, the only 100% sure way to validate template correctness is by running the code in the template commands, that's the downside of allowing JavaScript in the templates.