Node field validation
From DreamFactory
Toddappleton (Talk | contribs) (Created page with "This script validates that certain fields are in the POST request when creating records. If not, an exception is thrown and 500 error returned to the client. If the script exi...") |
Toddappleton (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | This script validates that certain fields are in the POST request when creating records. If not, an exception is thrown and 500 error returned to the client. If the script exits normally with no exception being thrown then the records will be written to the database and the post-process script, if any, will be run | + | This script validates that certain fields are in the POST request when creating records. If not, an exception is thrown and 500 error returned to the client. If the script exits normally with no exception being thrown then the records will be written to the database and the post-process script, if any, will be run. |
<source lang="javascript"> | <source lang="javascript"> | ||
+ | // To enable Node.js scripting, set the path to node in your DreamFactory .env file. | ||
+ | // This setting is commented out by default. | ||
+ | // | ||
+ | // DF_NODEJS_PATH=/usr/local/bin/node | ||
+ | // | ||
+ | // Use npm to install any dependencies. This script requires 'lodash.' | ||
+ | // Your scripts can call console.log to dump info to the log file in storage/logs. | ||
+ | |||
// POST /api/v2/db/_table/account triggers script db._table.account.post.pre_process | // POST /api/v2/db/_table/account triggers script db._table.account.post.pre_process | ||
// This script runs BEFORE records are written to the db. | // This script runs BEFORE records are written to the db. | ||
// records are in array event.request.payload.resource. | // records are in array event.request.payload.resource. | ||
− | |||
var lodash = require("lodash"); | var lodash = require("lodash"); |
Revision as of 19:38, 7 March 2016
This script validates that certain fields are in the POST request when creating records. If not, an exception is thrown and 500 error returned to the client. If the script exits normally with no exception being thrown then the records will be written to the database and the post-process script, if any, will be run.
// To enable Node.js scripting, set the path to node in your DreamFactory .env file. // This setting is commented out by default. // // DF_NODEJS_PATH=/usr/local/bin/node // // Use npm to install any dependencies. This script requires 'lodash.' // Your scripts can call console.log to dump info to the log file in storage/logs. // POST /api/v2/db/_table/account triggers script db._table.account.post.pre_process // This script runs BEFORE records are written to the db. // records are in array event.request.payload.resource. var lodash = require("lodash"); if (event.request.payload.resource) { lodash._.each(event.request.payload.resource, function( record ) { if (!record.hasOwnProperty("annual_revenue")) { throw 'missing field annual_revenue'; } if (record.annual_revenue <= 0) { throw 'annual_revenue must be > 0'; } }); }