Module: DeepL::Utils::TermValidator

Extended by:
TermValidator
Included in:
TermValidator
Defined in:
lib/deepl/utils/term_validator.rb

Constant Summary collapse

INVALID_CHARACTERS =

C0 and C1 control characters plus the Unicode line and paragraph separators.

/[\u0000-\u001F\u0080-\u009F

]/

Instance Method Summary collapse

Instance Method Details

#validate!(term) ⇒ String

Strips surrounding whitespace from a single glossary or spoken term, then checks it before it is serialized into a request.

Parameters:

  • term (String) —

    The term to check.

Returns:

  • (String) —

    The stripped term, which is what the request sends.

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/deepl/utils/term_validator.rb', line 22

def validate!(term)
  term = term.to_s.strip
  raise Exceptions::ValidationError, 'Term must not be empty' if term.empty?
  if INVALID_CHARACTERS.match?(term)
    raise Exceptions::ValidationError, "Term #{term.inspect} contains an invalid character"
  end

  term
end