Class: GuardrailsRuby::Checks::MaxLength

Inherits:
GuardrailsRuby::Check show all
Defined in:
lib/guardrails_ruby/checks/max_length.rb

Instance Attribute Summary

Attributes inherited from GuardrailsRuby::Check

#options

Instance Method Summary collapse

Methods inherited from GuardrailsRuby::Check

check_name, direction, #initialize, lookup

Constructor Details

This class inherits a constructor from GuardrailsRuby::Check

Instance Method Details

#call(text, context: {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/guardrails_ruby/checks/max_length.rb', line 9

def call(text, context: {})
  max_chars = @options[:chars]
  max_tokens = @options[:tokens]

  if max_chars && text.length > max_chars
    fail! "Input exceeds maximum length of #{max_chars} characters (got #{text.length})"
  elsif max_tokens && estimate_tokens(text) > max_tokens
    fail! "Input exceeds maximum length of #{max_tokens} tokens (estimated #{estimate_tokens(text)})"
  else
    pass!
  end
end