Python Field validation example

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsPython Field validation example
m (Added syntax highlighting)
Line 6: Line 6:
 
# 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.
 
+
payload = event['request']['payload'];
+
payload = event.request.payload;
 
+
if 'annual_revenue' not in payload:
+
if 'annual_revenue' not in payload.resource:
 
     raise ValueError('Missing field annual revenue');
 
     raise ValueError('Missing field annual revenue');
if payload['annual_revenue'] <= 0:
+
if payload.resource.annual_revenue <= 0:
 
     raise ValueError('Annual Revenue must be > 0');
 
     raise ValueError('Annual Revenue must be > 0');
 
</source>
 
</source>

Revision as of 05:17, 30 May 2016

This script validates that certain fields are in the POST request when creating records. If not, an exception is raised and 500 error returned to the client. If the script exists 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.

# POST /api/v2/db/_table/account triggers script db._table.account.post.pre_process
# This script runs BEFORE records are written to the db.
 
payload = event.request.payload;
 
if 'annual_revenue' not in payload.resource:
    raise ValueError('Missing field annual revenue');
if payload.resource.annual_revenue <= 0:
    raise ValueError('Annual Revenue must be > 0');