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

Add check for relative nodeset + ref attributes in the body #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**
3 changes: 2 additions & 1 deletion src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ const validate = async( xformStr, options = {} ) => {

return Promise.resolve( { warnings, errors, version, duration } );
}

xform.checkStructure( warnings, errors );
xform.checkBinds( warnings, errors );
xform.checkRelatives( warnings, errors );
xform.checkAppearances( warnings, errors );

if ( options.openclinica ) {
Expand Down
21 changes: 21 additions & 0 deletions src/xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,27 @@ class XForm {
.forEach( nodeName => errors.push( `Question "${nodeName}" has a calculation that is not set to readonly.` ) );
}

/**
* NEW! Check to see if there are any relative paths in refs or nodesets. Modifies provided `warnings` and `errors` arrays.
*
* @param {Array} warnings - Array of existing warnings.
* @param {Array} errors - Array of existing errors.
*/

checkRelatives( warnings, errors ) {
this.formControls.concat( this.groups ).concat( this.repeats )
.forEach( control => {
const controlNsPrefix = this.nsPrefixResolver( control.namespaceURI );
const controlName = controlNsPrefix && /:/.test( control.nodeName ) ? controlNsPrefix + ':' + control.nodeName.split( ':' )[ 1 ] : control.nodeName;
const pathAttr = controlName === 'repeat' ? 'nodeset' : 'ref';

const val = control.getAttribute(pathAttr);
if (val.startsWith('.')) {
errors.push( `"${controlName}" found with relative path.`)
}
})
}

/**
* Checks if appearances are valid. Modifies provided `warnings` and `errors` arrays.
*
Expand Down
71 changes: 71 additions & 0 deletions test/xform/relative-refs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<h:html xmlns="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:jr="http://openrosa.org/javarosa"
xmlns:orx="http://openrosa.org/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>Nested Repeats</h:title>
<model>
<instance>
<data id="data">
<kids>
<has-kids>1</has-kids>
<kids-details jr:template="">
<kids-name/>
</kids-details>
<group>
<nested-age/>
</group>
</kids>
<gps/>
<something>
<repeat jr:template="">
<a/>
</repeat>
</something>
<orx:meta>
<orx:instanceID/>
</orx:meta>
</data>
</instance>
<bind nodeset="/data/kids/has-kids" type="select1"/>
<bind nodeset="/data/kids/kids-details" relevant="/data/kids/has-kids='1'"/>
<bind nodeset="/data/kids/kids-details/kids-name" type="string"/>
<bind nodeset="/data/kids/group/nested-age" type="int"/>
</model>
</h:head>
<h:body>
<group ref="/data/kids">
<label>Kids</label>
<select1 ref="./has-kids">
<label>4. Do you have any kids?</label>
<item>
<label>yes</label>
<value>1</value>
</item>
</select1>
<group ref="kids-details">
<label>4.1 Kids Details</label>
<repeat nodeset=".">
<input ref="kids-name">
<label>4.1.1 Child's Name</label>
</input>
</repeat>
</group>
<group ref="./group">
<label>4.2 A Nested Group</label>
<input ref="nested-age">
<label>4.4 A Nested Age</label>
</input>
</group>
</group>
<group ref="/data/something">
<repeat nodeset="repeat">
<input ref="./a">
<label>a</label>
</input>
</repeat>
</group>
</h:body>
</h:html>