# TextDecoder overview

This namespace contains string decoders and additional utilities for string validations.

# Summary

# Functions

# TextDecoder.length

# Description

Check the length of the string

(len: number) => <I>(value: Decoder<I, unknown>) => Decoder<I, unknown>

# TextDecoder.min

# Description

Check the minimum length of the string

(minLength: number) => <I>(value: Decoder<I, unknown>) => Decoder<I, unknown>

# Example

const decoder = pipe(
  TextDecoder.string,
  TextDecoder.min(1)
)

expect(pipe('1', Decoder.validate(decoder), Result.isOk)).toBe(true)
expect(pipe('', Decoder.validate(decoder), Result.isKo)).toBe(true)

# TextDecoder.max

# Description

Check the maximum length of the string

(maxLength: number) => <I>(value: Decoder<I, unknown>) => Decoder<I, unknown>

# Example

const decoder = pipe(
  TextDecoder.string,
  TextDecoder.max(5)
)

expect(pipe('12345', Decoder.validate(decoder), Result.isOk)).toBe(true)
expect(pipe('123456', Decoder.validate(decoder), Result.isKo)).toBe(true)

# TextDecoder.between

# Description

Check both the minimum and maximum length of the string

(minLength: number, maxLength: number) => any

# Example

const decoder = pipe(
  TextDecoder.string,
  TextDecoder.between(1, 100)
)

expect(pipe('', Decoder.validate(decoder), Result.isKo)).toBe(true)

# TextDecoder.trim

# Description

Trim the string

<I>(decoder: Decoder<I, unknown>) => Decoder<I, unknown>

# Example

const decoder = pipe(
  TextDecoder.string,
  TextDecoder.trim,
  TextDecoder.between(1, 100)
)

expect(pipe('     ', Decoder.validate(decoder), Result.isKo)).toBe(true)

# TextDecoder.pattern

# Description

Check if the string matches a given pattern / regexp

<T extends string = string>(regexp: RegExp, message?: string, meta?: any) => <I>(value: Decoder<I, unknown>) => Decoder<I, unknown>

# Example

const decoder = pipe(
  TextDecoder.string,
  TextDecoder.pattern(/^Hello [a-zA-Z]+$/)
)

expect(pipe('Hello world', Decoder.validate(decoder), Result.isOk)).toBe(true)

# TextDecoder.varchar

# Description

Check if the input is a string between the given length.

(minLength: number, maxLength: number) => any

# Example

// The following:
const decoder = TextDecoder.varchar(1, 100)
// is the same as:
const decoder = pipe(TextDecoder.string, TextDecoder.between(1, 100))

# TextDecoder.oneOf

WARNING

Use EnumDecoder.isIn instead.

# Description

Check if the string is included in the given values

<T extends string>(arr: Array<T>): Decoder<unknown, T>
<T extends string>(arr: Set<T>): Decoder<unknown, T>

# Example

const decoder = TextDecoder.oneOf(['todo', 'in-progress', 'done', 'archived'] as const)

expect(pipe("todo", Decoder.validate(decoder), Result.isOk)).toBe(true)
expect(pipe("unknown", Decoder.validate(decoder), Result.isKo)).toBe(true)

# TextDecoder.equals

WARNING

Use EnumDecoder.literal instead.

# Description

Check if the string is included in the given values

<T extends string>(value: T) => any

# Example

const decoder = TextDecoder.equals('ongoing')

expect(pipe("todo", Decoder.validate(decoder), Result.isOk)).toBe(true)
expect(pipe("unknown", Decoder.validate(decoder), Result.isKo)).toBe(true)

# TextDecoder.htmlEscape

# Description

Escapes the HTML in the string.

<I>(decoder: Decoder<I, unknown>) => Decoder<I, unknown>

# Example

const decoder = pipe(
  TextDecoder.string,
  TextDecoder.trim,
  TextDecoder.htmlEscape,
  TextDecoder.between(1, 2000)
)

const escaped = pipe(
  "<script>window.alert("Hello")</script>",
  Decoder.validate(decoder),
  Result.get
)

expect(escaped).toBe('&lt;script&gt;window.alert(&quot;Hello&quot;)&lt;/script&gt;')