Class: GuardrailsRuby::Checks::ScriptDetection

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

Constant Summary collapse

SCRIPTS =
{
  latin:      /[\p{Latin}]/,
  cyrillic:   /[\p{Cyrillic}]/,
  arabic:     /[\p{Arabic}]/,
  cjk:        /[\p{Han}]/,
  hangul:     /[\p{Hangul}]/,
  hiragana:   /[\p{Hiragana}]/,
  katakana:   /[\p{Katakana}]/,
  thai:       /[\p{Thai}]/,
  devanagari: /[\p{Devanagari}]/,
  greek:      /[\p{Greek}]/,
  hebrew:     /[\p{Hebrew}]/,
  georgian:   /[\p{Georgian}]/,
  armenian:   /[\p{Armenian}]/,
  ethiopic:   /[\p{Ethiopic}]/,
  tamil:      /[\p{Tamil}]/
}.freeze

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/guardrails_ruby/checks/script_detection.rb', line 27

def call(text, context: {})
  allowed = @options.fetch(:allowed_scripts, [:latin])
  allowed = allowed.map(&:to_sym)

  detected = detect_scripts(text)
  unexpected = detected - allowed

  if unexpected.any?
    fail! "Unexpected scripts detected: #{unexpected.join(', ')}",
      matches: unexpected.map(&:to_s)
  else
    pass!
  end
end