Class AutomationDSLValidator

java.lang.Object
io.github.jspinak.brobot.runner.json.validation.schema.AutomationDSLValidator

@Component public class AutomationDSLValidator extends Object
Validates automation DSL JSON against the Brobot DSL schema.

This validator ensures that automation function definitions conform to the expected DSL (Domain Specific Language) schema. It validates the syntax and structure of automation functions, including their parameters, statements, and expressions, before they are parsed and executed.

DSL Purpose:

The Brobot DSL allows users to define automation logic in a structured JSON format. This includes function declarations, control flow statements, variable assignments, and API method calls. The validator ensures this code-like JSON structure is well-formed.

Schema Location:

The DSL schema is loaded from /schemas/automation-dsl-schema.json on the classpath. This schema defines valid statement types, expression formats, and function structures.

Validation Layers:

  1. JSON Parsing - Ensures valid JSON syntax
  2. Schema Validation - Checks DSL structure against schema
  3. Semantic Validation - Additional language-specific rules

Semantic Validations:

  • Function name uniqueness
  • Return statement presence for non-void functions
  • Variable scope and definition checking
  • Expression consistency validation

DSL Elements Validated:

  • Functions - Name, parameters, return type, body statements
  • Statements - Variable declarations, assignments, control flow
  • Expressions - Variables, literals, method calls, operations
  • Control Flow - If/else branches, forEach loops, return statements

Usage Example:


 AutomationDSLValidator validator = new AutomationDSLValidator();

 String dslJson = Files.readString(Path.of("automation-functions.json"));
 ValidationResult result = validator.validate(dslJson);

 // Check for critical issues
 if (result.hasCriticalErrors()) {
     throw new ConfigValidationException(
         "DSL syntax errors", result);
 }

 // Check for semantic issues
 result.getErrors().stream()
     .filter(e -> e.errorCode().contains("Undefined variable"))
     .forEach(e -> logger.error("Variable error: {}", e.message()));
 
Author:
jspinak
See Also:
  • Constructor Details

    • AutomationDSLValidator

      public AutomationDSLValidator()
      Initializes the validator by loading the DSL schema from classpath.

      This constructor loads and prepares the JSON schema for DSL validation. The schema file must be present on the classpath or initialization will fail with an IllegalStateException.

      Throws:
      IllegalStateException - if the schema file cannot be found or loaded, preventing creation of an invalid validator
  • Method Details

    • validate

      public ValidationResult validate(String jsonString)
      Validates the provided JSON string against the automation DSL schema.

      This method performs multi-level validation of DSL configurations, ensuring both structural correctness and semantic validity. It's designed to catch errors early before function parsing and execution.

      Validation Process:

      1. JSON Parsing - Validates JSON syntax
      2. Schema Validation - Checks structure against DSL schema
      3. Function Name Validation - Ensures unique function names
      4. Return Type Validation - Verifies return statements match types
      5. Expression Validation - Checks variable scoping and usage

      Error Categories:

      • CRITICAL - Malformed JSON or schema loading failures
      • ERROR - Invalid syntax, undefined variables, missing returns
      • WARNING - Unused parameters, unreachable code (future)
      Parameters:
      jsonString - The DSL configuration JSON string containing automation function definitions
      Returns:
      ValidationResult aggregating all validation errors found during the multi-phase validation process