@spotto/manifest-rules 1.0.70-alpha.42 → 1.0.70-alpha.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +189 -189
  2. package/dist/index.js +484 -484
  3. package/package.json +3 -3
package/dist/index.d.ts CHANGED
@@ -1,189 +1,189 @@
1
- /**
2
- * Manifest rules: the one evaluator (MANIFEST_RULES.md, MANIFEST_IMPLEMENTATION.md §4).
3
- *
4
- * A manifest's lines count what was found; its rules judge it. This module
5
- * evaluates the rules over a set of found items and, for READINESS rules, over
6
- * a check's answers. It is shared by everything that has to reach the same
7
- * result from the same items: the snapshot write, the kit recompute, and the
8
- * client's check screen.
9
- *
10
- * Two things it does NOT have, by design, so that it can run in all three:
11
- *
12
- * - **No database.** The caller loads the items and says which type each rule
13
- * names. What "the items found" are is the caller's decision too (at a check,
14
- * the entries the lines count; between checks, what the kit holds).
15
- * - **No expression engine of its own.** Expressions go through `@spotto/excelexp`,
16
- * the engine behind readiness, and the caller hands it in (`RuleEngine`). The
17
- * API and the client each have their own copy of that engine, at their own
18
- * version, and a field value in their own stored form; the caller turns a
19
- * value into the engine's form and this module never looks inside one.
20
- *
21
- * What it decides, so that no two callers can decide it differently:
22
- *
23
- * - **Order is by code unit.** Names are compared as plain `<` compares
24
- * strings, then ids, never by locale. A device set to another language and
25
- * the server must take the same items, because which `to` items a rule takes
26
- * decides which later rule comes out short.
27
- * - **`match` compares stored values.** Two items match on a field when they
28
- * hold the same stored value of the same kind. Text `18` and text `18.0` are
29
- * different values, though a filter's `==` would read both as the number 18.
30
- * - **An expression follows a readiness rule's missing-value policy.** Text
31
- * with no value reads as `''`, a yes/no as `false`; a number or a date with
32
- * no value fails the rule closed, as "Missing data: …", unless the
33
- * expression tests it with `ISSET()` or `ISBLANK()`.
34
- * - **A filter (`where`) is different.** It chooses, it does not judge: an item
35
- * with no value in a name the filter reads is left out, unless the filter
36
- * tests that name with `ISSET()` or `ISBLANK()`.
37
- */
38
- import { IManifestRule, IManifestRuleResult, ManifestRuleKind, ManifestRuleLevel, ManifestRuleValue } from '@spotto/contract';
39
- /**
40
- * The expression engine, as `@spotto/excelexp` exports it: pass the module
41
- * itself. `F` is its field value, which this module only passes through.
42
- */
43
- export interface RuleEngine<F> {
44
- EvaluateCondition(fields: F[], condition: string, now: Date, zone: string): boolean;
45
- FormatMessage(template: string, fields: F[], zone: string): {
46
- rendered: string;
47
- };
48
- ReferencedFields(expression: string): string[];
49
- PresenceGuardedFields(expression: string): string[];
50
- TemplateFields(template: string): string[];
51
- newFieldValue(init: {
52
- Name: string;
53
- Type: number;
54
- StringValue?: string;
55
- BoolValue?: boolean;
56
- }): F;
57
- FieldType: {
58
- String: number;
59
- Bool: number;
60
- Checkboxes: number;
61
- };
62
- }
63
- export declare type RuleDataType = 'STRING' | 'INTEGER' | 'DECIMAL' | 'BOOLEAN' | 'DATE' | 'DATETIME';
64
- /** A stored value, for `match` and as evidence. A multi-select is its ticked options. */
65
- export declare type RuleStoredValue = string | number | boolean | string[];
66
- /** What a subject holds under one name. */
67
- export interface RuleField<F> {
68
- /** The field's data type, where the subject defines the name at all. */
69
- dataType?: RuleDataType;
70
- /** A multi-select: its entries are a set, and an empty set is not missing data. */
71
- checkboxes?: boolean;
72
- /**
73
- * The value in the engine's form: one entry, or one per ticked option for a
74
- * multi-select. Empty when the subject has no value under the name; an
75
- * empty multi-select may be given empty too, and still resolves.
76
- */
77
- fields: F[];
78
- /** The value as stored. Undefined when there is none. */
79
- stored?: RuleStoredValue;
80
- }
81
- /**
82
- * Something an expression is evaluated over: a found item, or a check (its
83
- * answers over the checked asset's values and the context).
84
- */
85
- export interface RuleSubject<F> {
86
- /** The subject's EFFECTIVE value under a name: its own, with its type's
87
- * type-level value filling in where it has none. */
88
- field(name: string): RuleField<F>;
89
- }
90
- /** A found item. */
91
- export interface RuleItem<F> extends RuleSubject<F> {
92
- assetId: string;
93
- name: string;
94
- /** The item's type path. An item is of a type, or beneath it, when its path
95
- * starts with that type's path. */
96
- typePath: string;
97
- }
98
- export interface EvaluateManifestRulesInput<F> {
99
- /** The manifest's `rules`, in order. */
100
- rules: IManifestRule[];
101
- /** The items found. */
102
- items: RuleItem<F>[];
103
- /** What READINESS rules read, or `null` to leave them unevaluated. */
104
- env: RuleSubject<F> | null;
105
- /** A type's path by its id. Undefined when the type no longer exists: a
106
- * rule that names it then fails with that reason, rather than silently
107
- * never applying. */
108
- typePath: (typeId: string) => string | undefined;
109
- engine: RuleEngine<F>;
110
- /** For the engine: the moment of the evaluation, and the organisation's zone. */
111
- now: Date;
112
- timezone: string;
113
- }
114
- /** Plain code-unit order: what `<` gives two strings, on every runtime. */
115
- export declare const compareCodeUnits: (a: string, b: string) => number;
116
- declare type Context<F> = Pick<EvaluateManifestRulesInput<F>, 'engine' | 'now' | 'timezone' | 'typePath'>;
117
- /**
118
- * Does the subject pass the filter? A filter chooses; it does not judge. So
119
- * a subject with no value in a name the filter reads is left out, before the
120
- * expression runs, unless the filter tests that name's presence itself. A
121
- * filter that cannot be read or evaluated leaves the subject out.
122
- */
123
- export declare function passesFilter<F>(subject: RuleSubject<F>, where: string | undefined, ctx: Pick<Context<F>, 'engine' | 'now' | 'timezone'>): boolean;
124
- export interface RuleJudgement {
125
- /** The expression describes the problem: fired means there is one. */
126
- fired: boolean;
127
- reason?: string;
128
- /** Names the expression reads that the subject has no value for. */
129
- missingFields?: string[];
130
- /** What the expression read, as stored. */
131
- values: Record<string, ManifestRuleValue>;
132
- }
133
- /**
134
- * A message rendered over a subject, as a readiness rule's is: a missing
135
- * number or date reads "(not set)", missing text reads as nothing.
136
- */
137
- export declare function renderMessage<F>(subject: RuleSubject<F>, message: string, ctx: Pick<Context<F>, 'engine' | 'timezone'>): string;
138
- /**
139
- * Judge one subject with an expression that describes a problem, exactly as a
140
- * readiness rule on a type is judged (`readiness/evaluate.ts`): it fires when
141
- * the expression is true, when a number or a date it reads is missing and
142
- * unguarded, and when it cannot be parsed or evaluated.
143
- */
144
- export declare function judge<F>(subject: RuleSubject<F>, expression: string, message: string, ctx: Pick<Context<F>, 'engine' | 'now' | 'timezone'>): RuleJudgement;
145
- /**
146
- * Evaluate a manifest's rules: one result per rule, in the rules' order.
147
- *
148
- * RELATION rules run as one pass in array order, and a `to` item one rule
149
- * takes stays taken for every later rule: a battery serves one tool. Among a
150
- * group's candidates a rule takes first those the fewest later RELATION rules
151
- * could take, then by name, then by id (§3.2), so a general rule leaves the
152
- * particular items for a narrower rule after it. A SUITABILITY rule takes
153
- * nothing, so its place in the array changes no result.
154
- */
155
- export declare function evaluateManifestRules<F>(input: EvaluateManifestRulesInput<F>): IManifestRuleResult[];
156
- /** How many rules failed: a check's `computed.rulesFailed`. */
157
- export declare const rulesFailed: (results?: IManifestRuleResult[]) => number;
158
- /** The level-2 rules that failed, by name: what a check's verdict counts. */
159
- export declare const failedLevel2: (results?: IManifestRuleResult[]) => string[];
160
- /** A readiness issue a failed rule raises, before it is given to an asset. */
161
- export interface ManifestRuleIssue {
162
- ruleName: string;
163
- kind: ManifestRuleKind;
164
- level: ManifestRuleLevel;
165
- reason: string;
166
- missingFields?: string[];
167
- /** The item the issue names, where it names one. */
168
- itemId?: string;
169
- itemName?: string;
170
- }
171
- /**
172
- * The issues failed rules raise (§5.2):
173
- * - one per short group of a RELATION rule, its message rendered against the
174
- * group's first unmatched item, and one per `from` item with no value in a
175
- * `match` field;
176
- * - one per unsuitable item, naming it;
177
- * - one per triggered READINESS rule;
178
- * - one for a rule that could not be applied at all.
179
- */
180
- export declare function ruleIssues(results?: IManifestRuleResult[]): ManifestRuleIssue[];
181
- /**
182
- * The field names a rule reads from the ITEMS found: its `match` fields, the
183
- * names in its filters, and a SUITABILITY rule's expression. A change to one
184
- * of these on a member, or on a member's type, is what makes a kit's rules
185
- * worth evaluating again. A READINESS rule reads the check, not the items,
186
- * and gives none.
187
- */
188
- export declare function ruleItemReads<F>(rule: IManifestRule, engine: Pick<RuleEngine<F>, 'ReferencedFields'>): string[];
189
- export {};
1
+ /**
2
+ * Manifest rules: the one evaluator (MANIFEST_RULES.md, MANIFEST_IMPLEMENTATION.md §4).
3
+ *
4
+ * A manifest's lines count what was found; its rules judge it. This module
5
+ * evaluates the rules over a set of found items and, for READINESS rules, over
6
+ * a check's answers. It is shared by everything that has to reach the same
7
+ * result from the same items: the snapshot write, the kit recompute, and the
8
+ * client's check screen.
9
+ *
10
+ * Two things it does NOT have, by design, so that it can run in all three:
11
+ *
12
+ * - **No database.** The caller loads the items and says which type each rule
13
+ * names. What "the items found" are is the caller's decision too (at a check,
14
+ * the entries the lines count; between checks, what the kit holds).
15
+ * - **No expression engine of its own.** Expressions go through `@spotto/excelexp`,
16
+ * the engine behind readiness, and the caller hands it in (`RuleEngine`). The
17
+ * API and the client each have their own copy of that engine, at their own
18
+ * version, and a field value in their own stored form; the caller turns a
19
+ * value into the engine's form and this module never looks inside one.
20
+ *
21
+ * What it decides, so that no two callers can decide it differently:
22
+ *
23
+ * - **Order is by code unit.** Names are compared as plain `<` compares
24
+ * strings, then ids, never by locale. A device set to another language and
25
+ * the server must take the same items, because which `to` items a rule takes
26
+ * decides which later rule comes out short.
27
+ * - **`match` compares stored values.** Two items match on a field when they
28
+ * hold the same stored value of the same kind. Text `18` and text `18.0` are
29
+ * different values, though a filter's `==` would read both as the number 18.
30
+ * - **An expression follows a readiness rule's missing-value policy.** Text
31
+ * with no value reads as `''`, a yes/no as `false`; a number or a date with
32
+ * no value fails the rule closed, as "Missing data: …", unless the
33
+ * expression tests it with `ISSET()` or `ISBLANK()`.
34
+ * - **A filter (`where`) is different.** It chooses, it does not judge: an item
35
+ * with no value in a name the filter reads is left out, unless the filter
36
+ * tests that name with `ISSET()` or `ISBLANK()`.
37
+ */
38
+ import { IManifestRule, IManifestRuleResult, ManifestRuleKind, ManifestRuleLevel, ManifestRuleValue } from '@spotto/contract';
39
+ /**
40
+ * The expression engine, as `@spotto/excelexp` exports it: pass the module
41
+ * itself. `F` is its field value, which this module only passes through.
42
+ */
43
+ export interface RuleEngine<F> {
44
+ EvaluateCondition(fields: F[], condition: string, now: Date, zone: string): boolean;
45
+ FormatMessage(template: string, fields: F[], zone: string): {
46
+ rendered: string;
47
+ };
48
+ ReferencedFields(expression: string): string[];
49
+ PresenceGuardedFields(expression: string): string[];
50
+ TemplateFields(template: string): string[];
51
+ newFieldValue(init: {
52
+ Name: string;
53
+ Type: number;
54
+ StringValue?: string;
55
+ BoolValue?: boolean;
56
+ }): F;
57
+ FieldType: {
58
+ String: number;
59
+ Bool: number;
60
+ Checkboxes: number;
61
+ };
62
+ }
63
+ export declare type RuleDataType = 'STRING' | 'INTEGER' | 'DECIMAL' | 'BOOLEAN' | 'DATE' | 'DATETIME';
64
+ /** A stored value, for `match` and as evidence. A multi-select is its ticked options. */
65
+ export declare type RuleStoredValue = string | number | boolean | string[];
66
+ /** What a subject holds under one name. */
67
+ export interface RuleField<F> {
68
+ /** The field's data type, where the subject defines the name at all. */
69
+ dataType?: RuleDataType;
70
+ /** A multi-select: its entries are a set, and an empty set is not missing data. */
71
+ checkboxes?: boolean;
72
+ /**
73
+ * The value in the engine's form: one entry, or one per ticked option for a
74
+ * multi-select. Empty when the subject has no value under the name; an
75
+ * empty multi-select may be given empty too, and still resolves.
76
+ */
77
+ fields: F[];
78
+ /** The value as stored. Undefined when there is none. */
79
+ stored?: RuleStoredValue;
80
+ }
81
+ /**
82
+ * Something an expression is evaluated over: a found item, or a check (its
83
+ * answers over the checked asset's values and the context).
84
+ */
85
+ export interface RuleSubject<F> {
86
+ /** The subject's EFFECTIVE value under a name: its own, with its type's
87
+ * type-level value filling in where it has none. */
88
+ field(name: string): RuleField<F>;
89
+ }
90
+ /** A found item. */
91
+ export interface RuleItem<F> extends RuleSubject<F> {
92
+ assetId: string;
93
+ name: string;
94
+ /** The item's type path. An item is of a type, or beneath it, when its path
95
+ * starts with that type's path. */
96
+ typePath: string;
97
+ }
98
+ export interface EvaluateManifestRulesInput<F> {
99
+ /** The manifest's `rules`, in order. */
100
+ rules: IManifestRule[];
101
+ /** The items found. */
102
+ items: RuleItem<F>[];
103
+ /** What READINESS rules read, or `null` to leave them unevaluated. */
104
+ env: RuleSubject<F> | null;
105
+ /** A type's path by its id. Undefined when the type no longer exists: a
106
+ * rule that names it then fails with that reason, rather than silently
107
+ * never applying. */
108
+ typePath: (typeId: string) => string | undefined;
109
+ engine: RuleEngine<F>;
110
+ /** For the engine: the moment of the evaluation, and the organisation's zone. */
111
+ now: Date;
112
+ timezone: string;
113
+ }
114
+ /** Plain code-unit order: what `<` gives two strings, on every runtime. */
115
+ export declare const compareCodeUnits: (a: string, b: string) => number;
116
+ declare type Context<F> = Pick<EvaluateManifestRulesInput<F>, 'engine' | 'now' | 'timezone' | 'typePath'>;
117
+ /**
118
+ * Does the subject pass the filter? A filter chooses; it does not judge. So
119
+ * a subject with no value in a name the filter reads is left out, before the
120
+ * expression runs, unless the filter tests that name's presence itself. A
121
+ * filter that cannot be read or evaluated leaves the subject out.
122
+ */
123
+ export declare function passesFilter<F>(subject: RuleSubject<F>, where: string | undefined, ctx: Pick<Context<F>, 'engine' | 'now' | 'timezone'>): boolean;
124
+ export interface RuleJudgement {
125
+ /** The expression describes the problem: fired means there is one. */
126
+ fired: boolean;
127
+ reason?: string;
128
+ /** Names the expression reads that the subject has no value for. */
129
+ missingFields?: string[];
130
+ /** What the expression read, as stored. */
131
+ values: Record<string, ManifestRuleValue>;
132
+ }
133
+ /**
134
+ * A message rendered over a subject, as a readiness rule's is: a missing
135
+ * number or date reads "(not set)", missing text reads as nothing.
136
+ */
137
+ export declare function renderMessage<F>(subject: RuleSubject<F>, message: string, ctx: Pick<Context<F>, 'engine' | 'timezone'>): string;
138
+ /**
139
+ * Judge one subject with an expression that describes a problem, exactly as a
140
+ * readiness rule on a type is judged (`readiness/evaluate.ts`): it fires when
141
+ * the expression is true, when a number or a date it reads is missing and
142
+ * unguarded, and when it cannot be parsed or evaluated.
143
+ */
144
+ export declare function judge<F>(subject: RuleSubject<F>, expression: string, message: string, ctx: Pick<Context<F>, 'engine' | 'now' | 'timezone'>): RuleJudgement;
145
+ /**
146
+ * Evaluate a manifest's rules: one result per rule, in the rules' order.
147
+ *
148
+ * RELATION rules run as one pass in array order, and a `to` item one rule
149
+ * takes stays taken for every later rule: a battery serves one tool. Among a
150
+ * group's candidates a rule takes first those the fewest later RELATION rules
151
+ * could take, then by name, then by id (§3.2), so a general rule leaves the
152
+ * particular items for a narrower rule after it. A SUITABILITY rule takes
153
+ * nothing, so its place in the array changes no result.
154
+ */
155
+ export declare function evaluateManifestRules<F>(input: EvaluateManifestRulesInput<F>): IManifestRuleResult[];
156
+ /** How many rules failed: a check's `computed.rulesFailed`. */
157
+ export declare const rulesFailed: (results?: IManifestRuleResult[]) => number;
158
+ /** The level-2 rules that failed, by name: what a check's verdict counts. */
159
+ export declare const failedLevel2: (results?: IManifestRuleResult[]) => string[];
160
+ /** A readiness issue a failed rule raises, before it is given to an asset. */
161
+ export interface ManifestRuleIssue {
162
+ ruleName: string;
163
+ kind: ManifestRuleKind;
164
+ level: ManifestRuleLevel;
165
+ reason: string;
166
+ missingFields?: string[];
167
+ /** The item the issue names, where it names one. */
168
+ itemId?: string;
169
+ itemName?: string;
170
+ }
171
+ /**
172
+ * The issues failed rules raise (§5.2):
173
+ * - one per short group of a RELATION rule, its message rendered against the
174
+ * group's first unmatched item, and one per `from` item with no value in a
175
+ * `match` field;
176
+ * - one per unsuitable item, naming it;
177
+ * - one per triggered READINESS rule;
178
+ * - one for a rule that could not be applied at all.
179
+ */
180
+ export declare function ruleIssues(results?: IManifestRuleResult[]): ManifestRuleIssue[];
181
+ /**
182
+ * The field names a rule reads from the ITEMS found: its `match` fields, the
183
+ * names in its filters, and a SUITABILITY rule's expression. A change to one
184
+ * of these on a member, or on a member's type, is what makes a kit's rules
185
+ * worth evaluating again. A READINESS rule reads the check, not the items,
186
+ * and gives none.
187
+ */
188
+ export declare function ruleItemReads<F>(rule: IManifestRule, engine: Pick<RuleEngine<F>, 'ReferencedFields'>): string[];
189
+ export {};