Skip to content

Commit

Permalink
feat: ⚡ Implementado trace de log opcional
Browse files Browse the repository at this point in the history
O trace dos logs durante a validação do datalayer só serão ativados quando a variável de ambiente PENGUIN_DEBUGGING estiver com valor igual a true
  • Loading branch information
joaquimsn committed Mar 9, 2021
1 parent b0d44b4 commit cba1158
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
23 changes: 17 additions & 6 deletions datalayer-validation-core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const schemaParser = require('./schema-parser');
const Ajv = require('ajv');
process.env.PENGUIN_DEBUGGING = process.env.PENGUIN_DEBUGGING || false;
const debugging = process.env.PENGUIN_DEBUGGING || false;
let fullValidation = [];

const ajv = new Ajv({
Expand All @@ -19,7 +19,7 @@ const ajv = new Ajv({
* @param {*} keyName
*/
function validationResult(status, message, dlObject, objectName, keyName) {
console.log(`${status}, ${message}, ${dlObject}`);
trace(`${status}, ${message}, ${dlObject}, ${objectName}, ${keyName}`);
fullValidation.push({
status: status,
message: message,
Expand All @@ -36,8 +36,8 @@ function validationResult(status, message, dlObject, objectName, keyName) {
*/
function checkValidEvent(schemaItem, dataLayer) {
for (let index = 0; index < schemaItem.length; index++) {
//console.log("schema: "+JSON.stringify(schemaItem[index],null,2));
//console.log("datalayer: "+JSON.stringify(dataLayer,null,2));
trace('schema: ' + JSON.stringify(schemaItem[index], null, 2));
trace('datalayer: ' + JSON.stringify(dataLayer, null, 2));
let valid = ajv.validate(schemaItem[index], dataLayer);
if (valid) {
validationResult('OK', 'Validated Successfully', JSON.stringify(dataLayer, null, 2));
Expand Down Expand Up @@ -174,7 +174,7 @@ function checkMissingProperty(schemaItem, dataLayer) {
let valid = ajv.validate(item, dataLayer);
let errors = ajv.errors;

//console.log("retorno ajv", errors)
trace(`retorno ajv ${JSON.stringify(err)}"`);
if (!valid) {
errors
.filter((error) => error.schema.constructor === Object && error.keyword === 'required')
Expand Down Expand Up @@ -294,12 +294,23 @@ let validate = (schema, dataLayer, callback) => {
return fullValidation;
};

/**
* Enviado o log para o stdout, se somente se, a variável debugging = true
* @param {Object} log Que será apresentado no stdout
*/
function trace(log) {
if (debugging) {
console.log(log);
}
}

module.exports = {
validate,
validationResult,
checkMissingEvents,
checkErrorsPerSchema,
checkValidEvent,
checkMissingProperty,
revalidateSchema
revalidateSchema,
trace,
};
5 changes: 2 additions & 3 deletions schema-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ function parseEvent(event) {
* @param {*} items
*/
function parseToDataLayer(items) {
let dl = items.map((event) => parseEvent(event));
return dl;
return items.map((event) => parseEvent(event));
}

module.exports = {
parseToDataLayer,
parseEvent
parseEvent,
};
6 changes: 6 additions & 0 deletions test/unit/datalayer-validation-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ describe('datalayer-validation-core', () => {
assert.isFunction(core.revalidateSchema);
});
});

describe('#trace()', () => {
it('Deve ser uma function', () => {
assert.isFunction(core.trace);
});
});
});

0 comments on commit cba1158

Please sign in to comment.