@3dsource/business-rules
v0.0.2-dev.0
Published
Business rules for 3dsource applications
Readme
@3dsource/business-rules
Business rules of the 3dsource modular configurator: the rule set format, the operations that change it, and the evaluation that tells which component types are required or optional.
The core is pure functions. It does no network or storage calls, gets everything as arguments and never modifies its inputs. The rule set belongs to the core: other code does not parse or change it by hand but goes through these functions.
Used by:
- the Metabox configurator editor — shows and changes the rules;
- the Stream configurator frontend — applies the rules while a user builds a configuration;
- the backend — through its adapter, which parses the input and decides which issues to show.
Installation
npm install @3dsource/business-rulesVersion 1
- Rule set format
RuleSetV1(schemaVersion: 1). - One rule type,
componentOptionality: it makes a component type optional and sets whether a component of it is selected at the start. - One condition,
always. - One target, a component type. A rule applies to every component of the type, never to a single component.
{
"schemaVersion": 1,
"rules": [
{
"id": "88888888-8888-4888-8888-888888888888",
"type": "componentOptionality",
"condition": { "type": "always" },
"result": {
"target": {
"type": "componentType",
"id": "33333333-3333-4333-8333-333333333333"
},
"defaultState": "unselected"
}
}
]
}Results and issues
Every function except createRuleSet returns either the value or { issues }. A wrong input
is never thrown: an exception means a bug in the library.
Each issue has a literal code for code, a path to the input field it is about (e.g.
position, rule.result.target, ruleSet.rules[2].id) and a message for diagnostics, not
for users.
- Operations and evaluation return every issue found, in parameter order.
- JSON parsing returns the first issue and stops.
Applying the rules
Parse the stored set, evaluate it against the configurator, then read each component type.
import type { Configurator } from '@3dsource/business-rules';
import { createRuleSet, createRuleSetFromJson, evaluateRules, resolveOptionality } from '@3dsource/business-rules';
// `stored` is the parsed JSON of the set; `null` when the configurator has no rules yet.
function optionalities(stored: unknown, configurator: Configurator) {
const parsed = stored === null ? { ruleSet: createRuleSet() } : createRuleSetFromJson(stored);
if ('issues' in parsed) return parsed;
const evaluated = evaluateRules({ ruleSet: parsed.ruleSet, configurator });
if ('issues' in evaluated) return evaluated;
// Each: { kind: 'required' } or { kind: 'optional', defaultState, ruleId }
return {
types: configurator.componentTypes.map((type) => ({
type,
optionality: resolveOptionality(type, evaluated.rules),
})),
};
}- The root type is always required.
- A type with an effective
componentOptionalityrule is optional;defaultStateis its initial state. - Any other type is required, while its parent is present.
- When parsing or evaluation returns issues, apply no rules: a partly valid set is never used.
In V1 the result does not depend on the user's selection. Evaluate once and again only when the set or the configurator changes.
Changing the set
import { addRule, removeRule, replaceRule, toChoice } from '@3dsource/business-rules';
const choice = toChoice(optionality); // 'required' | 'optionalSelected' | 'optionalUnselected'
const result = addRule({
ruleSet,
ruleId: crypto.randomUUID(),
schemaVersion: ruleSet.schemaVersion,
position: null, // append; or a 1-based position
rule: {
type: 'componentOptionality',
condition: { type: 'always' },
result: {
target: { type: 'componentType', id: typeId },
defaultState: 'selected',
},
},
configurator,
});
if ('issues' in result) report(result.issues);
else save(result.ruleSet);| Editor change | Operation |
| ------------------------ | --------------------------- |
| Required → Optional | addRule |
| One Optional → the other | replaceRule with ruleId |
| Optional → Required | removeRule with ruleId |
Operations check the new rule against the configurator, but not the other rules of the set. A set with a stale rule can still be changed and fixed.
Data from outside — a rule a client sent, a set from storage or GraphQL — goes through
createRuleFromJson or createRuleSetFromJson first. Both take a parsed JSON value, not a
string.
When the configurator changes
Rules are not removed on their own. Call removeTarget with the configurator after the change:
- a component type was deleted, including when its last component was deleted — with the deleted type;
- the root type changed — with the new root.
const result = removeTarget({
ruleSet,
target: { type: 'componentType', id: deletedTypeId },
configurator,
});Deleting a single component while the type keeps others changes no rules.
A rule left on a missing type or on the root makes evaluateRules return unknown_target or
forbidden_target, and then no rules apply.
API reference
API reference — every function, type and issue, generated from the TSDoc.
| Function | Purpose |
| ----------------------- | ------------------------------------------------ |
| createRuleSet | An empty set of the current version |
| createRuleSetFromJson | Parse a stored set |
| createRuleFromJson | Parse a rule body |
| addRule | Add a rule at a position |
| replaceRule | Replace a rule body, keeping its id and position |
| removeRule | Remove one rule |
| removeTarget | Remove every rule of a target |
| evaluateRules | The effective rules for a configurator |
| resolveOptionality | Required or optional for one component type |
| toChoice | The editor choice for an optionality |
| evaluateCondition | Whether one condition holds |
Repository development
Run commands from the repository root:
pnpm business-rules:build
pnpm business-rules:docs
pnpm business-rules:test
pnpm business-rules:lint
pnpm business-rules:release:patchbusiness-rules:docsregenerates the API reference intoapi/from the TSDoc ofsrc/public-api.ts. The folder is gitignored; the package build regenerates it, so the published reference always matches the code.- The release command performs verification, changes the package version and publishes the verified tarball to npm.
