1
1
mirror of https://github.com/theoludwig/html-w3c-validator.git synced 2024-12-08 00:45:37 +01:00

feat: ability to specify the severity of the validator

fixes #4
This commit is contained in:
Théo LUDWIG 2023-07-02 13:05:33 +02:00
parent a8841b24fd
commit 29203985e4
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
2 changed files with 64 additions and 18 deletions

View File

@ -71,7 +71,10 @@ npm install --save-dev html-w3c-validator start-server-and-test
"urls": ["http://127.0.0.1:3000/", "http://127.0.0.1:3000/about"],
// You can also specify HTML files instead of URLs
"files": ["./index.html", "./about.html"]
"files": ["./index.html", "./about.html"],
// Specify the severities of the validator (default: ["error"])
"severities": ["info", "warning", "error"]
}
```

View File

@ -17,9 +17,14 @@ import { isExistingPath } from './utils/isExistingPath.js'
const CURRENT_DIRECTORY = process.cwd()
const CONFIG_FILE_NAME = '.html-w3c-validatorrc.json'
const severities = ['error', 'warning', 'info'] as const
export type Severity = (typeof severities)[number]
interface Config {
urls?: string[]
files?: string[]
severities?: Severity[]
}
interface Error {
@ -53,7 +58,7 @@ export class HTMLValidatorCommand extends Command {
try {
if (!(await isExistingPath(configPath))) {
throw new Error(
`No config file found at ${configPath}. Please create ${CONFIG_FILE_NAME}.`
`No config file found at ${configPath}. Please create "${CONFIG_FILE_NAME}".`
)
}
const configData = await fs.promises.readFile(configPath, {
@ -66,12 +71,19 @@ export class HTMLValidatorCommand extends Command {
} catch {
isValidConfig = false
}
isValidConfig =
isValidConfig &&
(Array.isArray(config.urls) || Array.isArray(config.urls))
if (!isValidConfig) {
throw new Error(
`Invalid config file at ${configPath}. Please check the syntax.`
`Invalid config file at "${configPath}". Please check the JSON syntax.`
)
}
if (config.urls != null && !Array.isArray(config.urls)) {
throw new Error(
`Invalid config file at "${configPath}". Please include an array of URLs.`
)
}
if (config.files != null && !Array.isArray(config.files)) {
throw new Error(
`Invalid config file at "${configPath}". Please include an array of files.`
)
}
const urls =
@ -87,6 +99,28 @@ export class HTMLValidatorCommand extends Command {
return { type: 'file', data: file }
})
const dataToValidate = [...urls, ...files]
if (dataToValidate.length === 0) {
throw new Error(
`Invalid config file at "${configPath}". Please add URLs or files.`
)
}
const configSeverities: Severity[] = config.severities ?? ['error']
for (const severity of configSeverities) {
if (!severities.includes(severity)) {
throw new Error(
`Invalid config file at "${configPath}". Please add valid severities (${severities.join(
', '
)}).`
)
}
}
if (configSeverities.length === 0) {
throw new Error(
`Invalid config file at "${configPath}". Please add valid severities (${severities.join(
', '
)}).`
)
}
const errors: Error[] = []
let isValid = true
const loader = ora(`Validating HTML (W3C)...`).start()
@ -108,7 +142,7 @@ export class HTMLValidatorCommand extends Command {
const htmlPath = path.resolve(CURRENT_DIRECTORY, data)
if (!(await isExistingPath(htmlPath))) {
throw new Error(
`No file found at ${htmlPath}. Please check the path.`
`No file found at "${htmlPath}". Please check the path.`
)
}
const html = await fs.promises.readFile(htmlPath, {
@ -122,7 +156,10 @@ export class HTMLValidatorCommand extends Command {
throw new Error('Invalid type')
}
const hasErrors = result.messages.some((message) => {
return message.type === 'error'
return (
configSeverities.includes(message.type as Severity) ||
configSeverities.includes(message.subType as Severity)
)
})
if (!hasErrors) {
results.push({ data, isSuccess: true })
@ -130,9 +167,16 @@ export class HTMLValidatorCommand extends Command {
results.push({ data, isSuccess: false })
const messagesTable: string[][] = []
for (const message of result.messages) {
if (message.type === 'error') {
const row: string[] = []
if (message.type === 'info') {
if (message.subType === 'warning') {
row.push(chalk.yellow(message.subType))
} else {
row.push(chalk.blue(message.type))
}
} else {
row.push(chalk.red(message.type))
}
row.push(message.message)
const violation = message as ValidationMessageLocationObject
if (violation.extract != null) {
@ -142,7 +186,6 @@ export class HTMLValidatorCommand extends Command {
}
messagesTable.push(row)
}
}
errors.push({ data, messagesTable })
isValid = false
}