Class: RosettAi::Validators::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/validators/schema_validator.rb

Overview

Generic JSON Schema validator for YAML configuration files.

Subclasses specify which schema to use by passing the schema filename to the constructor. The schema is loaded from conf/schemas/ relative to the project root.

API: valid?(file_path), validate(file_path), errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



22
23
24
25
26
# File 'lib/rosett_ai/validators/schema_validator.rb', line 22

def initialize(schema:)
  @schema_filename = schema
  @errors = []
  @schemer = JSONSchemer.schema(load_schema)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



20
21
22
# File 'lib/rosett_ai/validators/schema_validator.rb', line 20

def errors
  @errors
end

Instance Method Details

#valid?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/rosett_ai/validators/schema_validator.rb', line 28

def valid?(file_path)
  @errors = []
  content = load_file(file_path)
  return false unless content

  validate_schema(content)
  @errors.empty?
end

#validate(file_path) ⇒ Object



37
38
39
40
# File 'lib/rosett_ai/validators/schema_validator.rb', line 37

def validate(file_path)
  valid?(file_path)
  @errors
end