Skip to content

Commit

Permalink
Merge pull request #451 from twittner/issue-449
Browse files Browse the repository at this point in the history
Error if boolean helper params are missing.
  • Loading branch information
sunng87 authored Jul 1, 2021
2 parents 708c773 + e8c107b commit bf0a021
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ macro_rules! handlebars_helper {
fn call_inner<'reg: 'rc, 'rc>(
&self,
h: &$crate::Helper<'reg, 'rc>,
_: &'reg $crate::Handlebars<'reg>,
r: &'reg $crate::Handlebars<'reg>,
_: &'rc $crate::Context,
_: &mut $crate::RenderContext<'reg, 'rc>,
) -> Result<$crate::ScopedJson<'reg, 'rc>, $crate::RenderError> {
let mut param_idx = 0;

$(
let $name = h.param(param_idx)
.map(|x| x.value())
.and_then(|x| {
if r.strict_mode() && x.is_value_missing() {
None
} else {
Some(x.value())
}
})
.ok_or_else(|| $crate::RenderError::new(&format!(
"`{}` helper: Couldn't read parameter {}",
stringify!($struct_name), stringify!($name),
Expand Down
11 changes: 11 additions & 0 deletions tests/subexpression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ fn test_subexpression() {
);
}

#[test]
fn test_strict_mode() {
let mut hbs = Handlebars::new();
hbs.set_strict_mode(true);

let data = json!({"a": 1});

assert!(hbs.render_template("{{#if (eq a 1)}}Success{{else}}Failed{{/if}}", &data).is_ok());
assert!(hbs.render_template("{{#if (eq z 1)}}Success{{else}}Failed{{/if}}", &data).is_err())
}

#[test]
fn invalid_json_path() {
// The data here is not important
Expand Down

0 comments on commit bf0a021

Please sign in to comment.