@adguard/aglint
Version:
Universal adblock filter list linter.
49 lines (46 loc) • 1.84 kB
JavaScript
/*
* AGLint v3.0.0 (build date: Wed, 21 May 2025 13:24:14 GMT)
* (c) 2025 AdGuard Software Ltd.
* Released under the MIT license
* https://github.com/AdguardTeam/AGLint#readme
*/
import { assert, StructError } from 'superstruct';
import { linterConfigSchema } from './config.js';
import { EMPTY } from '../common/constants.js';
/**
* Validates the AGLint linter config object using Superstruct.
*
* @param config Config object to validate
* @throws If the config object is not valid according to the config schema.
*/
function validateLinterConfig(config) {
try {
// Validate the parsed config object against the config schema using Superstruct
assert(config, linterConfigSchema);
}
catch (error) {
if (error instanceof Error) {
// Handle Superstruct errors
if (error instanceof StructError) {
// We can customize the error message here to make it more user-friendly
// https://docs.superstructjs.org/guides/05-handling-errors#customizing-errors
const { key, value, type } = error;
let message = EMPTY;
if (value === undefined) {
message = `"${key}" is required, but it was not provided`;
}
else if (type === 'never') {
message = `"${key}" is unknown in the config schema, please remove it`;
}
else {
message = `Value "${value}" for "${key}" is not a valid "${type}" type`;
}
throw new Error(`Invalid linter config: ${message}`);
}
throw new Error(`Invalid linter config: ${error.message}`);
}
// Pass through any other errors
throw error;
}
}
export { validateLinterConfig };