allowed_values = {
let f = function(data, variable, config = {}) {
let {
level = 'warn',
id = 'test_allowedvalues' +
string_hash(variable) +
string_hash(config.values.toString()),
values = null,
expected = 0
} = config;
check_column_existence(data, variable);
if (!Array.isArray(values)) values = [values];
const variable_values = [...new Set(data.map(d => d[variable]))];
let not_allowed_values = new Set();
variable_values.forEach(v => {
if (!values.includes(v)) not_allowed_values.add(v);
});
not_allowed_values = [...not_allowed_values];
const nb_not_allowed = not_allowed_values.length;
const status = nb_not_allowed == expected;
let msg = `Values not allowed found in <code>${variable}</code> : <strong>${nb_not_allowed}</strong> `;
if (!status) msg += ` (expected : ${expected})`;
const details = not_allowed_values
.map(v => {
if (typeof v == "string") return `"${v}"`;
if (v === undefined) return "undefined";
if (v === null) return "null";
return v;
})
.map(v => `<code>${v}</code>`)
.join(", ");
return new Test({
msg: msg,
status: status,
level: level,
id: id,
show_details: nb_not_allowed > 0,
details: html`<div>${details}</div>`,
details_summary: "Show values"
});
};
f.doc = {
title:
"Check if all value(s) in a column are included in a set of allowed values",
category: "Data values tests",
description:
"`tests.allowed_values` is applied to a column of parsed data and checks if all its values are in a list of allowed value(s)",
args: [
{ name: 'data', desc: 'parsed data object' },
{ name: 'variable', desc: 'column name' },
{
name: 'config.values',
desc: 'an array of allowed values',
default: ''
},
{
name: 'config.expected',
desc: 'expected number of not allowed values',
default: 0
}
]
};
return f;
}