Skip to content
Merged
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
7 changes: 6 additions & 1 deletion src/client.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,12 @@ class Client extends Document {
},
params: sanitizeParameters(
Object.assign(
{ script: name, 'script.param': stringify(parameters) },
{
script: name,
'script.param': isJson(parameters)
? stringify(parameters)
: parameters.toString()
},
namespace({ limit: 1 })
)
)
Expand Down
11 changes: 9 additions & 2 deletions src/utilities/conversion.utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ const toArray = data => (Array.isArray(data) ? data : [data]);
*/

const isJson = data => {
data = typeof data !== 'string' ? JSON.stringify(data) : data;

try {
JSON.parse(data);
data = JSON.parse(data);
} catch (e) {
return false;
}
return true;

if (typeof data === 'object' && data !== null) {
return true;
}

return false;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/filemaker.utilities.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const _ = require('lodash');
const { stringify, isJson, parse } = require('./conversion.utilities');
const { stringify, parse } = require('./conversion.utilities');

/**
* @module Filemaker Utilities
Expand Down
30 changes: 27 additions & 3 deletions tests/script.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe before beforeEach it */
/* global describe before after it */

/* eslint-disable */

Expand Down Expand Up @@ -62,6 +62,30 @@ describe('Script Capabilities', () => {
.that.has.all.keys('result');
});

it('should allow you to trigger a script specifying a string as a parameter', () => {
return expect(
client.script(process.env.LAYOUT, 'FMS Triggered Script', 'string-here')
)
.to.eventually.be.a('object')
.that.has.all.keys('result');
});

it('should allow you to trigger a script specifying a number as a parameter', () => {
return expect(client.script(process.env.LAYOUT, 'FMS Triggered Script', 1))
.to.eventually.be.a('object')
.that.has.all.keys('result');
});

it('should allow you to trigger a script specifying an object as a parameter', () => {
return expect(
client.script(process.env.LAYOUT, 'FMS Triggered Script', {
object: true
})
)
.to.eventually.be.a('object')
.that.has.all.keys('result');
});

it('should allow you to trigger a script in a find', () => {
return expect(
client.find(
Expand All @@ -85,7 +109,7 @@ describe('Script Capabilities', () => {
.that.has.all.keys('scriptResult', 'scriptError', 'data');
});

it('should allow reject a script that does not exist', () => {
it('should reject a script that does not exist', () => {
return expect(
client
.script(process.env.LAYOUT, {
Expand Down Expand Up @@ -125,7 +149,7 @@ describe('Script Capabilities', () => {
});

it('should not parse script results if the results are not json', () => {
return expect(client.script(process.env.LAYOUT,'Non JSON Script'))
return expect(client.script(process.env.LAYOUT, 'Non JSON Script'))
.to.eventually.be.a('object')
.that.has.all.keys('result')
.and.property('result')
Expand Down
33 changes: 29 additions & 4 deletions tests/utilities.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe before after it */
/* global describe it */

/* eslint-disable */

Expand All @@ -9,9 +9,11 @@ const { expect, should } = require('chai');

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const environment = require('dotenv');
const varium = require('varium');
const { omit, parse } = require('../src/utilities/conversion.utilities');
const {
omit,
parse,
isJson
} = require('../src/utilities/conversion.utilities');

chai.use(chaiAsPromised);

Expand Down Expand Up @@ -51,4 +53,27 @@ describe('Utility Capabilities', () => {
.and.to.include.keys('name');
});
});
describe('isJson Utility', () => {
it('it should return true for an object', () => {
return expect(isJson({ object: true })).to.equal(true);
});
it('it should return true for an empty object', () => {
return expect(isJson({ object: true })).to.equal(true);
});
it('it should return true for a stringified object', () => {
return expect(isJson({})).to.equal(true);
});
it('it should return false for a number', () => {
return expect(isJson(1)).to.equal(false);
});
it('it should return false for undefined', () => {
return expect(isJson()).to.equal(false);
});
it('it should return false for a string', () => {
return expect(isJson('string')).to.equal(false);
});
it('it should return false for null', () => {
return expect(isJson(null)).to.equal(false);
});
});
});