Class: OlyxGuardrailsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/olyx/guardrails/rails/active_model_validator.rb

Overview

Validates an explicitly declared Active Model attribute with Olyx Guardrails.

validates :prompt, olyx_guardrails: true

By default the validator uses finalized Rails configuration. Pass olyx_guardrails: { policy: policy } to use a core Policy directly.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Evaluates value and adds an error to record and attribute when the active policy rejects it. Active Model calls this method.



14
15
16
17
18
19
# File 'lib/olyx/guardrails/rails/active_model_validator.rb', line 14

def validate_each(record, attribute, value)
  result = evaluate(record, attribute, value)
  return if result[:allowed]

  record.errors.add(attribute, options.fetch(:message, 'was rejected by guardrail policy'))
end