UNPKG

zon-format

Version:

ZON: The most token-efficient serialization format for LLMs - beats CSV, TOON, JSON, and all competitors

153 lines (152 loc) 4.79 kB
"use strict"; /** * Enhanced Validator & Linter * * Validate ZON data and provide best practice recommendations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.validator = exports.ZonValidator = void 0; exports.validateZon = validateZon; exports.lintZon = lintZon; const decoder_1 = require("../core/decoder"); const encoder_1 = require("../core/encoder"); const helpers_1 = require("./helpers"); /** * Enhanced validator with linting */ class ZonValidator { /** * Validate ZON string and provide detailed feedback */ validate(zon, options = {}) { const errors = []; const warnings = []; const suggestions = []; let data; try { data = (0, decoder_1.decode)(zon); } catch (error) { return { valid: false, errors: [{ path: 'root', message: error.message, severity: 'error' }], warnings: [], suggestions: ['Check ZON syntax for errors'] }; } const stats = (0, helpers_1.analyze)(data); if (options.maxDepth && stats.depth > options.maxDepth) { warnings.push({ path: 'root', message: `Nesting depth (${stats.depth}) exceeds recommended maximum (${options.maxDepth})`, severity: 'warning', rule: 'max-depth' }); suggestions.push('Consider flattening deeply nested structures'); } if (options.maxFields && stats.fieldCount > options.maxFields) { warnings.push({ path: 'root', message: `Field count (${stats.fieldCount}) exceeds recommended maximum (${options.maxFields})`, severity: 'warning', rule: 'max-fields' }); suggestions.push('Consider breaking large objects into smaller ones'); } if (options.checkPerformance) { if (stats.arrayCount > 100) { warnings.push({ path: 'root', message: `Large number of arrays (${stats.arrayCount}) may impact performance`, severity: 'warning', rule: 'performance-arrays' }); suggestions.push('Consider using binary format for better performance'); } if (stats.objectCount > 500) { warnings.push({ path: 'root', message: `Large number of objects (${stats.objectCount}) may impact performance`, severity: 'warning', rule: 'performance-objects' }); } } if (options.schema) { try { const result = options.schema.parse(data); if (!result.success) { result.issues.forEach(issue => { errors.push({ path: issue.path.join('.'), message: issue.message, severity: 'error' }); }); } } catch (error) { errors.push({ path: 'root', message: `Schema validation failed: ${error.message}`, severity: 'error' }); } } try { const roundtrip = (0, encoder_1.encode)((0, decoder_1.decode)(zon)); if (roundtrip !== zon.trim()) { suggestions.push('ZON format may not be optimal - consider reformatting'); } } catch (_a) { } return { valid: errors.length === 0, errors, warnings, suggestions }; } /** * Quick validation without detailed linting */ isValid(zon) { try { (0, decoder_1.decode)(zon); return true; } catch (_a) { return false; } } /** * Get best practice recommendations */ lint(zon, options = {}) { const result = this.validate(zon, { maxDepth: 10, maxFields: 100, checkPerformance: true, ...options }); return result.warnings; } } exports.ZonValidator = ZonValidator; /** * Global validator instance */ exports.validator = new ZonValidator(); /** * Quick validation function */ function validateZon(zon, schema) { return exports.validator.validate(zon, { schema }); } /** * Lint ZON for best practices */ function lintZon(zon, options) { return exports.validator.lint(zon, options); }