|
| 1 | +import { getFrontmatter } from './utils' |
| 2 | + |
| 3 | +// AJV validation error object structure |
| 4 | +interface AjvValidationError { |
| 5 | + instancePath: string |
| 6 | + keyword: string |
| 7 | + message: string |
| 8 | + params: { |
| 9 | + additionalProperty?: string |
| 10 | + missingProperty?: string |
| 11 | + [key: string]: unknown |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +// Processed error object for markdown linting |
| 16 | +interface ProcessedValidationError { |
| 17 | + instancePath: string |
| 18 | + detail: string |
| 19 | + context: string |
| 20 | + errorProperty: string |
| 21 | + searchProperty: string |
| 22 | +} |
| 23 | + |
| 24 | +export function formatAjvErrors(errors: AjvValidationError[] = []): ProcessedValidationError[] { |
| 25 | + const processedErrors: ProcessedValidationError[] = [] |
| 26 | + |
| 27 | + errors.forEach((errorObj: AjvValidationError) => { |
| 28 | + const error: Partial<ProcessedValidationError> = {} |
| 29 | + |
| 30 | + error.instancePath = |
| 31 | + errorObj.instancePath === '' |
| 32 | + ? errorObj.instancePath |
| 33 | + : errorObj.instancePath.slice(1).replace('/', '.') |
| 34 | + |
| 35 | + if (errorObj.keyword === 'additionalProperties') { |
| 36 | + error.detail = 'The frontmatter includes an unsupported property.' |
| 37 | + const pathContext = error.instancePath ? ` from \`${error.instancePath}\`` : '' |
| 38 | + error.context = `Remove the property \`${errorObj.params.additionalProperty}\`${pathContext}.` |
| 39 | + error.errorProperty = errorObj.params.additionalProperty |
| 40 | + error.searchProperty = error.errorProperty |
| 41 | + } |
| 42 | + |
| 43 | + // required rule |
| 44 | + if (errorObj.keyword === 'required') { |
| 45 | + error.detail = 'The frontmatter has a missing required property' |
| 46 | + const pathContext = error.instancePath ? ` from \`${error.instancePath}\`` : '' |
| 47 | + error.context = `Add the missing property \`${errorObj.params.missingProperty}\`${pathContext}` |
| 48 | + error.errorProperty = errorObj.params.missingProperty |
| 49 | + error.searchProperty = error.instancePath.split('.').pop() |
| 50 | + } |
| 51 | + |
| 52 | + // all other rules |
| 53 | + if (!error.detail) { |
| 54 | + error.detail = `Frontmatter ${errorObj.message}.` |
| 55 | + error.context = Object.values(errorObj.params).join('') |
| 56 | + error.errorProperty = error.context |
| 57 | + error.searchProperty = error.errorProperty |
| 58 | + } |
| 59 | + |
| 60 | + processedErrors.push(error as ProcessedValidationError) |
| 61 | + }) |
| 62 | + |
| 63 | + return processedErrors |
| 64 | +} |
| 65 | + |
| 66 | +// Alias for backward compatibility |
| 67 | +export const processSchemaValidationErrors = formatAjvErrors |
| 68 | + |
| 69 | +// Schema validator interface - generic due to different schema types (AJV, JSON Schema, etc.) |
| 70 | +interface SchemaValidator { |
| 71 | + validate(data: unknown): boolean |
| 72 | +} |
| 73 | + |
| 74 | +export function getSchemaValidator( |
| 75 | + frontmatterLines: string[], |
| 76 | +): (schema: SchemaValidator) => boolean { |
| 77 | + const frontmatter = getFrontmatter(frontmatterLines) |
| 78 | + return (schema: SchemaValidator) => schema.validate(frontmatter) |
| 79 | +} |
0 commit comments