(projectId, string, customRegex)
| 20 | // usage: node inspectWithCustomRegex.js my-project string customRegex |
| 21 | |
| 22 | function main(projectId, string, customRegex) { |
| 23 | // [START dlp_inspect_custom_regex] |
| 24 | // Imports the Google Cloud Data Loss Prevention library |
| 25 | const DLP = require('@google-cloud/dlp'); |
| 26 | |
| 27 | // Instantiates a client |
| 28 | const dlp = new DLP.DlpServiceClient(); |
| 29 | |
| 30 | // The project ID to run the API call under |
| 31 | // const projectId = 'my-project'; |
| 32 | |
| 33 | // The string to inspect |
| 34 | // const string = 'Patients MRN 444-5-22222'; |
| 35 | |
| 36 | // The regex pattern to match for |
| 37 | // const customRegex = '[1-9]{3}-[1-9]{1}-[1-9]{5}'; |
| 38 | |
| 39 | async function inspectWithCustomRegex() { |
| 40 | // Construct item to inspect |
| 41 | const item = { |
| 42 | byteItem: { |
| 43 | type: DLP.protos.google.privacy.dlp.v2.ByteContentItem.BytesType |
| 44 | .TEXT_UTF8, |
| 45 | data: Buffer.from(string, 'utf-8'), |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | // Construct the custom regex detector. |
| 50 | const customInfoTypes = [ |
| 51 | { |
| 52 | infoType: { |
| 53 | name: 'C_MRN', |
| 54 | }, |
| 55 | likelihood: DLP.protos.google.privacy.dlp.v2.Likelihood.POSSIBLE, |
| 56 | regex: { |
| 57 | pattern: customRegex, |
| 58 | }, |
| 59 | }, |
| 60 | ]; |
| 61 | |
| 62 | // Construct request |
| 63 | const request = { |
| 64 | parent: `projects/${projectId}/locations/global`, |
| 65 | inspectConfig: { |
| 66 | customInfoTypes: customInfoTypes, |
| 67 | includeQuote: true, |
| 68 | }, |
| 69 | item: item, |
| 70 | }; |
| 71 | |
| 72 | // Run request |
| 73 | const [response] = await dlp.inspectContent(request); |
| 74 | const findings = response.result.findings; |
| 75 | if (findings.length > 0) { |
| 76 | console.log('Findings: \n'); |
| 77 | findings.forEach(finding => { |
| 78 | console.log(`InfoType: ${finding.infoType.name}`); |
| 79 | console.log(`\tQuote: ${finding.quote}`); |
no test coverage detected