GS1.ValidatorConfig (gs1_barcode v0.1.2)

View Source

GS1 Validator config struct.

Defines the rules used to validate a parsed GS1.DataStructure.t/0. Supports builder style, allowing to chain configuration options.

Options

  • :fail_fast - If true, validation stops when error found. Defaults to true.
  • :required_ais - list of AIs that must appear in the Data Structure.
  • :forbidden_ais - ist of AIs that must NOT appear.
  • :constraints - map of custom validation functions keyed by AI.

Summary

Types

t()

Validation strategy and ruleset.

Functions

Creates a new GS1.ValidatorConfig with optional default values.

Adds a custom validation constraint for a specific AI.

Adds a single AI to the forbidden list.

Adds a single AI to the required list.

Sets (replaces) map of constraints.

Sets the fail_fast strategy.

Sets (replaces) list of forbidden AIs.

Sets (replaces) list of required AIs.

Types

t()

@type t() :: %GS1.ValidatorConfig{
  constraints: %{required(String.t()) => GS1.Validator.Constraint.predicate()},
  fail_fast: boolean(),
  forbidden_ais: [String.t()],
  required_ais: [String.t()]
}

Validation strategy and ruleset.

Functions

new(opts \\ [])

@spec new(
  fail_fast: boolean(),
  required_ais: [String.t()],
  forbidden_ais: [String.t()],
  constraints: %{required(String.t()) => GS1.Validator.Constraint.predicate()}
) :: t()

Creates a new GS1.ValidatorConfig with optional default values.

Examples

iex> GS1.ValidatorConfig.new(fail_fast: false, required_ais: ["01", "21"])
%GS1.ValidatorConfig{
  fail_fast: false,
  required_ais: ["01", "21"],
  forbidden_ais: [],
  constraints: %{}
}

put_constraint(config, ai, fun)

@spec put_constraint(t(), String.t(), GS1.Validator.Constraint.predicate()) :: t()

Adds a custom validation constraint for a specific AI.

Examples

iex> GS1.ValidatorConfig.new()
...> |> GS1.ValidatorConfig.put_constraint("01", fn val -> String.length(val) == 14 end)

put_forbidden_ai(config, ai)

@spec put_forbidden_ai(t(), String.t()) :: t()

Adds a single AI to the forbidden list.

put_required_ai(config, ai)

@spec put_required_ai(t(), String.t()) :: t()

Adds a single AI to the required list.

set_constraints(config, constraints)

@spec set_constraints(t(), %{
  required(String.t()) => GS1.Validator.Constraint.predicate()
}) :: t()

Sets (replaces) map of constraints.

set_fail_fast(config, fail_fast)

@spec set_fail_fast(t(), boolean()) :: t()

Sets the fail_fast strategy.

set_forbidden_ais(config, ais)

@spec set_forbidden_ais(t(), [String.t()]) :: t()

Sets (replaces) list of forbidden AIs.

set_required_ais(config, ais)

@spec set_required_ais(t(), [String.t()]) :: t()

Sets (replaces) list of required AIs.