@grafctory/keep 0.0.0-stage → 0.14.2

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.
@@ -0,0 +1,41 @@
1
+ import type { StoreClient } from "../index.js";
2
+ /**
3
+ * Creates a validating store backed by a delegate.
4
+ *
5
+ * Each method validates its inputs against the supplied shape before forwarding the request to `store`:
6
+ *
7
+ * - `entry` must be an absolute IRI with no query string and no fragment; otherwise the request is rejected with a
8
+ * {@link !RangeError RangeError}
9
+ * - `model` is validated as a {@link @grafctory/blue!validate | template} for {@link StoreClient.lookup lookup},
10
+ * within the query-complexity bounds (`plain`, `depth`, `limit`) of the caller's
11
+ * {@link StoreScope | retrieval scope}, and as a collection slice for {@link StoreClient.create create}
12
+ * - `state.id`, if present, must be nested under `entry` for {@link StoreClient.create create}, and equal to `entry`
13
+ * for {@link StoreClient.update update} and {@link StoreClient.insert insert}; otherwise the request is rejected
14
+ * with a {@link !RangeError RangeError}
15
+ * - `state` is validated as a resource with captive expansion capped at depth `0`, so inline captive batches are
16
+ * rejected; {@link StoreClient.create create} validates it against the shape of the collected resources, and
17
+ * {@link StoreClient.update update} and {@link StoreClient.insert insert} against the supplied shape
18
+ *
19
+ * The `trusted` option controls whether {@link StoreClient.lookup lookup} responses are re-validated against the
20
+ * shape, as narrowed by the caller's `model`. It defaults to `false`, the safe choice for connectors whose source may
21
+ * deliver non-conforming data, such as a remote REST endpoint. Local stores that compute results themselves can pass
22
+ * `trusted: true` to skip the redundant check.
23
+ *
24
+ * Validation failures surface as {@link @grafctory/core!TraceError | TraceError} or {@link !RangeError RangeError}
25
+ * rejections, in line with the unified {@link Store} error channel.
26
+ *
27
+ * @param store - Inner StoreClient receiving the validated requests
28
+ * @param options - Optional validation options
29
+ *
30
+ * @returns An immutable {@link StoreClient} wrapping `store` with input validation
31
+ */
32
+ export declare function createValidatingStore(store: StoreClient, { trusted }?: {
33
+ /**
34
+ * Whether to skip re-validation of {@link StoreClient.lookup lookup} responses against the shape, as narrowed by
35
+ * the caller's `model`.
36
+ *
37
+ * @defaultValue `false`, treating the wrapped store as untrusted
38
+ */
39
+ readonly trusted?: boolean;
40
+ }): StoreClient;
41
+ //# sourceMappingURL=validating.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validating.d.ts","sourceRoot":"","sources":["../../src/clients/validating.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAAS,WAAW,EAAc,MAAM,aAAa,CAAC;AAGlE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,WAAW,EAAE,EAEzD,OAAe,EAEf,GAAE;IAEF;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAErB,GAAG,WAAW,CAoJnB"}
@@ -0,0 +1,151 @@
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
+ * Validating store wrapper.
18
+ *
19
+ * Guards a {@link StoreClient} against malformed requests, checking entries, models and states against the request
20
+ * shape before they reach the backend. Retrieval responses from untrusted sources can be re-validated as well.
21
+ *
22
+ * @module
23
+ */
24
+ import { validate } from "@grafctory/blue";
25
+ import { blueprint } from "@grafctory/blue/value";
26
+ import { error } from "@grafctory/core";
27
+ import { isNestedIRI } from "@grafctory/core/resource";
28
+ import { TraceError } from "@grafctory/core/trace";
29
+ import { immutable } from "@grafctory/core/values";
30
+ import { isReference } from "@grafctory/qest/state";
31
+ /**
32
+ * Creates a validating store backed by a delegate.
33
+ *
34
+ * Each method validates its inputs against the supplied shape before forwarding the request to `store`:
35
+ *
36
+ * - `entry` must be an absolute IRI with no query string and no fragment; otherwise the request is rejected with a
37
+ * {@link !RangeError RangeError}
38
+ * - `model` is validated as a {@link @grafctory/blue!validate | template} for {@link StoreClient.lookup lookup},
39
+ * within the query-complexity bounds (`plain`, `depth`, `limit`) of the caller's
40
+ * {@link StoreScope | retrieval scope}, and as a collection slice for {@link StoreClient.create create}
41
+ * - `state.id`, if present, must be nested under `entry` for {@link StoreClient.create create}, and equal to `entry`
42
+ * for {@link StoreClient.update update} and {@link StoreClient.insert insert}; otherwise the request is rejected
43
+ * with a {@link !RangeError RangeError}
44
+ * - `state` is validated as a resource with captive expansion capped at depth `0`, so inline captive batches are
45
+ * rejected; {@link StoreClient.create create} validates it against the shape of the collected resources, and
46
+ * {@link StoreClient.update update} and {@link StoreClient.insert insert} against the supplied shape
47
+ *
48
+ * The `trusted` option controls whether {@link StoreClient.lookup lookup} responses are re-validated against the
49
+ * shape, as narrowed by the caller's `model`. It defaults to `false`, the safe choice for connectors whose source may
50
+ * deliver non-conforming data, such as a remote REST endpoint. Local stores that compute results themselves can pass
51
+ * `trusted: true` to skip the redundant check.
52
+ *
53
+ * Validation failures surface as {@link @grafctory/core!TraceError | TraceError} or {@link !RangeError RangeError}
54
+ * rejections, in line with the unified {@link Store} error channel.
55
+ *
56
+ * @param store - Inner StoreClient receiving the validated requests
57
+ * @param options - Optional validation options
58
+ *
59
+ * @returns An immutable {@link StoreClient} wrapping `store` with input validation
60
+ */
61
+ export function createValidatingStore(store, { trusted = false } = {}) {
62
+ return immutable({
63
+ async lookup({ virtual, entry, shape, model }, opts) {
64
+ return store.lookup({
65
+ virtual,
66
+ entry: assertEntry(entry),
67
+ shape,
68
+ model: assertModel(model, shape, opts)
69
+ }, opts).then(result => {
70
+ // ;(cast) blue types the validated copy by its own `Delivery` inference rather than by the `Match`
71
+ // the store states; the copy is returned, rather than the raw response, as it carries the verdict
72
+ return (trusted || result === undefined ? immutable(result) : validate(result, { shape, model })({
73
+ value: value => value,
74
+ trace: trace => { throw new TraceError("invalid response", trace ?? []); }
75
+ }));
76
+ });
77
+ },
78
+ async create({ entry, shape, model, state }) {
79
+ const $entry = assertEntry(entry);
80
+ const $model = validate(model, { shape, model: true })({
81
+ value: value => value,
82
+ trace: trace => { throw new TraceError("invalid model", trace ?? []); }
83
+ });
84
+ // the new resource is validated against the shape the collecting property ranges over, and a stated
85
+ // identifier is held to the collection it is created through
86
+ const plan = blueprint(shape, $model);
87
+ const { id } = state;
88
+ const $state = id === undefined || isReference(id) && isNestedIRI($entry, id) ? state
89
+ : error(new RangeError(`state id <${String(id)}> not nested under entry <${entry}>`));
90
+ // the validator hands back the state itself where it passes, so the typed input is relayed as it
91
+ // stands once the check has run, the blueprint being resolved at runtime rather than typed
92
+ validate($state, { shape: plan, depth: 0 })({
93
+ value: () => undefined,
94
+ trace: trace => { throw new TraceError("invalid state", trace ?? []); }
95
+ });
96
+ return store.create({ entry: $entry, shape, model: $model, state: $state });
97
+ },
98
+ async update({ entry, shape, state }) {
99
+ const $entry = assertEntry(entry);
100
+ return store.update({
101
+ entry: $entry,
102
+ shape,
103
+ state: validate(assertIdentity($entry, state), { shape, depth: 0 })({
104
+ value: () => state,
105
+ trace: trace => { throw new TraceError("invalid state", trace ?? []); }
106
+ })
107
+ });
108
+ },
109
+ async delete({ entry, shape }) {
110
+ return store.delete({ entry: assertEntry(entry), shape });
111
+ },
112
+ async insert({ entry, shape, state }) {
113
+ const $entry = assertEntry(entry);
114
+ return store.insert({
115
+ entry: $entry,
116
+ shape,
117
+ state: validate(assertIdentity($entry, state), { shape, depth: 0 })({
118
+ value: () => state,
119
+ trace: trace => { throw new TraceError("invalid state", trace ?? []); }
120
+ })
121
+ });
122
+ },
123
+ async remove({ entry, shape }) {
124
+ return store.remove({
125
+ entry: assertEntry(entry),
126
+ shape
127
+ });
128
+ }
129
+ });
130
+ function assertEntry(entry) {
131
+ if (!isReference(entry)) {
132
+ throw new RangeError(`expected absolute IRI <${entry}>`);
133
+ }
134
+ if (entry.includes("?") || entry.includes("#")) {
135
+ throw new RangeError(`unexpected query or fragment in <${entry}>`);
136
+ }
137
+ return entry;
138
+ }
139
+ function assertIdentity(entry, state) {
140
+ const { id } = state;
141
+ return id === undefined || id === entry ? state
142
+ : error(new RangeError(`mismatched state id <${String(id)}> for entry <${entry}>`));
143
+ }
144
+ function assertModel(model, shape, opts = {}) {
145
+ return validate(model, { ...opts, shape, model: true })({
146
+ value: value => value,
147
+ trace: trace => { throw new TraceError("invalid model", trace ?? []); }
148
+ });
149
+ }
150
+ }
151
+ //# sourceMappingURL=validating.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validating.js","sourceRoot":"","sources":["../../src/clients/validating.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAc,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,KAAK,EAA4B,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAiC,MAAM,uBAAuB,CAAC;AAInF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAkB,EAAE,EAEzD,OAAO,GAAG,KAAK,KAYZ,EAAE;IAEL,OAAO,SAAS,CAAC;QAEhB,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI;YAElD,OAAO,KAAK,CAAC,MAAM,CAAC;gBAEnB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