Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Typo in Rule K1: convetion --> convention #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 49 additions & 47 deletions rules/k1.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
/* Copyright (c) Looker Data Sciences, Inc. See https://github.com/looker-open-source/look-at-me-sideways/blob/master/LICENSE.txt */

const checkCustomRule = require('../lib/custom-rule/custom-rule.js');
const pkNamingConvention = require('./rules-lib/pk-naming-convention.js');

module.exports = function(
project,
) {
let ruleDef = {
$name: 'K1',
match: `$.model.*.view.*`,
ruleFn,
};
let messages = checkCustomRule(ruleDef, project, {ruleSource: 'internal'});

return {messages};
const checkCustomRule = require("../lib/custom-rule/custom-rule.js");
const pkNamingConvention = require("./rules-lib/pk-naming-convention.js");

module.exports = function (project) {
let ruleDef = {
$name: "K1",
match: `$.model.*.view.*`,
ruleFn,
};
let messages = checkCustomRule(ruleDef, project, { ruleSource: "internal" });

return { messages };
};

function ruleFn(match, path, project) {
let view = match;

const unique = (x, i, arr) => arr.indexOf(x) === i;

/* Skip field-only views */
if (!view.derived_table && !view.sql_table_name) {
return {
level: 'verbose',
description: `Field-only view ${view.$name} is not subject to Primary Key Dimension rule K1`,
};
}

let pkDimensions = (Object.values(view.dimension || {})).filter(pkNamingConvention);

if (!pkDimensions.length) {
return `View ${view.$name} has no dimensions that follow the PK naming convetion`;
}

let declaredNs = pkDimensions
.map(pkNamingConvention)
.map((match) => match[1].replace('pk', ''))
.map((n) => (n===''?'1':n))
.filter(unique);
if (declaredNs.length > 1) {
return `Different PK dimensions in ${view.$name} declare different column counts: ${declaredNs.join(', ')}`;
}

let n = parseInt(declaredNs[0]);
if (n != pkDimensions.length && n !== 0) {
return `View ${view.$name} has ${pkDimensions.length} PK dimension(s) but their names declare ${declaredNs[0]} columns`;
}

return true;
let view = match;

const unique = (x, i, arr) => arr.indexOf(x) === i;

/* Skip field-only views */
if (!view.derived_table && !view.sql_table_name) {
return {
level: "verbose",
description: `Field-only view ${view.$name} is not subject to Primary Key Dimension rule K1`,
};
}

let pkDimensions = Object.values(view.dimension || {}).filter(
pkNamingConvention
);

if (!pkDimensions.length) {
return `View ${view.$name} has no dimensions that follow the PK naming convention`;
}

let declaredNs = pkDimensions
.map(pkNamingConvention)
.map((match) => match[1].replace("pk", ""))
.map((n) => (n === "" ? "1" : n))
.filter(unique);
if (declaredNs.length > 1) {
return `Different PK dimensions in ${
view.$name
} declare different column counts: ${declaredNs.join(", ")}`;
}

let n = parseInt(declaredNs[0]);
if (n != pkDimensions.length && n !== 0) {
return `View ${view.$name} has ${pkDimensions.length} PK dimension(s) but their names declare ${declaredNs[0]} columns`;
}

return true;
}