V8 field validation

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsV8 field validation
Line 2: Line 2:
  
 
<source lang="javascript">
 
<source lang="javascript">
 
 
// 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.min.js");
 
var lodash = require("lodash.min.js");
 
 
if (event.request.payload.resource) {
 
if (event.request.payload.resource) {
 
     lodash._.each(event.request.payload.resource, function( record ) {
 
     lodash._.each(event.request.payload.resource, function( record ) {

Revision as of 18:00, 26 October 2015

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. lodash.min.js is available by default. The file is located in storage/scripting directory of the DreamFactory installation. You can add your own scripts there and use require() to include them.

// 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.min.js");
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';
        }
    });
}