Public
Edited
Nov 19, 2020
Importers
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
results(mytests, mytests2)
Insert cell
table(mytests, mytests2)
Insert cell
badge(mytests, mytests2)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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;

// Status and msg
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"
});
};

// Documentation
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;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more