Add linting and cleaned up obvious suggestions

This commit is contained in:
Phil Denoncourt
2019-01-18 07:56:53 -05:00
parent ebf32199ce
commit 0002fe0df8
46 changed files with 1161 additions and 601 deletions

View File

@@ -1,18 +1,18 @@
import Joi from 'joi';
import Joi from 'joi'
import eventSchema from '../schema/eventSchema';
import categorySchema from '../schema/categorySchema';
import siteSchema from '../schema/siteSchema';
import narrativeSchema from '../schema/narrativeSchema';
import eventSchema from '../schema/eventSchema'
import categorySchema from '../schema/categorySchema'
import siteSchema from '../schema/siteSchema'
import narrativeSchema from '../schema/narrativeSchema'
import sourceSchema from '../schema/sourceSchema'
import { capitalize } from './helpers.js';
import { capitalize } from './helpers.js'
/*
* Create an error notification object
* Types: ['error', 'warning', 'good', 'neural']
*/
function makeError(type, id, message) {
function makeError (type, id, message) {
return {
type: 'error',
id,
@@ -20,10 +20,8 @@ function makeError(type, id, message) {
}
}
const isLeaf = node => (Object.keys(node.children).length === 0);
const isDuplicate = (node, set) => { return (set.has(node.key)); };
const isLeaf = node => (Object.keys(node.children).length === 0)
const isDuplicate = (node, set) => { return (set.has(node.key)) }
/*
* Traverse a tag tree and check its duplicates
@@ -70,29 +68,29 @@ export function validateDomain (domain) {
categories: [],
sites: [],
narratives: [],
sources: [],
sources: []
}
function validateArrayItem(item, domainKey, schema) {
const result = Joi.validate(item, schema);
function validateArrayItem (item, domainKey, schema) {
const result = Joi.validate(item, schema)
if (result.error !== null) {
const id = item.id || '-';
const domainStr = capitalize(domainKey);
const error = makeError(domainStr, id, result.error.message);
const id = item.id || '-'
const domainStr = capitalize(domainKey)
const error = makeError(domainStr, id, result.error.message)
discardedDomain[domainKey].push(Object.assign(item, { error }));
discardedDomain[domainKey].push(Object.assign(item, { error }))
} else {
sanitizedDomain[domainKey].push(item);
sanitizedDomain[domainKey].push(item)
}
}
function validateArray(items, domainKey, schema) {
function validateArray (items, domainKey, schema) {
items.forEach(item => {
validateArrayItem(item, domainKey, schema)
})
}
function validateObject(obj, domainKey, itemSchema) {
function validateObject (obj, domainKey, itemSchema) {
Object.keys(obj).forEach(key => {
const vl = obj[key]
const result = Joi.validate(vl, itemSchema)
@@ -109,29 +107,28 @@ export function validateDomain (domain) {
})
}
validateArray(domain.events, 'events', eventSchema);
validateArray(domain.categories, 'categories', categorySchema);
validateArray(domain.sites, 'sites', siteSchema);
validateArray(domain.narratives, 'narratives', narrativeSchema);
validateObject(domain.sources, 'sources', sourceSchema);
validateArray(domain.events, 'events', eventSchema)
validateArray(domain.categories, 'categories', categorySchema)
validateArray(domain.sites, 'sites', siteSchema)
validateArray(domain.narratives, 'narratives', narrativeSchema)
validateObject(domain.sources, 'sources', sourceSchema)
// Message the number of failed items in domain
Object.keys(discardedDomain).forEach(disc => {
const len = discardedDomain[disc].length;
const len = discardedDomain[disc].length
if (len) {
sanitizedDomain.notifications.push({
message: `${len} invalid ${disc} not displayed.`,
items: discardedDomain[disc],
type: 'error'
});
})
}
});
})
// Validate uniqueness of tags
const tagSet = new Set([]);
const duplicateTags = [];
validateTree(domain.tags, {}, tagSet, duplicateTags);
const tagSet = new Set([])
const duplicateTags = []
validateTree(domain.tags, {}, tagSet, duplicateTags)
// Duplicated tags
if (duplicateTags.length > 0) {
@@ -139,9 +136,9 @@ export function validateDomain (domain) {
message: `Tags are required to be unique. Ignoring duplicates for now.`,
items: duplicateTags,
type: 'error'
});
})
}
sanitizedDomain.tags = domain.tags;
sanitizedDomain.tags = domain.tags
return sanitizedDomain;
return sanitizedDomain
}