This data hook will allow you to apply regex strings to multiple fields for validation purposes, while also allowing you to configure a custom error message in the event the field values are invalid.
const FIELDS = { 'employerId': { 'regex': /^\d{9}$/, 'message': 'Employer Id must be exactly 9 digits' }, 'memberId': { 'regex': /^[a-zA-Z0-9]{5}\d{4}$/, 'message': 'Member ID must be exactly 9 characters' }, 'lastName': { 'regex': /^.{1,20}$/, 'message': 'Last Name must be 20 characters or less' }, 'firstName': { 'regex': /^.{1,20}$/, 'message': 'First Name must be 20 characters or less' }, 'middleInitial': { 'regex': /^[a-zA-Z]{0,1}$/, 'message': 'Middle Initial can only be 1 letter' } } module.exports = async ({recordBatch, session, logger}) => { recordBatch.records.forEach((record) => { Object.keys(FIELDS).forEach(field => { if (FIELDS[field].regex.test(record.get(field)) === false) { record.addError(field, FIELDS[field].message) } }) }) }
This example is formatted for Data Hooks 3.0. For more information about Data Hooks 3.0, click here.
Comments
0 comments
Please sign in to leave a comment.