Template Validation

688 views
Skip to first unread message

lo...@kimbia.com

unread,
Nov 7, 2013, 10:58:46 AM11/7/13
to mustac...@googlegroups.com
We allow user defined templates which occasionally do not compile because typos, user error, the malice of incompetence, etc - what strategies are other people using to validate templates?

Thanks,
Loren

Sam

unread,
Nov 10, 2013, 11:58:32 AM11/10/13
to mustac...@googlegroups.com
Why not just try and compile them? If you wanted to also check that all their references will work at runtime you can use something similar to the TypeCheckingHandler.

Sam

Loren Cole

unread,
Nov 10, 2013, 2:51:35 PM11/10/13
to mustac...@googlegroups.com
We do just try to compile them now and we just catch and handle the exception if it fails.  However, we're using Java and EJB3 and in that environment runtime exceptions (like the MustacheException thrown by Mustache.compile()) inside a transaction cause that transaction to fail and get rolled back.  That in turn causes messages to get reprocessed over and over again by our message queues, and that ends in very bad things. 

It seems to me that pre-validation is the best way to ensure that mustache won't throw unchecked exceptions - this means validating both the codes and the template syntax.

I was sort of hoping someone out there had already cobbled together the regex for validating mustache templates, if I end up writing my own I'll add it to this thread...

Thanks for pointing me at TypeCheckingHandler, I also see there is a DefaultMustacheVisitor which may be a good place for safeguards.

-
Loren




--
You received this message because you are subscribed to a topic in the Google Groups "mustache.java" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mustachejava/_RrodU2qCfQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mustachejava...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

panikumar malepati

unread,
Apr 17, 2018, 1:52:12 PM4/17/18
to mustache.java
Hi Sam, 

       Could you please let us know, if there is a way to do the validation of the template apart of using compile. 
      the compile and execute seem to allow the following:

    case: 1 {{{name}}}} -- renders and returns '}'
    case: 2 {{{name}}}} --  renders and returns successfully "pani"
    case: 3 {{name}}}} --  renders and returns successfully "pani"
    case:4 {{{{name}}} -- render and returns empty..
    case:5  {{{{name}}}} -- render and returns empty..

    We have a requirement to validate the mustache templates. Could you please provide us the document on what is valid by mustache? 
     is there any to check all of them during the compile time?

-Thanks,
Pani

Sam Pullara

unread,
Apr 17, 2018, 3:19:01 PM4/17/18
to mustac...@googlegroups.com
Sadly, all those cases are valid mustache. The compilation step will expose anything that isn't. I think what you want is further semantic validation that isn't based on the whether or not the template is parseable. There are ways to add additional validation but I'm not sure if they would do what you want. For example, here is a type checking handler:


--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam Pullara

unread,
Apr 17, 2018, 3:19:19 PM4/17/18
to mustac...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages