Skip to content

Commit

Permalink
refactor(consumers/validators/creator): Refactor code, add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-tripathi committed Jul 4, 2018
1 parent 7fe1048 commit 40ff2a8
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/consumer/validators/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import {get, validateDate, validatePositiveInteger} from './base';
import {
validateAliases, validateIdentifiers, validateNameSection,
validateSubmissionSection
validateAliases, validateIdentifiers, validateNameSection
} from './common';
import _ from 'lodash';
import type {_IdentifierType} from './types';
import log from '../../helpers/logger';


export function validateCreatorSectionBeginArea(value: any): boolean {
Expand Down Expand Up @@ -76,17 +76,56 @@ export function validateCreatorSection(data: any): boolean {
}

export function validateCreator(
formData: any, identifierTypes?: ?Array<_IdentifierType>
validationObject: any, identifierTypes?: ?Array<_IdentifierType>
): boolean {
const conditions = [
validateAliases(get(formData, 'aliasSection', {})),
validateIdentifiers(
get(formData, 'identifierSection', {}), identifierTypes
),
validateNameSection(get(formData, 'nameSection', {})),
validateCreatorSection(get(formData, 'creatorSection', {})),
validateSubmissionSection(get(formData, 'submissionSection', {}))
];

return _.every(conditions);
let success = true;

const {workerId, ...creatorValidationObject} = validationObject;
if (_.isEmpty(creatorValidationObject)) {
log.warning(`[CONSUMER::${workerId}] CREATOR Incoming validation\
\r object empty`);
return false;
}

// Cumulative error messages to be stored in err string
let err = '';
const aliasSection = get(creatorValidationObject, 'aliasSection', {});
const identifierSection = get(
creatorValidationObject, 'identifierSection', {}
);
const nameSection = get(creatorValidationObject, 'nameSection', {});
const creatorSection = get(
creatorValidationObject,
'creatorSection',
{}
);

log.info(`[CONSUMER::${workerId}]\
\r CREATOR - Calling validation functions.`);

if (!validateAliases(aliasSection)) {
err += 'CREATOR - Validate alias section failed. \n';
success = false;
}

if (!validateIdentifiers(identifierSection, identifierTypes)) {
err += 'CREATOR - Validate identifier section failed. \n';
success = false;
}

if (!validateNameSection(nameSection)) {
err += 'CREATOR - Validate name section failed. \n';
success = false;
}

if (!validateCreatorSection(creatorSection)) {
err += 'CREATOR - Validate creator section failed. \n';
success = false;
}

if (!success) {
log.error(`[CONSUMER::${workerId}]:: ${err} Record for reference:
\r${JSON.stringify(validationObject, null, 4)}`);
}
return success;
}

0 comments on commit 40ff2a8

Please sign in to comment.