Skip to content

Commit

Permalink
Ensure booleans are cast correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
joewiz committed May 13, 2023
1 parent 9a43864 commit 8984e3a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions content/templates.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ declare %private function templates:cast($values as item()*, $targetType as xs:s
xs:dateTime($value)
case "xs:time" return
xs:time($value)
case "xs:boolean" return
xs:boolean($value)
case "element()" return
parse-xml($value)/*
case "text()" return
Expand Down
9 changes: 9 additions & 0 deletions test/app/modules/view.xql
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ function test:date($node as node(), $model as map(*), $date as xs:date) {
day-from-date($date)
};

declare
%templates:wrap
function test:boolean($node as node(), $model as map(*), $boolean as xs:boolean) {
if ($boolean instance of xs:boolean) then
"yes"
else
"no"
};

declare function test:custom-model($node as node(), $model as map(*)) {
$model?('my-model-item')
};
Expand Down
1 change: 1 addition & 0 deletions test/app/types-fail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<body>
<p data-template="test:numbers" class="numbers"></p>
<p data-template="test:date" class="date"></p>
<p data-template="test:boolean" class="boolean"></p>
</body>
</html>
1 change: 1 addition & 0 deletions test/app/types.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<body>
<p data-template="test:numbers" class="numbers"></p>
<p data-template="test:date" class="date"></p>
<p data-template="test:boolean" class="boolean"></p>
</body>
</html>
10 changes: 8 additions & 2 deletions test/mocha/rest_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ describe('expand HTML template types.html', function () {
params: {
n1: 20,
n2: 30.25,
date: '2021-02-07+01:00'
date: '2021-02-07+01:00',
boolean: 'true'
}
});
const { window } = new JSDOM(res.data);
Expand All @@ -109,6 +110,10 @@ describe('expand HTML template types.html', function () {
it('converts dates', async function () {
expect(document.querySelector('p.date').innerHTML).to.equal('7');
});

it('converts booleans', async function () {
expect(document.querySelector('p.boolean').innerHTML).to.equal('yes');
});
});

describe('expand HTML template types-fail.html', function () {
Expand All @@ -118,7 +123,8 @@ describe('expand HTML template types-fail.html', function () {
params: {
n1: 'abc',
n2: 30.25,
date: '2021-02-07+01:00'
date: '2021-02-07+01:00',
boolean: 'true'
}
})
.catch(error => {
Expand Down

0 comments on commit 8984e3a

Please sign in to comment.