sunng87 / handlebars-rust

Rust templating with Handlebars
MIT License
1.26k stars 137 forks source link

Add a way for validating template data based on input #641

Closed YouKnow-sys closed 4 months ago

YouKnow-sys commented 4 months ago

Let say we have a template that we always offer a set of default data arguments for. if we want to give user the ability to modify the template with a string in runtime and in same time making sure that user is using arguments that we are giving the template when rendering, we have to make sure that user is using the correct set of arguments inside the template, for that to happen we need to validate the arguments before registering the template and let user know if they are using wrong set of data inside their new template string. currently I don't know anyway for doing this, as I seen handlebars will just use empty string in place of the variable if it don't exist... so it would be nice if we had a solution for this as well (or is there any solution already that I don't know of?)

sunng87 commented 4 months ago

You might want to turn on strict_mode on Handlebars to see if that fits your scenario.

YouKnow-sys commented 4 months ago

You might want to turn on strict_mode on Handlebars to see if that fits your scenario.

oh didn't know about the strict_mode thanks but the thing is the process is still not that straightforward... I mean if I want to know if all needed arguments exist I need to first create the template and then either register it in Registry and render it or render the template directly with things like renders or render... these methods work but the thing is they do more then we need to, I mean we don't need to actually render the template, all we need is to check if the variables that user put in the template actually exist in our data...

sunng87 commented 4 months ago

Oh sorry I missed that point. I think what you want is a schema for the data you are going to render in the template. However, handlebars is a weak typed template system so it doesn't come with a schema. But I think you can do it by your own, just remember to keep the schema consistent with template.

YouKnow-sys commented 4 months ago

hmm ok I'll try it then thanks for the fast answer. Im going to close this issue.