GS1.Validator.Constraint (gs1_barcode v0.1.2)

View Source

A DSL for building reusable GS1 AI validation constraint predicates, that are intended to be lightweight and composable.

Examples

import GS1.Validator.Constraint

constraint = all([is_num(), len(14)])

constraint.("12345678901234") # => true
constraint.("ABC")            # => false

Summary

Types

Validation predicate function that tests a string value against a constraint.

Functions

Returns true only if all predicates in the list return true. Acts as a logical AND.

Returns true if at least one predicate in the list returns true. Acts as a logical OR.

Checks if value is an integer string within the range [min, max] (inclusive).

Generates a validator for a specific pre-defined GS1 data format.

Checks if the value consists entirely of digits.

Checks if the string value has an exact length of n.

Checks if the value matches the given Regex.

Checks if string value has a length less than or equal to n.

Checks if string value has a length greater than or equal to n.

Inverts the result of a predicate.

Types

predicate()

@type predicate() :: (String.t() -> boolean())

Validation predicate function that tests a string value against a constraint.

Functions

all(predicates)

(macro)
@spec all([predicate()]) :: Macro.t()

Returns true only if all predicates in the list return true. Acts as a logical AND.

Usage

all([is_num(), len(5)])

any(predicates)

(macro)
@spec any([predicate()]) :: Macro.t()

Returns true if at least one predicate in the list returns true. Acts as a logical OR.

Usage

any([len(5), len(8)])

between(min, max)

(macro)
@spec between(pos_integer(), pos_integer()) :: Macro.t()

Checks if value is an integer string within the range [min, max] (inclusive).

format(type)

(macro)
@spec format(atom()) :: Macro.t()

Generates a validator for a specific pre-defined GS1 data format.

Supported Formats

  • :date_yymmdd - validates YYMMDD date format and represents a valid calendar date.
  • :date_yymmd0 - validates YYMMD0 format.

Usage

format(:date_yymmdd)

is_num()

(macro)
@spec is_num() :: Macro.t()

Checks if the value consists entirely of digits.

len(n)

(macro)
@spec len(pos_integer()) :: Macro.t()

Checks if the string value has an exact length of n.

matches(regex)

(macro)
@spec matches(Macro.t()) :: Macro.t()

Checks if the value matches the given Regex.

max_len(n)

(macro)
@spec max_len(pos_integer()) :: Macro.t()

Checks if string value has a length less than or equal to n.

min_len(n)

(macro)
@spec min_len(pos_integer()) :: Macro.t()

Checks if string value has a length greater than or equal to n.

not_(a)

(macro)
@spec not_(predicate()) :: Macro.t()

Inverts the result of a predicate.