-
-
Save johnmjackson/47280df7f1ad6202c0c7 to your computer and use it in GitHub Desktop.
Using boolean fields in a Flow XO SDK service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var config = { | |
name: 'Send a Notification', | |
slug: 'send_notification', | |
type: 'action', | |
kind: 'task', | |
scripts: { | |
run: require('./run') | |
}, | |
fields: { | |
input: [{ | |
key: 'message', | |
label: 'Message', | |
type: 'textarea', | |
required: true | |
}, { | |
// To use the boolean field type, simply set the 'type' | |
// to 'boolean'. | |
key: 'notify', | |
label: 'Notify?', | |
type: 'boolean', | |
description: 'Whether this message should trigger a user notification.' | |
}], | |
output: [] | |
} | |
}; | |
module.exports = function(service) { | |
service.registerMethod(config); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function(options, done) { | |
var inputErr = this.validateScriptInput(options.input, { | |
message: { | |
presence: true | |
}, | |
// We use the custom validator 'fxoBoolean' to | |
// make sure that we can parse a boolean from | |
// the input that the user gave. | |
// For example, 'yes', 'no', '1', '0', 'true', 'false' | |
notify: { | |
fxoBoolean: true | |
} | |
}); | |
// If there's any kind of error, return it. | |
if(inputErr) { | |
return done(inputErr); | |
} | |
// Otherwise, we have a valid boolean object, | |
// which looks like this: | |
// | |
// { type: 'boolean', | |
// input: 'yes', | |
// valid: true, | |
// parsed: true } | |
// | |
// So the actual value we need is contained | |
// in the 'parsed' property. This will contain | |
// either true or false. | |
// | |
// Swap the object for the actual value now. | |
if(inputData.notify) { | |
inputData.notify = inputData.notify.parsed; | |
} | |
// Now we can make the actual request, and we | |
// know for sure we've got a bool to send. | |
var opt = { | |
endpoint: '/notification', | |
token: options.credentials.auth_token, | |
method: 'POST', | |
json: options.input | |
}; | |
this.request(opt, done); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment