@chartguard/runtime-chartjs
v0.1.1
Published
Optional opt-in runtime validation wrapper for chartjs
Readme
@chartguard/runtime-chartjs
Checks a Chart.js config when the chart is created and every time it is updated, through Chart.js's own plugin hooks. Opt-in per chart: nothing is registered globally and Chart.js is not patched.
Status: pre-release (0.x). Coverage is partial, and the API and settings may still change between minor versions.
npm install @chartguard/runtime-chartjsimport { Chart } from "chart.js";
import { guard } from "@chartguard/runtime-chartjs";
const chart = new Chart(canvas, guard(config));
chart.data.datasets[0].borderWidth = settings.width; // "wide", read from a settings file
chart.update(); // warns: $.data.datasets[0].borderWidth (Expected number, got string.)- Creation: the config is checked as it was passed to
guard(Chart.js writes default scales into it afterwards), against the runningChart.versionunless you name one. - Updates: the chart's own
type,dataandoptionsare checked before eachupdate(), exceptoptions.scales, which Chart.js merges with its defaults: while they are still the scales the chart was first drawn with, what creation found on them stands; once they change, the result notes that they are not checked (CFG_UNCHECKED) and ispartial. One guarded config may build several charts; each is tracked on its own. - What is registered: each check reads what
Chart.registryholds at that moment, plus the inline plugins of the config (plugins: [Legend]). On a tree-shaken Chart.js (Chart.register(...)instead ofchart.js/auto), a controller or scale that is not registered is a confirmedCFG_UNREGISTEREDerror (Chart.js would throw), and an option of a plugin that is neither registered nor inline isCFG_INEFFECTIVE.registriesOf(Chart, config.plugins)gives the same context for your own validation. - Reporting:
onResult(result, { phase, mode })receives every result. Without it, a result with confirmed errors is written withconsole.warn; nothing else is logged, except a config that turns the guard off (Known limits). - Blocking:
guard(config, { block: true })does not draw an invalid config: an invalid creation is not drawn at any render, the first one included, and an invalid update is cancelled; Chart.js keeps what it drew before. Nothing is thrown inside Chart.js. validatorreplaces the shipped schema (for examplewithExtensions([...])from@chartguard/config-chartjs),optionspassesregistriesorlibraryVersion,updates: falsechecks creation only.
guard never changes the config you pass and never runs its getters. To validate before construction and throw instead, use validateBeforeCreate(config, (checked) => new Chart(canvas, checked)).
Known limits
- Turning the guard off: with
options.plugins: false(oroptions.plugins.chartguard: false) Chart.js calls none of the guard's hooks, so the chart is neither checked nor blocked.guardwarns about it withconsole.warn, and withblock: truerefuses the config with aTypeError. Whenoptionsoroptions.pluginsis behind a getter, the guard does not run it and cannot tell. - Inline plugins:
guardadds its plugin toconfig.plugins. Whenconfig.pluginsis an accessor or not an array, it refuses the config with aTypeErrorinstead of running the getter or dropping your plugins. An inline plugin behind a getter is kept and not read, so the plugin registry is incomplete: options of unregistered plugins are then not claimed ineffective. - Reporter errors: an
onResultthat throws never throws inside Chart.js (the chart is created and drawn as without it); its error is rethrown in a microtask, where the host reports it as uncaught. - Version: the running version is
versionof the chart's class, or of the class it extends (class MyChart extends Chart), read as a data property; when there is none, the config is checked as the schema's version.
