File size: 8,871 Bytes
c66cada |
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 279 280 281 282 283 284 |
/**
* Canonical Market Data Schema Definition
* Version: 1.0
*
* This schema defines the expected structure for healthcare market research data
* across all system components (OpenClaw Agent, n8n Workflow, WordPress Dashboard)
*/
export const SCHEMA_VERSION = '1.0';
/**
* Schema version history
*/
export const SCHEMA_VERSIONS = [
{
version: '1.0',
releaseDate: '2024-01-20',
changes: ['Initial schema definition with canonical structure'],
backwardCompatible: true
}
];
/**
* Canonical Market Data Schema
* All components must conform to this structure
*/
export const MARKET_DATA_SCHEMA = {
// Schema metadata
schemaVersion: SCHEMA_VERSION,
// Required top-level fields
required: [
'marketTitle',
'executiveOverview',
'pastYear_2023',
'currentYear_2025',
'forecastYear_2033',
'global_cagr_Forecast',
'marketSegments',
'marketDrivers',
'competitiveLandscape'
],
// Field type definitions
fields: {
marketTitle: { type: 'string', required: true },
executiveOverview: { type: 'string', required: true },
pastYear_2023: { type: 'number', required: true },
currentYear_2025: { type: 'number', required: true },
forecastYear_2033: { type: 'number', required: true },
global_cagr_Forecast: { type: 'number', required: true },
marketSegments: {
type: 'array',
required: true,
items: {
segmentCategory: { type: 'string', required: true },
segmentName: { type: 'string', required: true },
segmentName_cagr_Forecast: { type: 'number', required: false },
subSegments: {
type: 'array',
required: true,
items: {
subSegmentName: { type: 'string', required: true },
segment_marketShare_2023: { type: 'number', required: false },
sub_segment_marketShare_2023: { type: 'number', required: false },
segment_marketShare_2025: { type: 'number', required: false },
sub_segment_marketShare_2025: { type: 'number', required: false },
segment_marketShare_2033: { type: 'number', required: false },
sub_segment_marketShare_2033: { type: 'number', required: false },
sub_segmentName_cagr_Forecast: { type: 'number', required: false }
}
}
}
},
marketDrivers: { type: 'array', required: true },
emergingTrends: { type: 'array', required: false },
insights: {
type: 'object',
required: false,
fields: {
largestSegment2025: { type: 'string', required: false },
fastestGrowingSegment: { type: 'string', required: false },
keyOpportunities: { type: 'array', required: false },
majorChallenges: { type: 'array', required: false }
}
},
competitiveLandscape: {
type: 'array',
required: true,
items: {
company: { type: 'string', required: true },
player_marketShare_2025: { type: 'number', required: true },
positioning: { type: 'string', required: false }
}
},
regulatoryEnvironment: { type: 'string', required: false },
geographicAnalysis: { type: 'string', required: false },
futureOutlook: { type: 'string', required: false },
strategicRecommendations: { type: 'array', required: false }
}
};
/**
* Schema Validator Class
* Validates market data against the canonical schema
*/
export class SchemaValidator {
constructor(schema = MARKET_DATA_SCHEMA) {
this.schema = schema;
}
/**
* Validate data against schema
* @param {Object} data - Data to validate
* @returns {Object} - { valid: boolean, errors: string[], warnings: string[] }
*/
validate(data) {
const errors = [];
const warnings = [];
if (!data || typeof data !== 'object') {
errors.push('Data must be an object');
return { valid: false, errors, warnings };
}
// Check required top-level fields
for (const field of this.schema.required) {
if (!(field in data) || data[field] === null || data[field] === undefined) {
errors.push(`Missing required field: ${field}`);
}
}
// Validate field types
this.validateFields(data, this.schema.fields, '', errors, warnings);
// Validate marketSegments structure
if (Array.isArray(data.marketSegments)) {
data.marketSegments.forEach((segment, idx) => {
if (!segment.segmentName) {
errors.push(`marketSegments[${idx}]: missing segmentName`);
}
if (!Array.isArray(segment.subSegments)) {
errors.push(`marketSegments[${idx}]: subSegments must be an array`);
}
});
}
// Validate competitiveLandscape
if (Array.isArray(data.competitiveLandscape)) {
if (data.competitiveLandscape.length < 5) {
warnings.push('competitiveLandscape should include at least 5 companies');
}
data.competitiveLandscape.forEach((company, idx) => {
if (!company.company) {
errors.push(`competitiveLandscape[${idx}]: missing company name`);
}
if (typeof company.player_marketShare_2025 !== 'number') {
errors.push(`competitiveLandscape[${idx}]: player_marketShare_2025 must be a number`);
}
});
}
return {
valid: errors.length === 0,
errors,
warnings
};
}
/**
* Validate individual fields recursively
*/
validateFields(data, fieldDefs, path, errors, warnings) {
for (const [fieldName, fieldDef] of Object.entries(fieldDefs)) {
const fullPath = path ? `${path}.${fieldName}` : fieldName;
const value = data[fieldName];
// Check if required field is missing
if (fieldDef.required && (value === null || value === undefined)) {
errors.push(`Missing required field: ${fullPath}`);
continue;
}
// Skip validation if field is optional and not present
if (!fieldDef.required && (value === null || value === undefined)) {
continue;
}
// Validate type
if (fieldDef.type === 'array') {
if (!Array.isArray(value)) {
errors.push(`${fullPath} must be an array`);
} else if (fieldDef.items && value.length > 0) {
// Validate array items
value.forEach((item, idx) => {
if (typeof fieldDef.items === 'object' && !Array.isArray(fieldDef.items)) {
this.validateFields(item, fieldDef.items, `${fullPath}[${idx}]`, errors, warnings);
}
});
}
} else if (fieldDef.type === 'object') {
if (typeof value !== 'object' || Array.isArray(value)) {
errors.push(`${fullPath} must be an object`);
} else if (fieldDef.fields) {
this.validateFields(value, fieldDef.fields, fullPath, errors, warnings);
}
} else if (fieldDef.type === 'string') {
if (typeof value !== 'string') {
errors.push(`${fullPath} must be a string`);
}
} else if (fieldDef.type === 'number') {
if (typeof value !== 'number' || isNaN(value)) {
errors.push(`${fullPath} must be a number`);
}
} else if (fieldDef.type === 'boolean') {
if (typeof value !== 'boolean') {
errors.push(`${fullPath} must be a boolean`);
}
}
}
}
/**
* Get schema version
*/
getVersion() {
return this.schema.schemaVersion;
}
/**
* Get schema documentation
*/
getDocumentation() {
return {
version: this.schema.schemaVersion,
required: this.schema.required,
fields: this.schema.fields,
versions: SCHEMA_VERSIONS
};
}
}
/**
* Validate market data (convenience function)
* @param {Object} data - Data to validate
* @returns {Object} - Validation result
*/
export function validateMarketData(data) {
const validator = new SchemaValidator();
return validator.validate(data);
}
/**
* Error types for schema validation
*/
export class ValidationError extends Error {
constructor(message, errors = []) {
super(message);
this.name = 'ValidationError';
this.errors = errors;
}
}
export class MissingDataError extends Error {
constructor(message, missingFields = []) {
super(message);
this.name = 'MissingDataError';
this.missingFields = missingFields;
}
}
export class TransformationError extends Error {
constructor(message, details = {}) {
super(message);
this.name = 'TransformationError';
this.details = details;
}
}
|