/** * Create pre-insertion validator function * * **Poka-Yoke**: Returns a function that validates triples before insertion. * Use this to guard store.addTriples() calls. * * @param {RDFValidator} validator - Validator instance * @param {Object} [options] - Options * @param {boolean} [options.throwOnError=true] - Throw on validation error * @param {string[]} [options.shapes] - Shapes to validate against * @returns {(triples: Triple[]) => Promise<{valid: boolean, errors: ValidationError[], triples: Triple[]}>} */ export function createPreInsertionValidator(validator: RDFValidator, options?: { throwOnError?: boolean; shapes?: string[]; }): (triples: Triple[]) => Promise<{ valid: boolean; errors: ValidationError[]; triples: Triple[]; }>; /** * Common RDF namespace prefixes * @type {Object} */ export const NAMESPACES: { [x: string]: string; }; /** * Validation error structure * @typedef {Object} ValidationError * @property {string} type - Error type (e.g., 'IRI_INVALID', 'DATATYPE_MISMATCH') * @property {string} message - Human-readable error message * @property {Object} [context] - Additional context (subject, property, value) * @property {'error' | 'warning'} severity - Error severity */ /** * Shape rule definition * @typedef {Object} ShapeRule * @property {string} property - Property IRI or prefixed name * @property {boolean} [required=false] - Whether property is required * @property {string} [datatype] - Expected datatype (prefixed or full IRI) * @property {number} [minValue] - Minimum numeric value (for numeric datatypes) * @property {number} [maxValue] - Maximum numeric value (for numeric datatypes) * @property {number} [minLength] - Minimum string length * @property {number} [maxLength] - Maximum string length * @property {RegExp} [pattern] - Regex pattern for string values * @property {number} [minCount=0] - Minimum occurrences * @property {number} [maxCount] - Maximum occurrences (undefined = unlimited) * @property {string[]} [in] - Allowed values (closed list) * @property {string} [nodeKind] - Expected node kind ('IRI', 'Literal', 'BlankNode') */ /** * Shape definition * @typedef {Object} ShapeDefinition * @property {string} shape - Shape name/IRI * @property {string} [targetClass] - Target class for this shape * @property {ShapeRule[]} rules - Validation rules */ /** * Triple structure * @typedef {Object} Triple * @property {string} subject - Subject IRI or blank node * @property {string} predicate - Predicate IRI * @property {string} value - Object value (IRI, literal, or blank node) * @property {string} [datatype] - Literal datatype * @property {string} [language] - Literal language tag */ /** * Validation result * @typedef {Object} ValidationResult * @property {boolean} valid - Whether validation passed * @property {ValidationError[]} errors - List of validation errors * @property {ValidationError[]} warnings - List of validation warnings * @property {number} triplesValidated - Number of triples validated */ /** * RDF Validator class * * **Poka-Yoke Design**: Validates RDF data before store insertion. * Prevents invalid data from corrupting the knowledge graph. */ export class RDFValidator { /** * Create RDF Validator * * @param {Object} [oxigraphBridge] - Optional OxigraphBridge for graph queries */ constructor(oxigraphBridge?: any); /** @type {Object|null} */ bridge: any | null; /** @type {Map} */ shapes: Map; /** @type {ValidationError[]} */ errors: ValidationError[]; /** @type {ValidationError[]} */ warnings: ValidationError[]; /** @type {Object} */ prefixes: { [x: string]: string; }; /** * Register built-in shapes for common RDF types * @private */ private _registerBuiltInShapes; /** * Expand prefixed name to full IRI * * @param {string} prefixedName - Prefixed name (e.g., 'foaf:name') * @returns {string} Full IRI or original if not prefixed */ expandPrefix(prefixedName: string): string; /** * Validate IRI format * * **Poka-Yoke**: Ensures IRI conforms to RFC 3987 format * * @param {string} value - Value to validate as IRI * @returns {{valid: boolean, error?: ValidationError}} */ validateIRI(value: string): { valid: boolean; error?: ValidationError; }; /** * Validate literal value against datatype * * **Poka-Yoke**: Ensures literal conforms to XSD datatype * * @param {string} value - Literal value * @param {string} datatype - Datatype IRI or prefixed name * @returns {{valid: boolean, error?: ValidationError}} */ validateLiteral(value: string, datatype: string): { valid: boolean; error?: ValidationError; }; /** * Validate that required property is present for a subject * * @param {string} subject - Subject IRI * @param {string} property - Property IRI * @param {Triple[]} triples - Triples to search * @returns {{valid: boolean, error?: ValidationError}} */ validateProperty(subject: string, property: string, triples?: Triple[]): { valid: boolean; error?: ValidationError; }; /** * Register a validation shape * * @param {string} shapeName - Shape name/IRI * @param {ShapeRule[]} rules - Validation rules * @param {string} [targetClass] - Optional target class */ registerShape(shapeName: string, rules: ShapeRule[], targetClass?: string): void; /** * Validate a single triple * * @param {Triple} triple - Triple to validate * @returns {{valid: boolean, errors: ValidationError[]}} */ validateTriple(triple: Triple): { valid: boolean; errors: ValidationError[]; }; /** * Validate triples against a registered shape * * @param {string} shapeName - Shape name to validate against * @param {string} subject - Subject IRI to validate * @param {Triple[]} triples - Triples describing the subject * @returns {{valid: boolean, errors: ValidationError[]}} */ validateAgainstShape(shapeName: string, subject: string, triples: Triple[]): { valid: boolean; errors: ValidationError[]; }; /** * Validate all triples in a graph * * @param {Triple[]} triples - Triples to validate * @param {Object} [options] - Validation options * @param {string[]} [options.shapes] - Specific shapes to validate against * @param {boolean} [options.validateStructure=true] - Validate triple structure * @returns {Promise} */ validateGraph(triples: Triple[], options?: { shapes?: string[]; validateStructure?: boolean; }): Promise; /** * Get accumulated validation errors * * @returns {ValidationError[]} */ getValidationErrors(): ValidationError[]; /** * Get accumulated validation warnings * * @returns {ValidationError[]} */ getValidationWarnings(): ValidationError[]; /** * Clear accumulated errors and warnings */ clearErrors(): void; /** * Get registered shape names * * @returns {string[]} */ getRegisteredShapes(): string[]; /** * Get shape definition * * @param {string} shapeName - Shape name * @returns {ShapeDefinition|undefined} */ getShape(shapeName: string): ShapeDefinition | undefined; /** * Add namespace prefix * * @param {string} prefix - Prefix (without colon) * @param {string} namespace - Full namespace IRI */ addPrefix(prefix: string, namespace: string): void; } /** * Validation error structure */ export type ValidationError = { /** * - Error type (e.g., 'IRI_INVALID', 'DATATYPE_MISMATCH') */ type: string; /** * - Human-readable error message */ message: string; /** * - Additional context (subject, property, value) */ context?: any; /** * - Error severity */ severity: "error" | "warning"; }; /** * Shape rule definition */ export type ShapeRule = { /** * - Property IRI or prefixed name */ property: string; /** * - Whether property is required */ required?: boolean; /** * - Expected datatype (prefixed or full IRI) */ datatype?: string; /** * - Minimum numeric value (for numeric datatypes) */ minValue?: number; /** * - Maximum numeric value (for numeric datatypes) */ maxValue?: number; /** * - Minimum string length */ minLength?: number; /** * - Maximum string length */ maxLength?: number; /** * - Regex pattern for string values */ pattern?: RegExp; /** * - Minimum occurrences */ minCount?: number; /** * - Maximum occurrences (undefined = unlimited) */ maxCount?: number; /** * - Allowed values (closed list) */ in?: string[]; /** * - Expected node kind ('IRI', 'Literal', 'BlankNode') */ nodeKind?: string; }; /** * Shape definition */ export type ShapeDefinition = { /** * - Shape name/IRI */ shape: string; /** * - Target class for this shape */ targetClass?: string; /** * - Validation rules */ rules: ShapeRule[]; }; /** * Triple structure */ export type Triple = { /** * - Subject IRI or blank node */ subject: string; /** * - Predicate IRI */ predicate: string; /** * - Object value (IRI, literal, or blank node) */ value: string; /** * - Literal datatype */ datatype?: string; /** * - Literal language tag */ language?: string; }; /** * Validation result */ export type ValidationResult = { /** * - Whether validation passed */ valid: boolean; /** * - List of validation errors */ errors: ValidationError[]; /** * - List of validation warnings */ warnings: ValidationError[]; /** * - Number of triples validated */ triplesValidated: number; };