Custom extension to validate CNPJ and CPF (brazilian tax numbers).
/extensions/server_connect/modules/check_doc.hjson:
[
{
type: 'check_doc',
module : 'check_doc',
action : 'check',
groupTitle : 'Validator',
groupIcon : 'fas fa-check comp-data',
title : 'CheckDoc @@var(name)@@',
icon : 'fas fa-check comp-data',
dataPickObject: true,
usedModules: {
node: {
"cpf-cnpj-validator": "^1.0.3"
}
},
properties : [
{
group: 'Check document',
variables: [
{ name: 'name', optionName: 'name', title: 'Name', type: 'text', required: true, defaultValue: 'query', help: 'This will be the name for the output key and the tag used in the expression builder'},
{ name: 'doc_type', optionName: 'doc_type', title: 'Document type', required: true,
type: 'droplist',
values: [
{title: 'CPF', value: 'cpf' },
{title: 'CNPJ', value: 'cnpj' },
{title: 'Any', value: 'any' }
], defaultValue: 'any',
help: 'Choose the document type to validate.'
},
{ name: 'document', optionName: 'document', title: 'Document number', type: 'text', serverDataBindings: true, required: true, defaultValue: '', help: 'Document number to validate, with or without dots (999.999.999-99, 99.999.999/0001-99).'}
]
},
{
group: 'Output',
variables: [
{ name: 'output', optionName: 'output', title: 'Output', initValue: true, defaultValue: false, type: 'boolean'}
]
}
]
}
]
/extensions/server_connect/modules/check_doc.js:
const { cpf, cnpj } = require('cpf-cnpj-validator');
exports.check = async function (options) {
const type = this.parse(options.doc_type);
const document = this.parse(options.document);
switch (type) {
case 'cnpj':
return cnpj.isValid(document);
case 'cpf':
return cpf.isValid(document);
default:
return cpf.isValid(document) || cnpj.isValid(document);
}
}
PS: Is there a way to give the same threatment as out-of-box Validate action? If “false”, throw an error that is “catched” by the fronted, highlighting and displaying the error in the corresponding field?
Community Page
Last updated:
Last updated: