Node field validation

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsNode field validation

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. Use npm to install lodash.

// POST /api/v2/db/_table/account triggers script
// This script runs BEFORE records are written to the db.
// records are in array event.request.payload.resource.
// Your scripts can call console.log to dump info to the log file in storage/logs.
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';