@grafctory/blue 0.0.0-stage → 0.15.1
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.
- package/LICENSE +201 -0
- package/README.md +509 -2
- package/dist/boolean/assembler.d.ts +53 -0
- package/dist/boolean/assembler.d.ts.map +1 -0
- package/dist/boolean/assembler.js +78 -0
- package/dist/boolean/assembler.js.map +1 -0
- package/dist/boolean/index.d.ts +86 -0
- package/dist/boolean/index.d.ts.map +1 -0
- package/dist/boolean/index.js +99 -0
- package/dist/boolean/index.js.map +1 -0
- package/dist/boolean/validator.d.ts +30 -0
- package/dist/boolean/validator.d.ts.map +1 -0
- package/dist/boolean/validator.js +68 -0
- package/dist/boolean/validator.js.map +1 -0
- package/dist/dictionary/assembler.d.ts +70 -0
- package/dist/dictionary/assembler.d.ts.map +1 -0
- package/dist/dictionary/assembler.js +154 -0
- package/dist/dictionary/assembler.js.map +1 -0
- package/dist/dictionary/index.d.ts +211 -0
- package/dist/dictionary/index.d.ts.map +1 -0
- package/dist/dictionary/index.js +43 -0
- package/dist/dictionary/index.js.map +1 -0
- package/dist/dictionary/inference.d.ts +42 -0
- package/dist/dictionary/inference.d.ts.map +1 -0
- package/dist/dictionary/inference.js +17 -0
- package/dist/dictionary/inference.js.map +1 -0
- package/dist/dictionary/validator.d.ts +34 -0
- package/dist/dictionary/validator.d.ts.map +1 -0
- package/dist/dictionary/validator.js +91 -0
- package/dist/dictionary/validator.js.map +1 -0
- package/dist/index.core.d.ts +33 -0
- package/dist/index.core.d.ts.map +1 -0
- package/dist/index.core.js +149 -0
- package/dist/index.core.js.map +1 -0
- package/dist/index.d.ts +333 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +378 -0
- package/dist/index.js.map +1 -0
- package/dist/number/assembler.d.ts +70 -0
- package/dist/number/assembler.d.ts.map +1 -0
- package/dist/number/assembler.js +188 -0
- package/dist/number/assembler.js.map +1 -0
- package/dist/number/index.d.ts +329 -0
- package/dist/number/index.d.ts.map +1 -0
- package/dist/number/index.js +312 -0
- package/dist/number/index.js.map +1 -0
- package/dist/number/validator.d.ts +31 -0
- package/dist/number/validator.d.ts.map +1 -0
- package/dist/number/validator.js +57 -0
- package/dist/number/validator.js.map +1 -0
- package/dist/reference/accessors.d.ts +29 -0
- package/dist/reference/accessors.d.ts.map +1 -0
- package/dist/reference/accessors.js +38 -0
- package/dist/reference/accessors.js.map +1 -0
- package/dist/reference/assembler.d.ts +63 -0
- package/dist/reference/assembler.d.ts.map +1 -0
- package/dist/reference/assembler.js +127 -0
- package/dist/reference/assembler.js.map +1 -0
- package/dist/reference/index.d.ts +108 -0
- package/dist/reference/index.d.ts.map +1 -0
- package/dist/reference/index.js +40 -0
- package/dist/reference/index.js.map +1 -0
- package/dist/reference/validator.d.ts +33 -0
- package/dist/reference/validator.d.ts.map +1 -0
- package/dist/reference/validator.js +56 -0
- package/dist/reference/validator.js.map +1 -0
- package/dist/resource/accessors.d.ts +140 -0
- package/dist/resource/accessors.d.ts.map +1 -0
- package/dist/resource/accessors.js +173 -0
- package/dist/resource/accessors.js.map +1 -0
- package/dist/resource/assembler.d.ts +265 -0
- package/dist/resource/assembler.d.ts.map +1 -0
- package/dist/resource/assembler.js +816 -0
- package/dist/resource/assembler.js.map +1 -0
- package/dist/resource/index.d.ts +898 -0
- package/dist/resource/index.d.ts.map +1 -0
- package/dist/resource/index.js +160 -0
- package/dist/resource/index.js.map +1 -0
- package/dist/resource/inference.d.ts +310 -0
- package/dist/resource/inference.d.ts.map +1 -0
- package/dist/resource/inference.js +17 -0
- package/dist/resource/inference.js.map +1 -0
- package/dist/resource/validator.d.ts +158 -0
- package/dist/resource/validator.d.ts.map +1 -0
- package/dist/resource/validator.js +1031 -0
- package/dist/resource/validator.js.map +1 -0
- package/dist/string/assembler.d.ts +69 -0
- package/dist/string/assembler.d.ts.map +1 -0
- package/dist/string/assembler.js +143 -0
- package/dist/string/assembler.js.map +1 -0
- package/dist/string/index.d.ts +431 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +528 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/validator.d.ts +30 -0
- package/dist/string/validator.d.ts.map +1 -0
- package/dist/string/validator.js +69 -0
- package/dist/string/validator.js.map +1 -0
- package/dist/union/accessors.d.ts +137 -0
- package/dist/union/accessors.d.ts.map +1 -0
- package/dist/union/accessors.js +253 -0
- package/dist/union/accessors.js.map +1 -0
- package/dist/union/assembler.d.ts +56 -0
- package/dist/union/assembler.d.ts.map +1 -0
- package/dist/union/assembler.js +140 -0
- package/dist/union/assembler.js.map +1 -0
- package/dist/union/index.d.ts +172 -0
- package/dist/union/index.d.ts.map +1 -0
- package/dist/union/index.js +45 -0
- package/dist/union/index.js.map +1 -0
- package/dist/union/inference.d.ts +30 -0
- package/dist/union/inference.d.ts.map +1 -0
- package/dist/union/inference.js +17 -0
- package/dist/union/inference.js.map +1 -0
- package/dist/union/validator.d.ts +94 -0
- package/dist/union/validator.d.ts.map +1 -0
- package/dist/union/validator.js +171 -0
- package/dist/union/validator.js.map +1 -0
- package/dist/value/accessors.d.ts +151 -0
- package/dist/value/accessors.d.ts.map +1 -0
- package/dist/value/accessors.js +468 -0
- package/dist/value/accessors.js.map +1 -0
- package/dist/value/assembler.d.ts +129 -0
- package/dist/value/assembler.d.ts.map +1 -0
- package/dist/value/assembler.js +248 -0
- package/dist/value/assembler.js.map +1 -0
- package/dist/value/index.d.ts +309 -0
- package/dist/value/index.d.ts.map +1 -0
- package/dist/value/index.js +26 -0
- package/dist/value/index.js.map +1 -0
- package/dist/value/inference.d.ts +292 -0
- package/dist/value/inference.d.ts.map +1 -0
- package/dist/value/inference.js +17 -0
- package/dist/value/inference.js.map +1 -0
- package/dist/value/validator.d.ts +76 -0
- package/dist/value/validator.d.ts.map +1 -0
- package/dist/value/validator.js +111 -0
- package/dist/value/validator.js.map +1 -0
- package/package.json +95 -4
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dictionary shape types and operations.
|
|
3
|
+
*
|
|
4
|
+
* Defines the shape describing localised text, stated once per language, together with its constraint types, and
|
|
5
|
+
* provides the {@link dictionary} factory stating them, mapping
|
|
6
|
+
* [JSON-LD language maps](https://www.w3.org/TR/json-ld11/#language-maps) to
|
|
7
|
+
* [RDF 1.1](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string) language-tagged strings. A property
|
|
8
|
+
* whose value varies by language takes one member rather than one per language.
|
|
9
|
+
*
|
|
10
|
+
* > [!IMPORTANT]
|
|
11
|
+
* > Contradictory constraints are rejected as the shape is built, so a shape that exists admits at least one value:
|
|
12
|
+
* > stating `minLength` above `maxLength` throws a {@link @grafctory/core!TraceError | TraceError}.
|
|
13
|
+
*
|
|
14
|
+
* How many strings a tag carries is fixed by {@link DictionaryConstraints.uniqueLang | uniqueLang}: a unique-tagged
|
|
15
|
+
* shape gives every tag a single string, and any other gives every tag an array. A member ranging over a dictionary
|
|
16
|
+
* carries the map whole, never in an array, whatever cardinality it states.
|
|
17
|
+
*
|
|
18
|
+
* The tag-keyed map is the form a value is stored and submitted in. A localised member additionally *coalesces* under
|
|
19
|
+
* language negotiation when retrieved, at the same per-tag arity: the value may be retrieved as the negotiated content
|
|
20
|
+
* alone, and filtered by a plain string operand matched against it under ordinary textual semantics. Submission never
|
|
21
|
+
* coalesces: it accepts the tag-keyed form alone.
|
|
22
|
+
*
|
|
23
|
+
* **Compatibility**
|
|
24
|
+
*
|
|
25
|
+
* | JSON | JSON-LD | RDF 1.1 |
|
|
26
|
+
* | -------------------- | ---------------------------------- | ------------------------------- |
|
|
27
|
+
* | tag-keyed string map | `@language`-container language map | language-tagged string literals |
|
|
28
|
+
*
|
|
29
|
+
* **Defining language-tagged shapes**
|
|
30
|
+
*
|
|
31
|
+
* Values are tag-keyed maps; the [`und`](https://iso639-3.sil.org/code/und) tag carries content of undetermined
|
|
32
|
+
* language (a proper name, say), and the [`zxx`](https://iso639-3.sil.org/code/zxx) tag content with no language at
|
|
33
|
+
* all (identifiers, codes, formulae):
|
|
34
|
+
*
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { dictionary } from '@grafctory/blue/dictionary';
|
|
37
|
+
*
|
|
38
|
+
* const label = dictionary({ uniqueLang: true }); // a single string per tag
|
|
39
|
+
* const keywords = dictionary(); // an array of strings per tag
|
|
40
|
+
* const name = dictionary({ minLength: 1, maxLength: 200 });// length-constrained
|
|
41
|
+
* const abstract = dictionary({ languageIn: ["en", "it"] });// language-restricted
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* **Using in resource shapes**
|
|
45
|
+
*
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { multiple, optional, required, resource } from '@grafctory/blue/resource';
|
|
48
|
+
* import { dictionary } from '@grafctory/blue/dictionary';
|
|
49
|
+
*
|
|
50
|
+
* const Article = resource({
|
|
51
|
+
* title: required(dictionary({ uniqueLang: true, minLength: 1 })),
|
|
52
|
+
* abstract: optional(dictionary({ uniqueLang: true })),
|
|
53
|
+
* keywords: multiple(dictionary({ languageIn: ["en", "fr", "de"] }))
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* A member carries one map whatever cardinality it states, so `keywords` above reads as a single map of string arrays
|
|
58
|
+
* and never as an array of maps.
|
|
59
|
+
*
|
|
60
|
+
* @module
|
|
61
|
+
*
|
|
62
|
+
* @see {@link https://www.w3.org/TR/json-ld11/#language-maps JSON-LD 1.1 § 9.8 Language Maps}
|
|
63
|
+
* @see {@link https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string RDF 1.1 § 3.3 Literals}
|
|
64
|
+
*/
|
|
65
|
+
import type { TagRange } from "@grafctory/core/language";
|
|
66
|
+
/**
|
|
67
|
+
* Describes a localised textual value.
|
|
68
|
+
*
|
|
69
|
+
* Admits the [JSON-LD language maps](https://www.w3.org/TR/json-ld11/#language-maps) a resource may carry: a map keyed
|
|
70
|
+
* by [BCP 47](https://www.rfc-editor.org/info/bcp47) language tag, holding the content the resource states in each
|
|
71
|
+
* language, so that a property whose value varies by language is described by a single shape rather than by one member
|
|
72
|
+
* per language. Content of undetermined language (a proper name, say) is keyed by the
|
|
73
|
+
* [`und`](https://iso639-3.sil.org/code/und) tag, and content belonging to no language at all (an identifier, a code, a
|
|
74
|
+
* formula) by the [`zxx`](https://iso639-3.sil.org/code/zxx) tag. Each entry is stored as a
|
|
75
|
+
* [RDF 1.1](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string) language-tagged string.
|
|
76
|
+
*
|
|
77
|
+
* How many strings a tag carries is fixed by {@link DictionaryConstraints.uniqueLang | uniqueLang}: a unique-tagged
|
|
78
|
+
* shape gives every tag a single string, and any other gives every tag an array. The map is a value set in its own
|
|
79
|
+
* right, so a member carries it whole, never in an array, whatever the bounds of the enclosing {@link value!Range}
|
|
80
|
+
* admit: those bounds count the values of the other branches of a two-natured range, which a resource carries in
|
|
81
|
+
* place of the map and never beside it.
|
|
82
|
+
*
|
|
83
|
+
* **Negotiation**
|
|
84
|
+
*
|
|
85
|
+
* The tag-keyed map is the form a value is stored and submitted in. On retrieval a localised member is additionally
|
|
86
|
+
* *coalesced* under language negotiation, at the same per-tag arity: the value may be retrieved as the negotiated
|
|
87
|
+
* content alone (a bare string under a unique-tagged shape, otherwise the array of strings the selected tag carries),
|
|
88
|
+
* and filtered by a plain string operand (a comparison, a text search or an option) matched against the negotiated
|
|
89
|
+
* content under ordinary textual semantics. Submission never coalesces: it accepts the tag-keyed form alone.
|
|
90
|
+
*
|
|
91
|
+
* **Inheritance**
|
|
92
|
+
*
|
|
93
|
+
* Where a {@link resource!ResourceShape} extends the shapes it lists as `parents`, localised members are merged
|
|
94
|
+
* according to the following rules. The *child* is the extending shape; the *parent* is the inherited one.
|
|
95
|
+
*
|
|
96
|
+
* | Field | Override Rule |
|
|
97
|
+
* | ------------ | -------------------------------------------------------------------------- |
|
|
98
|
+
* | `kind` | Cannot be overridden |
|
|
99
|
+
* | `minLength` | Child may only raise the minimum length |
|
|
100
|
+
* | `maxLength` | Child may only lower the maximum length |
|
|
101
|
+
* | `languageIn` | Child may only drop accepted ranges |
|
|
102
|
+
* | `uniqueLang` | Child may add but not drop; an inherited constraint always carries through |
|
|
103
|
+
*
|
|
104
|
+
* **Cross-field validation**
|
|
105
|
+
*
|
|
106
|
+
* - merged `minLength` must be ≤ merged `maxLength`
|
|
107
|
+
*
|
|
108
|
+
* @see {@link https://www.w3.org/TR/json-ld11/#language-maps JSON-LD 1.1 § 9.8 Language Maps}
|
|
109
|
+
* @see {@link https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string RDF 1.1 § 3.3 Literals}
|
|
110
|
+
*/
|
|
111
|
+
export type DictionaryShape = DictionaryConstraints & {
|
|
112
|
+
/**
|
|
113
|
+
* Discriminator identifying this as a dictionary shape.
|
|
114
|
+
*
|
|
115
|
+
* **Inheritance** — cannot be overridden.
|
|
116
|
+
*/
|
|
117
|
+
readonly kind: "dictionary";
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Constraints accepted by the {@link dictionary} shape factory.
|
|
121
|
+
*
|
|
122
|
+
* Bounds the content a tag may carry and the languages the map may key it by. Length bounds are matched against each
|
|
123
|
+
* string separately, so a value is held to them whatever language it is stated in.
|
|
124
|
+
*
|
|
125
|
+
* @see {@link https://www.w3.org/TR/shacl/#core-components-string SHACL § 4.4 String-based Constraint Components}
|
|
126
|
+
*/
|
|
127
|
+
export type DictionaryConstraints = {
|
|
128
|
+
/**
|
|
129
|
+
* Restricts every tag to a single string.
|
|
130
|
+
*
|
|
131
|
+
* When `true`, a tag carries a single string, so that a member stating several values spreads them across languages
|
|
132
|
+
* rather than stacking them under one. Fixes the arity the content is resolved at: a bare string under every tag
|
|
133
|
+
* where the constraint is stated, an array of strings under every tag otherwise.
|
|
134
|
+
*
|
|
135
|
+
* **Inheritance** — a child may add the constraint but not drop it: overriding a unique-tagged parent with a
|
|
136
|
+
* non-unique-tagged child is rejected.
|
|
137
|
+
*
|
|
138
|
+
* @defaultValue `undefined` (several strings admitted per tag)
|
|
139
|
+
*
|
|
140
|
+
* @see {@link https://www.w3.org/TR/shacl/#UniqueLangConstraintComponent SHACL § 4.4.5 sh:uniqueLang}
|
|
141
|
+
*/
|
|
142
|
+
readonly uniqueLang?: boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Minimum string length in characters.
|
|
145
|
+
*
|
|
146
|
+
* **Inheritance** — child may only raise the minimum length.
|
|
147
|
+
*
|
|
148
|
+
* @defaultValue `undefined` (no minimum length)
|
|
149
|
+
*
|
|
150
|
+
* @see {@link https://www.w3.org/TR/shacl/#MinLengthConstraintComponent SHACL § 4.4.1 sh:minLength}
|
|
151
|
+
*/
|
|
152
|
+
readonly minLength?: number;
|
|
153
|
+
/**
|
|
154
|
+
* Maximum string length in characters.
|
|
155
|
+
*
|
|
156
|
+
* **Inheritance** — child may only lower the maximum length.
|
|
157
|
+
*
|
|
158
|
+
* @defaultValue `undefined` (no maximum length)
|
|
159
|
+
*
|
|
160
|
+
* @see {@link https://www.w3.org/TR/shacl/#MaxLengthConstraintComponent SHACL § 4.4.2 sh:maxLength}
|
|
161
|
+
*/
|
|
162
|
+
readonly maxLength?: number;
|
|
163
|
+
/**
|
|
164
|
+
* Allowed language ranges.
|
|
165
|
+
*
|
|
166
|
+
* When specified, every tag the map is keyed by must match one of the given {@link TagRange} values under RFC 4647
|
|
167
|
+
* basic filtering. Each range is a basic language range (a sequence of subtags or the standalone `*` wildcard);
|
|
168
|
+
* extended ranges such as `en-*` are not accepted. Empty arrays are ignored. Language *tags*, which identify a
|
|
169
|
+
* language rather than select one, are described by {@link string!tag | tag} instead.
|
|
170
|
+
*
|
|
171
|
+
* **Inheritance** — child may only drop accepted ranges.
|
|
172
|
+
*
|
|
173
|
+
* @defaultValue `undefined` (no language constraint)
|
|
174
|
+
*
|
|
175
|
+
* @see {@link https://www.w3.org/TR/shacl/#LanguageInConstraintComponent SHACL § 4.4.4 sh:languageIn}
|
|
176
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc4647.html RFC 4647 - Matching of Language Tags}
|
|
177
|
+
*/
|
|
178
|
+
readonly languageIn?: readonly TagRange[];
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Creates a dictionary shape.
|
|
182
|
+
*
|
|
183
|
+
* Contradictory constraints are rejected as the shape is built, so a shape that exists admits at least one value.
|
|
184
|
+
*
|
|
185
|
+
* @typeParam C The stated constraints
|
|
186
|
+
*
|
|
187
|
+
* @param constraints Optional shape {@link DictionaryConstraints constraints}
|
|
188
|
+
*
|
|
189
|
+
* @returns An immutable shape admitting the language maps the constraints bound, carrying a single string per tag where
|
|
190
|
+
* they state {@link DictionaryConstraints.uniqueLang | uniqueLang}
|
|
191
|
+
*
|
|
192
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
*
|
|
196
|
+
* ```typescript
|
|
197
|
+
* const label = required(dictionary({ uniqueLang: true }));
|
|
198
|
+
* const description = optional(dictionary({ languageIn: ["en", "it"] }));
|
|
199
|
+
* const keywords = multiple(dictionary({ languageIn: ["en"] }));
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
export declare function dictionary<const C extends DictionaryConstraints = {}>(constraints?: C): DictionaryShape & {
|
|
203
|
+
/**
|
|
204
|
+
* Restricts every tag to a single string, exactly as stated.
|
|
205
|
+
*
|
|
206
|
+
* Carries the constraint at the value it was given rather than at the whole boolean domain, so that the content of
|
|
207
|
+
* a tag is typed at the arity the shape admits.
|
|
208
|
+
*/
|
|
209
|
+
readonly uniqueLang: C["uniqueLang"];
|
|
210
|
+
};
|
|
211
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dictionary/index.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAKzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,MAAM,eAAe,GAAG,qBAAqB,GAAG;IAErD;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;CAE3B,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAEnC;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;IAE9B;;;;;;;;OAQG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAE5B;;;;;;;;OAQG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAE5B;;;;;;;;;;;;;;OAcG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;CAE1C,CAAA;AAKD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,UAAU,CAAC,KAAK,CAAC,CAAC,SAAS,qBAAqB,GAAG,EAAE,EAAE,WAAW,CAAC,EAAE,CAAC,GAAG,eAAe,GAAG;IAE1G;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,YAAY,CAAC,CAAA;CAEpC,CAIA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { assemble } from "./assembler.js";
|
|
17
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
18
|
+
/**
|
|
19
|
+
* Creates a dictionary shape.
|
|
20
|
+
*
|
|
21
|
+
* Contradictory constraints are rejected as the shape is built, so a shape that exists admits at least one value.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam C The stated constraints
|
|
24
|
+
*
|
|
25
|
+
* @param constraints Optional shape {@link DictionaryConstraints constraints}
|
|
26
|
+
*
|
|
27
|
+
* @returns An immutable shape admitting the language maps the constraints bound, carrying a single string per tag where
|
|
28
|
+
* they state {@link DictionaryConstraints.uniqueLang | uniqueLang}
|
|
29
|
+
*
|
|
30
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const label = required(dictionary({ uniqueLang: true }));
|
|
36
|
+
* const description = optional(dictionary({ languageIn: ["en", "it"] }));
|
|
37
|
+
* const keywords = multiple(dictionary({ languageIn: ["en"] }));
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function dictionary(constraints) {
|
|
41
|
+
return assemble(constraints ?? {});
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/dictionary/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAqEH,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA+H1C,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,UAAU,CAA6C,WAAe;IAYrF,OAAO,QAAQ,CAAkB,WAAW,IAAI,EAAE,CAAC,CAAC;AAErD,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dictionary value inference.
|
|
3
|
+
*
|
|
4
|
+
* Types every form a localised value takes from the dictionary shape that describes it: the tag-keyed map the value
|
|
5
|
+
* is stored and submitted as, and the negotiated text a retrieval returns in its place. Both forms follow the per-tag
|
|
6
|
+
* arity the shape admits, so stored and retrieved text never disagree on how many strings a tag carries.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { Eager, Lazy } from "@grafctory/core";
|
|
11
|
+
import type { Tag } from "@grafctory/core/language";
|
|
12
|
+
import type { DictionaryShape } from "./index.js";
|
|
13
|
+
/**
|
|
14
|
+
* Resolves the tag-keyed map a dictionary shape describes.
|
|
15
|
+
*
|
|
16
|
+
* Yields a map keyed by language tag, carrying a single string under each tag where the shape states
|
|
17
|
+
* {@link DictionaryConstraints.uniqueLang | uniqueLang} and an array of strings under each tag otherwise, so that the
|
|
18
|
+
* content of a tag is typed at the arity the shape admits. A shape leaving the constraint unstated, or stating it only
|
|
19
|
+
* as a boolean, admits several strings under each tag.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam S The describing shape, possibly deferred to break definition cycles
|
|
22
|
+
*/
|
|
23
|
+
export type Tagged<S extends Lazy<DictionaryShape>> = {
|
|
24
|
+
readonly [tag: Tag]: Eager<S> extends {
|
|
25
|
+
readonly uniqueLang: true;
|
|
26
|
+
} ? string : readonly string[];
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Resolves the negotiated text a dictionary shape coalesces to.
|
|
30
|
+
*
|
|
31
|
+
* A localised member asked for by the atomic placeholder comes back as plain text: the content of the one tag that
|
|
32
|
+
* language negotiation selects. The text is a single string where the shape states
|
|
33
|
+
* {@link DictionaryConstraints.uniqueLang | uniqueLang}, and an array of strings otherwise, matching the per-tag arity
|
|
34
|
+
* {@link Tagged} states for the map. A shape that leaves the constraint unstated, or states it only as a boolean, may
|
|
35
|
+
* carry several strings under a tag, so its text is always an array, however many strings the selected tag holds.
|
|
36
|
+
*
|
|
37
|
+
* @typeParam S The describing shape, possibly deferred to break definition cycles
|
|
38
|
+
*/
|
|
39
|
+
export type Coalesced<S extends Lazy<DictionaryShape>> = Eager<S> extends {
|
|
40
|
+
readonly uniqueLang: true;
|
|
41
|
+
} ? string : readonly string[];
|
|
42
|
+
//# sourceMappingURL=inference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inference.d.ts","sourceRoot":"","sources":["../../src/dictionary/inference.ts"],"names":[],"mappings":"AAiBA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,KAAK,EAAyB,eAAe,EAAE,MAAM,YAAY,CAAC;AAEzE;;;;;;;;;GASG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,IAAI,CAAC,eAAe,CAAC,IAAI;IAErD,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAA;KAAE,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAA;CAEhG,CAAA;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,IAAI,CAAC,eAAe,CAAC,IACpD,KAAK,CAAC,CAAC,CAAC,SAAS;IAAE,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAA;CAAE,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=inference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inference.js","sourceRoot":"","sources":["../../src/dictionary/inference.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dictionary value validation.
|
|
3
|
+
*
|
|
4
|
+
* Holds a value to what a dictionary shape admits and reports everything wrong with it at once, keyed by the tag at
|
|
5
|
+
* fault, so that a caller may tell which language failed and why.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { type Optional } from "@grafctory/core";
|
|
10
|
+
import { type Trace } from "@grafctory/core/trace";
|
|
11
|
+
import type { Scope } from "../value/validator.js";
|
|
12
|
+
import { type DictionaryShape } from "./index.js";
|
|
13
|
+
/**
|
|
14
|
+
* Validates values against a dictionary shape.
|
|
15
|
+
*
|
|
16
|
+
* Reports a value that is not a language map as a `{kind}` violation, and each entry that breaks a constraint under
|
|
17
|
+
* its own facet, keyed by the tag it is stated under, so that a caller may tell which language failed and why. A tag
|
|
18
|
+
* carries a single string where the shape states
|
|
19
|
+
* {@link dictionary!DictionaryConstraints.uniqueLang | uniqueLang} and an array of strings otherwise; a value stated at
|
|
20
|
+
* the other arity is rejected as a malformed map rather than as a broken constraint.
|
|
21
|
+
*
|
|
22
|
+
* @param values The values to validate
|
|
23
|
+
* @param shape The shape the values are matched against
|
|
24
|
+
* @param opts Validation options
|
|
25
|
+
* @param opts.scope The {@link Scope | strictness} the shape is enforced at, defaulting to `"state"`. A `"model"`
|
|
26
|
+
* placeholder asks for the text either coalesced, as the atomic placeholder, or per tag, as a map of the language
|
|
27
|
+
* ranges wanted. The map is keyed by range, the `*` wildcard included, where a value is keyed by tag
|
|
28
|
+
*
|
|
29
|
+
* @returns A trace of the violations found, or `undefined` where every value matches `shape`
|
|
30
|
+
*/
|
|
31
|
+
export declare function validateDictionary(values: readonly unknown[], shape: DictionaryShape, { scope }?: {
|
|
32
|
+
scope?: Scope;
|
|
33
|
+
}): Optional<Trace>;
|
|
34
|
+
//# sourceMappingURL=validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/dictionary/validator.ts"],"names":[],"mappings":"AAiBA;;;;;;;GAOG;AAEH,OAAO,EAA4C,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE1F,OAAO,EAA0C,KAAK,KAAK,EAAkB,MAAM,uBAAuB,CAAC;AAE3G,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAGlD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,EAAE,KAAK,EAAE,eAAe,EAAE,EAEtF,KAAe,EAEf,GAAE;IAEF,KAAK,CAAC,EAAE,KAAK,CAAA;CAER,GAAG,QAAQ,CAAC,KAAK,CAAC,CAuGvB"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Dictionary value validation.
|
|
18
|
+
*
|
|
19
|
+
* Holds a value to what a dictionary shape admits and reports everything wrong with it at once, keyed by the tag at
|
|
20
|
+
* fault, so that a caller may tell which language failed and why.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { isArray, isObject, isString, opt as fold } from "@grafctory/core";
|
|
25
|
+
import { isTag, isTagRange, matchTag } from "@grafctory/core/language";
|
|
26
|
+
import { all, array, fail, length, object, pass } from "@grafctory/core/trace";
|
|
27
|
+
import { isAtomic } from "@grafctory/qest/model";
|
|
28
|
+
/**
|
|
29
|
+
* Validates values against a dictionary shape.
|
|
30
|
+
*
|
|
31
|
+
* Reports a value that is not a language map as a `{kind}` violation, and each entry that breaks a constraint under
|
|
32
|
+
* its own facet, keyed by the tag it is stated under, so that a caller may tell which language failed and why. A tag
|
|
33
|
+
* carries a single string where the shape states
|
|
34
|
+
* {@link dictionary!DictionaryConstraints.uniqueLang | uniqueLang} and an array of strings otherwise; a value stated at
|
|
35
|
+
* the other arity is rejected as a malformed map rather than as a broken constraint.
|
|
36
|
+
*
|
|
37
|
+
* @param values The values to validate
|
|
38
|
+
* @param shape The shape the values are matched against
|
|
39
|
+
* @param opts Validation options
|
|
40
|
+
* @param opts.scope The {@link Scope | strictness} the shape is enforced at, defaulting to `"state"`. A `"model"`
|
|
41
|
+
* placeholder asks for the text either coalesced, as the atomic placeholder, or per tag, as a map of the language
|
|
42
|
+
* ranges wanted. The map is keyed by range, the `*` wildcard included, where a value is keyed by tag
|
|
43
|
+
*
|
|
44
|
+
* @returns A trace of the violations found, or `undefined` where every value matches `shape`
|
|
45
|
+
*/
|
|
46
|
+
export function validateDictionary(values, shape, { scope = "state" } = {}) {
|
|
47
|
+
switch (scope) {
|
|
48
|
+
case "state":
|
|
49
|
+
return state(shape)(values);
|
|
50
|
+
case "model":
|
|
51
|
+
return model(shape)(values);
|
|
52
|
+
}
|
|
53
|
+
function state({ uniqueLang, minLength, maxLength, languageIn }) {
|
|
54
|
+
return tagged(uniqueLang, isTag, "invalid tag", (tag, content) => all(length(minLength, maxLength), accepts(tag, languageIn))(content), (tag, content) => all(array(length(minLength, maxLength)), accepts(tag, languageIn))(content));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The form alone: a localised member is asked for coalesced, as the atomic placeholder, or structurally, as a map
|
|
58
|
+
* naming the tag ranges wanted. Per-tag arity follows the shape, so a map entry asks for the value and no more.
|
|
59
|
+
*/
|
|
60
|
+
function model({}) {
|
|
61
|
+
return array((value) => isAtomic(value) ? undefined
|
|
62
|
+
: !isObject(value) ? ["{kind} expected <Atomic> or <Locale> placeholder"]
|
|
63
|
+
: object(([range, asked]) => !isTagRange(range) ? [{ [range]: ["invalid tag range"] }]
|
|
64
|
+
: isAtomic(asked) ? undefined
|
|
65
|
+
: [{ [range]: ["{type} expected <Atomic> value"] }])(value));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Builds a validator enforcing the form of every language map in a set, holding each entry to the stated arity.
|
|
69
|
+
*
|
|
70
|
+
* Reports a value that is not a language map as a `{kind}` violation, and a key `admits` turns down or an entry
|
|
71
|
+
* stated at the other per-tag arity as an entry violation keyed by the key; every violation is keyed by the index
|
|
72
|
+
* of the map carrying it in the validated set. A surviving entry is handed to `unique` or to `stacked`, according
|
|
73
|
+
* to the arity the shape states; left out, either admits whatever the form itself admits.
|
|
74
|
+
*/
|
|
75
|
+
function tagged(uniqueLang, admits, invalid, unique = () => undefined, stacked = () => undefined) {
|
|
76
|
+
return array((value) => !isObject(value) ? ["{kind} expected <dictionary> value"]
|
|
77
|
+
: object(([tag, text]) => !admits(tag) ? [{ [tag]: [invalid] }]
|
|
78
|
+
: uniqueLang === true
|
|
79
|
+
? isString(text)
|
|
80
|
+
? fold(unique(tag, text), trace => [{ [tag]: trace }])
|
|
81
|
+
: [{ [tag]: ["expected string value"] }]
|
|
82
|
+
: isArray(text, isString)
|
|
83
|
+
? fold(stacked(tag, text), trace => [{ [tag]: trace }])
|
|
84
|
+
: [{ [tag]: ["expected string array value"] }])(value));
|
|
85
|
+
}
|
|
86
|
+
function accepts(tag, languageIn) {
|
|
87
|
+
return languageIn === undefined || languageIn.some(range => matchTag(tag, range)) ? pass
|
|
88
|
+
: fail([`{languageIn} unsupported tag for allowed languages [${languageIn.join(", ")}]`]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/dictionary/validator.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ResourceShape } from "./resource/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Applies retrieval policies to a validated template.
|
|
4
|
+
*
|
|
5
|
+
* Returns the template the server will actually honour, so that a caller may hold a request to what the service
|
|
6
|
+
* guarantees rather than to what the client happened to ask for. The template is rewritten where a policy requires it
|
|
7
|
+
* and returned unchanged everywhere else, keeping what the client asked for intact; a template stating no policy at
|
|
8
|
+
* all comes back as it went in.
|
|
9
|
+
*
|
|
10
|
+
* **Default paging**
|
|
11
|
+
*
|
|
12
|
+
* A collection left unpaged is given the largest page the service serves, so that a client omitting `#` is handed a
|
|
13
|
+
* bounded set rather than the whole of it. A collection already stating a page of its own is left alone, as is a
|
|
14
|
+
* single-valued or localised slot, which nothing pages, and a projection column, which holds one value per row and so
|
|
15
|
+
* reaches no collection of its own.
|
|
16
|
+
*
|
|
17
|
+
* The template is expected to have been validated against the shape: a malformed one is returned as it stands rather
|
|
18
|
+
* than reported.
|
|
19
|
+
*
|
|
20
|
+
* @param value The validated template to enforce
|
|
21
|
+
* @param shape The shape the template was validated against
|
|
22
|
+
* @param opts Enforcement options, each applied on its own
|
|
23
|
+
* @param opts.limit The page every unpaged collection is held to; omitted, or `0`, leaves paging to the client
|
|
24
|
+
*
|
|
25
|
+
* @returns A template structurally equivalent to `value`, rewritten where a policy requires it
|
|
26
|
+
*
|
|
27
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `shape` reaches itself through a cycle no link
|
|
28
|
+
* breaks, or where a union it reaches declares a shared member name inconsistently across its branches
|
|
29
|
+
*/
|
|
30
|
+
export declare function enforce(value: unknown, shape: ResourceShape, { limit }?: {
|
|
31
|
+
readonly limit?: number;
|
|
32
|
+
}): unknown;
|
|
33
|
+
//# sourceMappingURL=index.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.core.d.ts","sourceRoot":"","sources":["../src/index.core.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAKzD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,EAE7D,KAAK,EAEL,GAAE;IAEF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAElB,GAAG,OAAO,CAgIf"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Retrieval policy enforcement.
|
|
18
|
+
*
|
|
19
|
+
* Holds a validated template to what the service will actually serve: {@link enforce} rewrites the template where a
|
|
20
|
+
* policy requires it and leaves it as it stands everywhere else.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { isIdentifier, isObject } from "@grafctory/core";
|
|
25
|
+
import { decodeProbe, isBinding, isBranch, isUnion } from "@grafctory/qest/model";
|
|
26
|
+
import { getShapeTarget } from "./reference/index.js";
|
|
27
|
+
import { getShapeBranches } from "./union/index.js";
|
|
28
|
+
import { effective } from "./value/index.js";
|
|
29
|
+
/**
|
|
30
|
+
* Applies retrieval policies to a validated template.
|
|
31
|
+
*
|
|
32
|
+
* Returns the template the server will actually honour, so that a caller may hold a request to what the service
|
|
33
|
+
* guarantees rather than to what the client happened to ask for. The template is rewritten where a policy requires it
|
|
34
|
+
* and returned unchanged everywhere else, keeping what the client asked for intact; a template stating no policy at
|
|
35
|
+
* all comes back as it went in.
|
|
36
|
+
*
|
|
37
|
+
* **Default paging**
|
|
38
|
+
*
|
|
39
|
+
* A collection left unpaged is given the largest page the service serves, so that a client omitting `#` is handed a
|
|
40
|
+
* bounded set rather than the whole of it. A collection already stating a page of its own is left alone, as is a
|
|
41
|
+
* single-valued or localised slot, which nothing pages, and a projection column, which holds one value per row and so
|
|
42
|
+
* reaches no collection of its own.
|
|
43
|
+
*
|
|
44
|
+
* The template is expected to have been validated against the shape: a malformed one is returned as it stands rather
|
|
45
|
+
* than reported.
|
|
46
|
+
*
|
|
47
|
+
* @param value The validated template to enforce
|
|
48
|
+
* @param shape The shape the template was validated against
|
|
49
|
+
* @param opts Enforcement options, each applied on its own
|
|
50
|
+
* @param opts.limit The page every unpaged collection is held to; omitted, or `0`, leaves paging to the client
|
|
51
|
+
*
|
|
52
|
+
* @returns A template structurally equivalent to `value`, rewritten where a policy requires it
|
|
53
|
+
*
|
|
54
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `shape` reaches itself through a cycle no link
|
|
55
|
+
* breaks, or where a union it reaches declares a shared member name inconsistently across its branches
|
|
56
|
+
*/
|
|
57
|
+
export function enforce(value, shape, { limit } = {}) {
|
|
58
|
+
return limit ? template(value, shape) : value; // an omitted or `0` page leaves the set unbounded
|
|
59
|
+
/**
|
|
60
|
+
* Walks the slots of a template, resolving each to what the shape says it reaches.
|
|
61
|
+
*/
|
|
62
|
+
function template(value, shape) {
|
|
63
|
+
if (!isObject(value)) {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
return Object.fromEntries(Object.entries(value).map(([name, asked]) => {
|
|
67
|
+
const range = resolve(shape, name);
|
|
68
|
+
// a path the shape cannot resolve leaves the slot as it stands
|
|
69
|
+
return [name, range !== undefined ? slot(asked, range, !isBinding(name)) : asked];
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Resolves a template key to the range the shape gives it, if the shape declares it.
|
|
74
|
+
*/
|
|
75
|
+
function resolve(shape, name) {
|
|
76
|
+
// a projection column carries its own path, while a plain member names the single step it is
|
|
77
|
+
const probe = isBinding(name) ? decodeProbe(name)
|
|
78
|
+
: isIdentifier(name) ? { target: name, pipe: [], path: [name] }
|
|
79
|
+
: undefined;
|
|
80
|
+
const range = probe && effective(shape, probe);
|
|
81
|
+
return isObject(range) ? range : undefined;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Walks what a slot asks for, under the cardinality and the form the shape gives it.
|
|
85
|
+
*
|
|
86
|
+
* @param value The slot to walk
|
|
87
|
+
* @param range The range the shape gives the slot
|
|
88
|
+
* @param paged Whether the slot reaches a collection of its own, which a projection column never does, holding
|
|
89
|
+
* one value per row
|
|
90
|
+
*/
|
|
91
|
+
function slot(value, range, paged) {
|
|
92
|
+
const branches = getShapeBranches(range.shape);
|
|
93
|
+
const [branch] = branches;
|
|
94
|
+
const collection = paged && range.maxCount !== 1;
|
|
95
|
+
return branches.length > 1 ? model(value, branches, collection)
|
|
96
|
+
: branch.kind === "dictionary" ? value // nothing pages a localised value
|
|
97
|
+
: collection ? page(placeholder(value, branch))
|
|
98
|
+
: placeholder(value, branch);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Walks what a single value asks for, descending into the template behind a resource.
|
|
102
|
+
*/
|
|
103
|
+
function placeholder(value, branch) {
|
|
104
|
+
// a union of one flattens away, so its branch map arrives here rather than through `model`
|
|
105
|
+
const target = isObject(value) ? getShapeTarget(branch) : undefined;
|
|
106
|
+
return isUnion(value) ? indexed(value, [branch])
|
|
107
|
+
: target !== undefined ? template(value, target)
|
|
108
|
+
: value;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Walks what a polymorphic slot asks for, paging the collection it reaches.
|
|
112
|
+
*/
|
|
113
|
+
function model(value, branches, collection) {
|
|
114
|
+
return collection ? page(indexed(value, branches)) : indexed(value, branches);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Walks each alternative of a branch map under every branch it fits, its opaque key naming none.
|
|
118
|
+
*/
|
|
119
|
+
function indexed(value, branches) {
|
|
120
|
+
if (!isObject(value)) {
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
return Object.fromEntries(Object.entries(value).map(([key, asked]) => [key, isBranch(key)
|
|
124
|
+
? branches
|
|
125
|
+
.filter(branch => fits(asked, branch))
|
|
126
|
+
.reduce((walked, branch) => placeholder(walked, branch), asked)
|
|
127
|
+
: asked
|
|
128
|
+
]));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Checks whether an alternative fits a branch by form alone.
|
|
132
|
+
*
|
|
133
|
+
* A template fits only the resources declaring every member it asks for.
|
|
134
|
+
*/
|
|
135
|
+
function fits(asked, branch) {
|
|
136
|
+
const target = getShapeTarget(branch);
|
|
137
|
+
return target === undefined || !isObject(asked)
|
|
138
|
+
|| Object.keys(asked).every(name => resolve(target, name) !== undefined);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Holds a collection to the largest page the service serves, leaving a stated one alone.
|
|
142
|
+
*/
|
|
143
|
+
function page(node) {
|
|
144
|
+
return !isObject(node) ? node // a malformed slot is left as it stands
|
|
145
|
+
: "#" in node ? node
|
|
146
|
+
: { ...node, "#": limit };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=index.core.js.map
|