Skip to content

Commit 61a2977

Browse files
committed
Add option for static default value for the transform URL
1 parent b9872a5 commit 61a2977

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ Output:
173173
* *Context*: Node-RED node, or command line with `node ./index.js multi-schema-transformer`
174174
* *Purpose*: Ability to transform a JSON observation on the fly from whichever format to another format (e.g. one of the FIWARE NGSI types) using a specified JSONata URL. Schemas are automatically downloaded and cached the first time they are needed.
175175
* *Input*: A JSON observation in whichever format in the `msg.payload` property, and the corresponding JSONata URL on the `msg.schemaUrl` property (coming from json-multi-schema-resolver).
176-
* If no `msg.schemaUrl` is provided, no transformation is performed.
176+
* Alternatively, a static default value for the schema URL can be set in the corresponding property.
177+
* If no `msg.schemaUrl` is provided, and no value set in the default schema URL property, then no transformation is performed.
177178
* *Output*: The transformed JSON observation in the `msg.payload` property, the used JSONata in the `msg.transformUrl` (if any transformation was performed), and potential validation errors in the `msg.error` property.
178179
* *Implementation*: Based on [JSONata](https://github.com/jsonata-js/jsonata).
179180

@@ -238,6 +239,14 @@ node ./index.js json-multi-schema-transformer | \
238239
jq .
239240
```
240241

242+
Same example, but using a static default value for the schema URL in the corresponding property:
243+
244+
```
245+
echo '{"payload":{"id":"TA120-T246177","type":"Cesva-TA120","NoiseLevelObserved":{"id":"TA120-T246177-NoiseLevelObserved-2018-09-17T07:01:09.000000Z","sonometerClass":"1","location":{"coordinates":[24.985891,60.274286],"type":"Point"},"measurand":["LAeq | 48.6 | A-weighted, equivalent, sound level"],"dateObserved":"2018-09-17T07:01:09.000000Z","LAeq":48.6,"type":"NoiseLevelObserved"}},"error":false}' | \
246+
node ./index.js json-multi-schema-transformer --defaultSchemaUrl='"https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/Cesva-TA120-to-NoiseLevelObserved.jsonata.js"' | \
247+
jq .
248+
```
249+
241250
Output:
242251

243252
```json

json-multi-schema-transformer.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
color: '#00B4FF',
55
defaults: {
66
name: { value: '', },
7+
defaultSchemaUrl: { value: '', },
78
},
89
inputs: 1,
910
outputs: 1,
@@ -19,12 +20,18 @@
1920
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
2021
<input type="text" id="node-input-name" placeholder="Name" />
2122
</div>
23+
24+
<div class="form-row">
25+
<label for="node-input-defaultSchemaUrl"><i class="icon-tag"></i> Default Schema URL</label>
26+
<input type="text" id="node-input-defaultSchemaUrl" placeholder="defaultSchemaUrl" />
27+
</div>
2228
</script>
2329

2430
<script type="text/x-red" data-help-name="json-multi-schema-transformer">
2531
<p>Node to transform a JSON observation to another format using a configurable set of JSONata rules.</p>
2632
<ul>
27-
<li>The node will transform a given JSON payload received in <code>msg.payload</code> using some JSONata rules, which URL is given in <code>msg.schemaUrl</code></li>
33+
<li>The node will transform a given JSON payload received in <code>msg.payload</code> using some JSONata rules, which URL is given in <code>msg.schemaUrl</code>.<br />
34+
Alternatively, a static default value for the schema URL can be set in the corresponding property.</li>
2835
<li>If any transformation is actually performed, the URL of the corresponding JSONata file is returned in <code>msg.transformUrl</code>.</li>
2936
<li>The transformed payload is returned in <code>msg.payload</code>. If no <code>msg.schemaUrl</code> is provided, no transformation is performed.</li>
3037
<li>Errors are returned on <code>msg.error</code></li>

json-multi-schema-transformer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = RED => {
1515
function JsonMultiSchemaTransformerNode(config) {
1616
RED.nodes.createNode(this, config);
1717
const node = this;
18+
const defaultSchemaUrl = config.defaultSchemaUrl;
1819

1920
let lastStatusError = true;
2021
node.status({ fill:'grey', shape:'ring', text:'Uninitialized', });
@@ -62,6 +63,9 @@ module.exports = RED => {
6263

6364
node.on('input', async msg => {
6465
msg.error = msg.error ? msg.error + ' ; ' : '';
66+
if (!msg.schemaUrl) {
67+
msg.schemaUrl = defaultSchemaUrl;
68+
}
6569
if (msg.schemaUrl) {
6670
msg.transformUrl = msg.schemaUrl;
6771
try {

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-json-multi-schema",
3-
"version": "1.0.6",
3+
"version": "1.1.0",
44
"description": "Generic JSON data pipeline tools, with dynamic transformation (using JSONata rules), resolving JSON Schema (using JSONata rules), and then validation (using JSON Schema). For Node-RED and for command-line.",
55
"main": "index.js",
66
"readmeFilename": "readme.md",
@@ -31,13 +31,13 @@
3131
"url": "https://github.com/alexandrainst/node-red-contrib-json-multi-schema.git"
3232
},
3333
"dependencies": {
34-
"ajv": "^6.10.2",
34+
"ajv": "^6.11.0",
3535
"jsonata": "^1.7.0",
3636
"node-fetch": "^2.6.0",
37-
"node-red-contrib-mock-cli": "^1.0.6"
37+
"node-red-contrib-mock-cli": "^1.0.7"
3838
},
3939
"devDependencies": {
40-
"jshint": "^2.10.3"
40+
"jshint": "^2.11.0"
4141
},
4242
"jshintConfig": {
4343
"esversion": 8,

0 commit comments

Comments
 (0)