V8 field validation
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. lodash.min.js is...") |
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. 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. | 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. | ||
− | + | <source> | |
// 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. | ||
Line 18: | Line 18: | ||
}); | }); | ||
} | } | ||
− | + | </source> |
Revision as of 16:51, 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.
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, aimms, algol68, apache, applescript, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, caddcl, cadlisp, cfdg, cfm, chaiscript, chapel, cil, clojure, cmake, cobol, coffeescript, cpp, csharp, css, cuesheet, d, dart, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, ezt, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, ispfpanel, j, java, java5, javascript, jcl, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nginx, nimrod, nsis, oberon2, objc, objeck, ocaml, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, pic16, pike, pixelbender, pli, plsql, postgresql, postscript, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, qml, racket, rails, rbs, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, rust, sas, scala, scheme, scilab, scl, sdlbasic, smalltalk, smarty, spark, sparql, sql, standardml, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vbscript, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xpp, yaml, z80, zxbasic
// 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'; } }); }