Skip to content

Commit

Permalink
Merge pull request #280 from feup-infolab/fix_#278
Browse files Browse the repository at this point in the history
Fix #278
  • Loading branch information
NelsonPereira1991 committed Nov 10, 2017
2 parents 215b339 + 987effb commit 3df2df2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/models/meta/descriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,15 @@ Descriptor.mergeDescriptors = function (descriptorsArray, callback)
{
if (descriptor.value instanceof Array)
{
newDescriptors[descriptor.prefix][descriptor.shortName] = [descriptor.value].push(newDescriptors[descriptor.prefix][descriptor.shortName]);
let newValues = newDescriptors[descriptor.prefix][descriptor.shortName];
if (newValues instanceof Array)
{
newDescriptors[descriptor.prefix][descriptor.shortName] = _.union(descriptor.value, newValues);
}
else
{
newDescriptors[descriptor.prefix][descriptor.shortName] = _.union(descriptor.value, [newValues]);
}
}
else if (!isNull(descriptor.value))
{
Expand Down
52 changes: 38 additions & 14 deletions src/models/meta/elements.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const path = require("path");
const _ = require("underscore");
const async = require("async");
const validUrl = require("valid-url");
const Pathfinder = global.Pathfinder;
const Controls = require(Pathfinder.absPathInSrcFolder("/models/meta/controls.js")).Controls;
const isNull = require(Pathfinder.absPathInSrcFolder("/utils/null.js")).isNull;

function Elements ()
{}
Expand Down Expand Up @@ -46,7 +48,7 @@ Elements.checkIfValidPrefixedResource = function (candidatePrefixedResource)
Elements.getInvalidTypeErrorMessageForDescriptor = function (currentDescriptor)
{
let errorMessagesForTypes = {};
const msgStart = "Error: The value type for the descriptor: " + currentDescriptor.prefixedForm + " should be ";
const msgStart = "Error: The value type for the descriptor " + currentDescriptor.prefix + ":" + "(" + currentDescriptor.label + ")" + " should be ";
errorMessagesForTypes[Elements.types.resourceNoEscape] = msgStart + "an 'URI'";
errorMessagesForTypes[Elements.types.resource] = msgStart + "an 'URI'";
errorMessagesForTypes[Elements.types.property] = msgStart + "an 'URI'";
Expand All @@ -64,20 +66,42 @@ Elements.getInvalidTypeErrorMessageForDescriptor = function (currentDescriptor)

Elements.validateDescriptorValueTypes = function (currentDescriptor)
{
let typesValidators = {};
typesValidators[Elements.types.resourceNoEscape] = ((typeof currentDescriptor.value === "string" || currentDescriptor.value instanceof String) && validUrl.is_uri(currentDescriptor.value));
typesValidators[Elements.types.resource] = ((typeof currentDescriptor.value === "string" || currentDescriptor.value instanceof String) && validUrl.is_uri(currentDescriptor.value));
typesValidators[Elements.types.property] = ((typeof currentDescriptor.value === "string" || currentDescriptor.value instanceof String) && validUrl.is_uri(currentDescriptor.value));
typesValidators[Elements.types.string] = (typeof currentDescriptor.value === "string" || currentDescriptor.value instanceof String);
typesValidators[Elements.types.int] = Number.isInteger(currentDescriptor.value);
typesValidators[Elements.types.double] = !isNaN(currentDescriptor.value);
typesValidators[Elements.types.boolean] = (currentDescriptor.value === "true" || currentDescriptor.value === "false" || currentDescriptor.value === true || currentDescriptor.value === false);
typesValidators[Elements.types.prefixedResource] = Elements.checkIfValidPrefixedResource(currentDescriptor.value);
typesValidators[Elements.types.date] = !isNaN(Date.parse(currentDescriptor.value));
typesValidators[Elements.types.long_string] = (typeof currentDescriptor.value === "string" || currentDescriptor.value instanceof String);
typesValidators[Elements.types.stringNoEscape] = (typeof currentDescriptor.value === "string" || currentDescriptor.value instanceof String);
const validateADescriptorValueAgainstItsType = function (descriptorType, descriptorValue)
{
let typesValidators = {};
typesValidators[Elements.types.resourceNoEscape] = ((typeof descriptorValue === "string" || descriptorValue instanceof String) && validUrl.is_uri(descriptorValue));
typesValidators[Elements.types.resource] = ((typeof descriptorValue === "string" || descriptorValue instanceof String) && validUrl.is_uri(descriptorValue));
typesValidators[Elements.types.property] = ((typeof descriptorValue === "string" || descriptorValue instanceof String) && validUrl.is_uri(descriptorValue));
typesValidators[Elements.types.string] = (typeof descriptorValue === "string" || descriptorValue instanceof String);
typesValidators[Elements.types.int] = Number.isInteger(descriptorValue);
typesValidators[Elements.types.double] = !isNaN(descriptorValue);
typesValidators[Elements.types.boolean] = (descriptorValue === "true" || descriptorValue === "false" || descriptorValue === true || descriptorValue === false);
typesValidators[Elements.types.prefixedResource] = Elements.checkIfValidPrefixedResource(descriptorValue);
typesValidators[Elements.types.date] = !isNaN(Date.parse(descriptorValue));
typesValidators[Elements.types.long_string] = (typeof descriptorValue === "string" || descriptorValue instanceof String);
typesValidators[Elements.types.stringNoEscape] = (typeof descriptorValue === "string" || descriptorValue instanceof String);

return typesValidators[descriptorType];
};

return typesValidators[currentDescriptor.type];
// When there are various instances of a descriptor, for example: two dcterms:contributor
if (currentDescriptor.value instanceof Array)
{
for (let i = 0; i !== currentDescriptor.value.length; i++)
{
let resultOfValidation = validateADescriptorValueAgainstItsType(currentDescriptor.type, currentDescriptor.value[i]);
if (resultOfValidation === false)
{
return false;
}
}
}
else
{
// When there is only one instance of a descriptor (for example only one dcterms:abstract)
return validateADescriptorValueAgainstItsType(currentDescriptor.type, currentDescriptor.value);
}
return true;
};

/**
Expand Down

0 comments on commit 3df2df2

Please sign in to comment.