/** * @fileoverview JSON validation utility with support for flexible validation rules. * * This module provides a lightweight JSON validator that supports special field * suffixes and validation rules for validating objects against example templates. * It's designed to be simpler and more flexible than formal JSON Schema validation. * * ## Validation Syntax * * Field names can include validation rules using these patterns: * - `fieldName?` - Optional field * - `fieldName*` - Required field with any content (wildcard) * - `fieldName:rule` - Field with validation rule * - `fieldName:rule1:rule2` - Field with multiple validation rules * - `fieldName:rule?` - Optional field with validation rule * * ## Supported Validation Rules * * **Array Length:** * - `[N+]` - Array with at least N elements * - `[N-M]` - Array with between N and M elements * - `[=N]` - Array with exactly N elements * * **Type Checking:** * - `string` - Must be a string * - `number` - Must be a number * - `boolean` - Must be a boolean * - `object` - Must be an object (not array) * - `array` - Must be an array * * **Value Constraints:** * - `!empty` - Non-empty string, array, or object * * @module @memberjunction/global * @author MemberJunction.com * @since 3.0.0 */ import { ValidationResult } from './ValidationTypes.js'; /** * Lightweight JSON validator with flexible validation rules. * * @example * ```typescript * const validator = new JSONValidator(); * * const template = { * "name": "John Doe", // Required field * "email?": "user@example.com", // Optional field * "settings*": {}, // Required, any content * "tags:[1+]": ["tag1"], // Array with 1+ items * "age:number": 25 // Must be number * }; * * const data = { * name: "Jane Smith", * tags: ["work", "urgent"], * age: 30 * }; * * const result = validator.validate(data, template); * if (result.Success) { * console.log('Validation passed!'); * } * ``` */ export declare class JSONValidator { /** * Validates an object against a template with validation rules. * * @param data - The data object to validate * @param template - The template object with validation rules * @param path - The current path in the object hierarchy (used internally) * @returns ValidationResult with Success flag and any validation errors */ validate(data: unknown, template: unknown, path?: string): ValidationResult; /** * Validates an object against a template recursively. * * @private */ private validateObject; /** * Parses a field key to extract the field name and validation rules. * * @private */ private parseFieldKey; /** * Parses validation rules from a string, handling special cases like array syntax. * * @private */ private parseValidationRules; /** * Applies validation rules to a field value. * * @private */ private applyValidationRules; /** * Validates array length constraints. * * @private */ private validateArrayLength; /** * Validates type constraints. * * @private */ private validateType; /** * Validates non-empty constraint. * * @private */ private validateNonEmpty; /** * Validates an object against a JSON schema string. * Convenience method that parses the schema and validates. * * @param data - The data to validate * @param schemaJson - JSON string containing the validation schema * @returns ValidationResult with Success flag and any validation errors */ validateAgainstSchema(data: unknown, schemaJson: string): ValidationResult; /** * Cleans validation syntax from JSON object keys. * * This method recursively processes a JSON object and removes validation * syntax markers (?, *, :rules) from all object keys. This is useful when * AI models mistakenly include our validation syntax in their responses. * * @example * ```typescript * interface MyData { * name: string; * items: string[]; * config: { enabled: boolean }; * } * * const dirtyJson = { * "name?": "John", * "items:[1+]": ["a", "b"], * "config*": { "enabled?": true } * }; * * const cleanJson = validator.cleanValidationSyntax(dirtyJson); * // Result is typed as MyData * ``` * * @template T - The expected type of the cleaned data * @param data - The JSON data to clean * @returns A new object with cleaned keys, typed as T */ cleanValidationSyntax(data: unknown): T; } //# sourceMappingURL=JSONValidator.d.ts.map