Reference for require("sanitize") YAML and JSON repair helpers.
Use require("sanitize") to lint, repair, inspect, and strictly parse YAML and JSON strings from JavaScript.
The module exposes two namespaces: sanitize.yaml and sanitize.json. Both namespaces return Go-backed result objects with PascalCase fields such as Sanitized, Fixes, Issues, and StrictParseClean.
const sanitize = require("sanitize");
Returns a Go-backed YAML options builder.
const config = sanitize.yaml.options()
.MaxIterations(5)
.TabWidth(2)
.Build();
Builder methods include MaxIterations(n), TabWidth(n), OnlyRules(...rules), DisabledRules(...rules), RejectUnknownOptions(), AllowUnknownOptions(), CollectUnknownOptions(), FromObject(obj), Validate(), and Build().
Repairs YAML-like input and returns a sanitize result.
const result = sanitize.yaml.sanitize("name:Alice\n");
console.log(result.Sanitized);
console.log(result.Fixes.map((fix) => fix.Rule));
Returns diagnostics without using the sanitized output as the main result.
Returns a parse-tree view for debugging and explanation.
Returns the YAML rule catalog.
Returns example YAML inputs from the underlying sanitize package.
Returns a Go-backed JSON options builder. JSON has no YAML tab-width option.
Repairs JSON-like input, including common LLM wrapper and syntax issues.
const result = sanitize.json.sanitize("~~~json\n{'ok': True,}\n~~~\n");
console.log(result.Sanitized);
console.log(result.StrictParseClean);
Returns JSON diagnostics.
Returns a parse-tree view for debugging.
Parses strict JSON and fails if repair would be required.
Returns the JSON rule catalog.
Returns example JSON inputs from the underlying sanitize package.
Builders reject unknown keys imported through FromObject() by default. Use CollectUnknownOptions() to keep diagnostics instead of failing immediately, or AllowUnknownOptions() only when the script explicitly tolerates ignored fields.