Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for required field #132

Open
anamba opened this issue Jun 18, 2019 · 2 comments
Open

Support for required field #132

anamba opened this issue Jun 18, 2019 · 2 comments

Comments

@anamba
Copy link

anamba commented Jun 18, 2019

Currently, in Sablon::Statement::Insertion, when expr evaluates to nil (i.e. there is no entry in the context), it simply removes the field (basically, nil is the same as an empty string).

It would be nice if there were a way to specify that we want to abort with an exception if a value is not provided for a given field. This is important for documents like contracts, where there might be serious consequences if a value is not filled in.

@stadelmanma
Copy link
Collaborator

Hi @anamba, I completely understand your use case but I don't think this is something we will add to the main sablon gem. However, it can be easily done in your application's initiailization/config via a custom field handler and a slightly modified Insertion class.

This comment #108 (comment) has some useful background on either monkey patching Sablon::Statement::Insertion or implementing your own field handler class. See lib/sablon/processor/document/field_handlers.rb and lib/sablon/processor/document.rb for examples on how the "builtin" handlers are done.

Let me know if you have any additional questions.

@stadelmanma
Copy link
Collaborator

Actually, on second thought I think my sleepy mind may have made too hasty a judgement. Sablon aims to keep templates simple because they can be a real pain in the neck to debug. That is why we tend to decline changes that move more logic into the template.

However, "required" fields may add a degree of robustness to a template that help prevent context changes in one part of the code (possibly a quite distant section of code) from breaking the final document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants