Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// merged in the present definition we don't need this
// empty group any more.
delete defClone.properties[propName];
}
// Update property
defClone.properties[propName] = prop;
});
return defClone;
}
/**
* Introduce custom validation function to tv4
*/
tv4.defineKeyword("validator", (
data,
validationFunction //
) =>
// Call the validation function and return:
//
// - null : No error
// - 'string' : The error description
//
validationFunction(data)
);
/**
* The default tv4 error messages are quite bad.
* Here are a bit more user-friendly alternatives
*
* The default language is 'en-gb', so by setting
const tv4 = require('tv4');
tv4.defineError('UNIQUE_KEYS', 10001, '{uniqueKeys} are must be unique value.');
tv4.defineKeyword('uniqueKeys', function(data, value, schema) {
if (!Array.isArray(value)) {
return null;
}
let targets = [];
for (let key of value) {
if (data[key] !== null && data[key] !== undefined) {
targets.push(data[key]);
}
}
let duplicated = targets.filter(function(x, i, self) {
return self.indexOf(x) !== self.lastIndexOf(x);
});
if (duplicated.length > 0) {
return {
code: tv4.errorCodes.UNIQUE_KEYS,
message: { uniqueKeys: value.join(',') },