Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 14x 14x 8x 8x 8x 14x 14x 14x 14x 28x 28x 28x 28x 28x 28x 28x 28x 28x 14x 14x 14x 14x 14x 14x 14x 14x | /**
* Workspace Configuration Validator
*
* Validates workspace configuration using Zod schemas from @quiqr/types.
* Replaces the old Joi-based validator.
*/
import path from 'path';
import { z } from 'zod';
import {
workspaceConfigSchema,
singleConfigSchema,
collectionConfigSchema,
type SingleConfig,
type CollectionConfig,
type WorkspaceConfig,
} from '@quiqr/types';
import { FormatProviderResolver } from '../../utils/format-provider-resolver.js';
// Re-export WorkspaceConfig type for consumers
export type { WorkspaceConfig } from '@quiqr/types';
/**
* Validation utilities for content and data formats
*/
class ValidationUtils {
private formatResolver: FormatProviderResolver;
contentFormatReg: RegExp;
dataFormatReg: RegExp;
allFormatsReg: RegExp;
constructor() {
this.formatResolver = new FormatProviderResolver();
const dataFormatsPiped = this.formatResolver.allFormatsExt().join('|');
this.contentFormatReg = new RegExp('^(md|qmd|mmark)$');
this.dataFormatReg = new RegExp('^(' + dataFormatsPiped + ')$');
this.allFormatsReg = new RegExp('^(' + dataFormatsPiped + '|md|qmd|mmark)$');
}
}
const validationUtils = new ValidationUtils();
/**
* WorkspaceConfigValidator validates workspace configuration files
* Uses Zod schemas for type-safe validation
*/
export class WorkspaceConfigValidator {
/**
* Normalize config by ensuring collections and singles arrays exist
*/
normalizeConfig(config: Partial<WorkspaceConfig>): void {
Eif (config) {
Iif (!config.collections) config.collections = [];
Eif (!config.singles) config.singles = [];
}
}
/**
* Apply config migration from old format to new format
* Handles hugover → ssgType + ssgVersion migration
*/
private applyConfigMigration(config: Record<string, unknown>): Record<string, unknown> {
Iif (!config) return config;
console.log('[CONFIG MIGRATION] Input config:', JSON.stringify({ ssgType: config.ssgType, ssgVersion: config.ssgVersion, hugover: config.hugover }, null, 2));
// If old format with hugover, convert to new format
Eif ('hugover' in config) {
console.log('[CONFIG MIGRATION] Has hugover, converting to new format');
return {
...config,
ssgType: config.ssgType || 'hugo',
ssgVersion: config.hugover,
hugover: undefined // Remove old field
};
}
// If missing both hugover and ssgVersion, add defaults (very old configs)
if (!('ssgVersion' in config) && !('hugover' in config)) {
console.log('[CONFIG MIGRATION] Missing both hugover and ssgVersion, adding defaults');
return {
...config,
ssgType: config.ssgType || 'hugo',
ssgVersion: 'v0.100.2' // Default version for old configs without version info
};
}
// If has ssgVersion but missing ssgType, default to hugo
if ('ssgVersion' in config && !('ssgType' in config)) {
console.log('[CONFIG MIGRATION] Has ssgVersion but no ssgType, defaulting to hugo');
return {
...config,
ssgType: 'hugo'
};
}
console.log('[CONFIG MIGRATION] No migration needed, returning config as-is');
return config;
}
/**
* Validate the entire workspace configuration
* @returns null if valid, error message string if invalid
*/
validate(config: Partial<WorkspaceConfig>): string | null {
this.normalizeConfig(config);
// First apply the preprocessing from workspaceDetailsSchema to handle migrations
// (hugover → ssgVersion, etc.)
const preprocessed = this.applyConfigMigration(config);
// Update the original config object with migrated values
Eif (preprocessed !== config) {
Object.assign(config, preprocessed);
}
try {
// Use the workspaceConfigSchema from @quiqr/types (includes dynamics support)
workspaceConfigSchema.parse(preprocessed);
} catch (err) {
if (err instanceof z.ZodError) {
// TODO
// we need to output clear schema error to the log
// model-developers need this when creating new sites
return String(err);
}
return String(err);
}
// Validate each collection
Eif (config.collections) {
for (const collection of config.collections) {
const error = this.validateCollection(collection);
Iif (error) return error;
}
}
// Validate each single
Eif (config.singles) {
for (const single of config.singles) {
const error = this.validateSingle(single);
if (error) return error;
}
}
return null;
}
/**
* Check that all fields have unique keys
* Accepts unknown[] to accommodate various field schema shapes from Zod
*/
checkFieldsKeys(fields: unknown[] | null | undefined, hintPath: string): void {
Iif (fields == null) return;
const keys: string[] = [];
const error = `${hintPath}: Each field must have an unique key and the key must be a string.`;
for (const field of fields) {
// Runtime check: field must be an object with a key property
Iif (typeof field !== 'object' || field === null) {
throw new Error(error + ' Field must be an object.');
}
const fieldObj = field as Record<string, unknown>;
const key = fieldObj.key;
Iif (key == null) {
throw new Error(error + ` Field without a key is not allowed.`);
}
Iif (typeof key !== 'string') {
throw new Error(error + ' Field key must be a string.');
I} else if (keys.indexOf(key) !== -1) {
throw new Error(error + ` The key "${key}" is duplicated.`);
} else {
keys.push(key);
// Recursively check nested fields if present
const nestedFields = fieldObj.fields;
Iif (Array.isArray(nestedFields)) {
this.checkFieldsKeys(nestedFields, `${hintPath} > Field[key=${key}]`);
}
}
}
}
/**
* Validate a single collection configuration
* @returns null if valid, error message string if invalid
*/
validateCollection(collection: CollectionConfig): string | null {
try {
// Use the Zod schema from @quiqr/types
collectionConfigSchema.parse(collection);
} catch (err) {
if (err instanceof z.ZodError) {
return `Collection validation error: ${err.errors[0].message}`;
}
return String(err);
}
// Additional validation for content files vs data files
if (validationUtils.contentFormatReg.test(collection.extension)) {
// Content files require dataformat
Iif (!collection.dataformat) {
return 'The dataformat value is invalid. Content files require a dataformat.';
}
Iif (!validationUtils.dataFormatReg.test(collection.dataformat)) {
return 'The dataformat value is invalid.';
}
} else E{
// Data files: dataformat should match extension
if (collection.dataformat && collection.dataformat !== collection.extension) {
return 'The dataformat value does not match the extension value.';
}
}
// Check field keys are unique
try {
this.checkFieldsKeys(collection.fields, `Collection[key=${collection.key}]`);
} catch (err) {
return String(err);
}
return null;
}
/**
* Validate a single configuration
* @returns null if valid, error message string if invalid
*/
validateSingle(single: SingleConfig): string | null {
try {
// Use the Zod schema from @quiqr/types
singleConfigSchema.parse(single);
} catch (err) {
if (err instanceof z.ZodError) {
return `Single validation error: ${err.errors[0].message}`;
}
return String(err);
}
// Additional validation only if file is specified
if (single.file) {
const extension = path.extname(single.file).replace('.', '');
// Additional validation for content files
if (single.file.startsWith('content') || single.file.startsWith('quiqr/home')) {
// Content files require dataformat
if (!single.dataformat) {
return 'The dataformat value is invalid. Content files require a dataformat.';
}
if (!validationUtils.dataFormatReg.test(single.dataformat)) {
return 'The dataformat value is invalid.';
}
} else {
// Data files: dataformat should match extension
if (single.dataformat && single.dataformat !== extension) {
return 'The dataformat value does not match the file value: ' + single.dataformat;
}
}
}
// Check field keys are unique
try {
this.checkFieldsKeys(single.fields, `Single[key=${single.key}]`);
} catch (err) {
return String(err);
}
return null;
}
}
/**
* Default singleton instance
*/
export default WorkspaceConfigValidator;
|