morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::forms::InstanceConstraints Class Reference

A set of per-field constraints sourced from one instance (a database row, a tenant record, a catalogue version) — the single declaration that both decorates a served schema and checks a submitted value. More...

#include <instance_constraints.hpp>

Public Member Functions

InstanceConstraints & declare (FieldConstraint constraint)
 Declares (or replaces, by field name) one field's constraint.
const FieldConstraint * forField (std::string_view field) const noexcept
 The constraint declared for field, if any.
bool empty () const noexcept
 Whether nothing has been declared.
const std::vector< FieldConstraint > & fields () const noexcept
 Every declared constraint, in declaration order.
std::string decorate (std::string schema) const
 Rewrites schema so the framework's own keys carry this instance's values.
std::vector< ConstraintViolation > checkValue (std::string_view field, const math::Rational &value) const
 Checks one exact value against the constraint declared for field.
template<typename A>
std::vector< ConstraintViolation > checkAction (const A &action) const
 Checks every constrained Quantity member of action.

Detailed Description

A set of per-field constraints sourced from one instance (a database row, a tenant record, a catalogue version) — the single declaration that both decorates a served schema and checks a submitted value.

constraints.declare({.field = "value",
.decimalPlaces = version.decimalPlaces,
.minimum = version.specLow,
.maximum = version.specHigh});
// Serving: the framework's own keys now carry the version's values.
// Checking: the same object, so the served bound and the checked bound
// cannot disagree.
for (const auto& violation : constraints.checkAction(action)) { ... }
A set of per-field constraints sourced from one instance (a database row, a tenant record,...
InstanceConstraints & declare(FieldConstraint constraint)
Declares (or replaces, by field name) one field's constraint.
std::vector< ConstraintViolation > checkAction(const A &action) const
Checks every constrained Quantity member of action.
std::string instanceSchemaJson(const InstanceConstraints &constraints)
schemaJson<A>() decorated with constraints — the served form for one instance.

Definition at line 197 of file instance_constraints.hpp.

Member Function Documentation

◆ declare()

InstanceConstraints & morph::forms::InstanceConstraints::declare ( FieldConstraint constraint)
inline

Declares (or replaces, by field name) one field's constraint.

Parameters
constraintThe constraint to record.
Returns
*this, so declarations can be chained — a reference into this InstanceConstraints, valid only for as long as it is.

Definition at line 203 of file instance_constraints.hpp.

◆ forField()

const FieldConstraint * morph::forms::InstanceConstraints::forField ( std::string_view field) const
inlinenodiscardnoexcept

The constraint declared for field, if any.

Parameters
fieldThe wire (JSON) field name to look up.
Returns
A pointer to the entry, or nullptr when the field is unconstrained — into this InstanceConstraints, so valid only for as long as it is. Invalidated by any later declare().

Definition at line 219 of file instance_constraints.hpp.

◆ empty()

bool morph::forms::InstanceConstraints::empty ( ) const
inlinenodiscardnoexcept

Whether nothing has been declared.

Returns
true when no field carries a constraint.

Definition at line 230 of file instance_constraints.hpp.

◆ fields()

const std::vector< FieldConstraint > & morph::forms::InstanceConstraints::fields ( ) const
inlinenodiscardnoexcept

Every declared constraint, in declaration order.

Returns
The constraint entries — a reference into this InstanceConstraints, valid only for as long as it is.

Definition at line 235 of file instance_constraints.hpp.

◆ decorate()

std::string morph::forms::InstanceConstraints::decorate ( std::string schema) const
inlinenodiscard

Rewrites schema so the framework's own keys carry this instance's values.

For each declared field that the schema actually has a property for: x-decimalPlaces is overwritten with the declared precision (when one is declared), and x-minimum / x-maximum are added as {"num","den","dp"} nodes. The document then carries x-instanceConstraints: the array of field names whose keys came from instance data, which is how a renderer (and a reviewer) tells a decorated schema from the compiled one.

Never throws and never mangles: a schema that is not readable JSON is returned verbatim, and a constraint naming a field the schema has no property for is skipped — the same "an author's declaration mistake is not worth a crash" rule mergeSchemaExtras follows.

Parameters
schemaThe compiled schema text, typically schemaJson<A>().
Returns
The decorated schema text.

Definition at line 254 of file instance_constraints.hpp.

◆ checkValue()

std::vector< ConstraintViolation > morph::forms::InstanceConstraints::checkValue ( std::string_view field,
const math::Rational & value ) const
inlinenodiscard

Checks one exact value against the constraint declared for field.

The value-level entry point, for a model whose stored value is derived from the payload rather than taken from it verbatim (a reading multiplied by a dilution factor, a total recomputed from line items): the constraint governs the value the model is about to store, which is not a member of the action at all.

Parameters
fieldThe wire (JSON) field name the value belongs to.
valueThe exact value to check.
Returns
Every violation, in minimum, maximum, decimalPlaces order; empty when the field is unconstrained or the value satisfies every declared key.

Definition at line 313 of file instance_constraints.hpp.

◆ checkAction()

template<typename A>
std::vector< ConstraintViolation > morph::forms::InstanceConstraints::checkAction ( const A & action) const
inlinenodiscard

Checks every constrained Quantity member of action.

Walks the action's reflected members in declaration order and runs checkValue for each engaged Quantity whose wire name carries a constraint. An empty Quantity is skipped (emptiness is required's business, not a bound's), as is any member that is not a Quantity — bounds and decimal precision are exact-value concepts, and Quantity is the only field type morph forms carry an exact value in. A no-op for an action type glaze cannot reflect.

Template Parameters
AAction type (a reflectable aggregate).
Parameters
actionThe decoded action to check.
Returns
Every violation across every constrained field, in member declaration order.

Definition at line 346 of file instance_constraints.hpp.


The documentation for this class was generated from the following file: