Validation Rules

Tutorials

Interface

class RulesBuilder

A builder to create validation rules programmatically.

Public Functions

template<typename ...Attributes>
inline void addRule(const NamePathLike &namePath, const RuleType ruleType, Attributes... attributes)

Add a rule to the document.

Parameters:
  • namePath – The name-path of the new rule.

  • ruleType – The type of the new rule.

  • attributes – All the attributes for the rule definition.

template<typename ...Attributes>
inline void addAlternative(const NamePathLike &namePath, const RuleType ruleType, Attributes... attributes)

Add an alternative to the document.

Parameters:
  • namePath – The name-path of the new rule.

  • ruleType – The type of the new rule.

  • attributes – All the attributes for the rule definition.

void reset()

Reset the builder and discard the current rules.

RulesPtr takeRules()

Finalize the rules document and return the rules.

This will finalize the currently processed rules document and return it to the caller. The builder is reset afterward and can be reused to create a new rules document.

If errors are found while finalizing the rules document, an exception is thrown.

Throws:

Error – (Validation) on any logical error found. E.g. missing key references.

Returns:

The finalized rules document.

class Rule

Public Functions

virtual NamePath namePath() const = 0

The target name-path of this rule.

This is the name path of the value in the validated document.

virtual RuleType type() const = 0

The type of the rule.

virtual String title() const = 0

An optional title.

virtual String description() const = 0

An optional description.

virtual bool hasDefault() const = 0

If this rule has a default.

virtual bool hasCustomError() const = 0

Test if this rule has a custom error message.

virtual String customError() const = 0

A custom error message for the rule.

virtual std::vector<ConstraintPtr> constraints() const = 0

A list of constraints.

virtual bool isOptional() const = 0

If this rule is optional.

virtual CaseSensitivity caseSensitivity() const = 0

If constraints are tested case-sensitive.

virtual bool isSecret() const = 0

If the validated value is a secret.

virtual std::vector<RulePtr> children() const = 0

A list of child rules.

virtual bool hasLocation() const noexcept = 0

Test if there is location info.

virtual const Location &location() const noexcept = 0

Get the location info.

virtual void setLocation(const Location &newLocation) noexcept = 0

Set the location info.

typedef std::shared_ptr<Rule> erbsland::conf::vr::RulePtr
class RuleType

Construction and Assignment

RuleType() = default

Create an undefined value type.

inline RuleType(const Enum value)

Create a new value type.

Parameters:

value – The enum value.

RuleType(const RuleType&) = default

Default copy constructor.

~RuleType() = default

Default destructor.

RuleType &operator=(const RuleType&) = default

Default copy assignment.

Operators

inline RuleType &operator=(const Enum value) noexcept

Assign an enum.

inline constexpr operator Enum() const noexcept

Cast back to the enum value.

Tests

inline constexpr bool isUndefined() const noexcept

Test if the type is undefined.

inline bool isList() const noexcept

Test if this type represents any kind of list.

inline bool isScalar() const noexcept

Test if this type represents a scalar value.

inline bool acceptsDefaults() const noexcept

Test if this rule type accepts defaults.

bool matchesValueType(ValueType valueType) const noexcept

Test if this rule type matches a value type.

Public Functions

const String &toText() const noexcept

Convert this type into text.

ValueType toValueType() const noexcept

Convert this type into a value type.

const String &expectedValueTypeText() const noexcept

Get the expected type text for this rule type.

inline constexpr Enum raw() const noexcept

Access the underlying enum value.

Public Static Functions

static RuleType fromText(const String &text) noexcept

Create a validation type from a given text.

Returns:

The validation type or Undefined if the text does not match any valid type.

static const std::array<RuleType, 19> &all() noexcept

Get an array with all value types.

class Constraint

A constraint for a validation-rule.

Public Functions

virtual String name() const = 0

The name.

virtual ConstraintType type() const = 0

The type.

virtual bool hasCustomError() const = 0

Test if a custom error message set.

virtual String customError() const = 0

The custom error message.

virtual bool isNegated() const = 0

If the result of this constraint test is negated.

virtual bool hasLocation() const noexcept = 0

Test if there is location info.

virtual const Location &location() const noexcept = 0

Get the location info.

virtual void setLocation(const Location &newLocation) noexcept = 0

Set the location info.

using erbsland::conf::vr::ConstraintPtr = std::shared_ptr<Constraint>
class ConstraintType

Construction and Assignment

ConstraintType() = default

Create an undefined value type.

inline ConstraintType(const Enum value)

Create a new value type.

Parameters:

value – The enum value.

ConstraintType(const ConstraintType&) = default

Default copy constructor.

~ConstraintType() = default

Default destructor.

ConstraintType &operator=(const ConstraintType&) = default

Default copy assignment.

Operators

inline ConstraintType &operator=(const Enum value) noexcept

Assign an enum.

inline constexpr operator Enum() const noexcept

Cast back to the enum value.

Tests

inline constexpr bool isUndefined() const noexcept

Test if the type is undefined.

Public Functions

const String &toText() const noexcept

Convert this type into text.

inline constexpr Enum raw() const noexcept

Access the underlying enum value.

Public Static Functions

static ConstraintType fromText(const String &text) noexcept

Create a validation type from a given text.

Returns:

The validation type or Undefined if the text does not match any valid type.

static inline const std::array<ConstraintType, 15> &all() noexcept

Get an array with all value types.