wyrm-mcp 7.3.2 → 7.3.3
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/README.md +8 -14
- package/dist/activation.js +59 -1
- package/dist/agent-daemon.js +281 -4
- package/dist/agent-loop.js +332 -7
- package/dist/analytics.js +236 -13
- package/dist/attribution.js +49 -1
- package/dist/audit.js +457 -2
- package/dist/auto-capture.js +138 -3
- package/dist/auto-orchestrator.js +325 -1
- package/dist/autoconfig.d.ts +50 -0
- package/dist/autoconfig.d.ts.map +1 -1
- package/dist/autoconfig.js +1115 -39
- package/dist/autoconfig.js.map +1 -1
- package/dist/buddy-runner.js +109 -1
- package/dist/buddy.js +564 -14
- package/dist/build-flags.js +15 -1
- package/dist/capabilities.js +183 -3
- package/dist/capture.js +56 -1
- package/dist/causality.js +148 -8
- package/dist/cli.js +281 -20
- package/dist/cloud/cli.js +541 -5
- package/dist/cloud/client.js +221 -1
- package/dist/cloud/crypto.js +85 -1
- package/dist/cloud/machine-id.js +113 -2
- package/dist/cloud/recovery.js +60 -1
- package/dist/cloud/sync-engine.js +543 -7
- package/dist/cloud-backup.js +579 -5
- package/dist/cloud-profile.js +138 -1
- package/dist/cloud-sync-entrypoint.js +47 -1
- package/dist/cloud-sync.js +309 -2
- package/dist/connectors/bridge-source.d.ts +46 -0
- package/dist/connectors/bridge-source.d.ts.map +1 -0
- package/dist/connectors/bridge-source.js +77 -0
- package/dist/connectors/bridge-source.js.map +1 -0
- package/dist/connectors/index.d.ts +24 -0
- package/dist/connectors/index.d.ts.map +1 -0
- package/dist/connectors/index.js +69 -0
- package/dist/connectors/index.js.map +1 -0
- package/dist/connectors/ingest.d.ts +16 -0
- package/dist/connectors/ingest.d.ts.map +1 -0
- package/dist/connectors/ingest.js +116 -0
- package/dist/connectors/ingest.js.map +1 -0
- package/dist/connectors/types.d.ts +99 -0
- package/dist/connectors/types.d.ts.map +1 -0
- package/dist/connectors/types.js +17 -0
- package/dist/connectors/types.js.map +1 -0
- package/dist/constellation.js +168 -12
- package/dist/content-signature.js +45 -1
- package/dist/context-build-budgeted.js +144 -4
- package/dist/context-ranking.js +69 -1
- package/dist/crypto.js +179 -1
- package/dist/daemon-write-endpoint.js +290 -1
- package/dist/daemon-writer.js +406 -2
- package/dist/database.js +1278 -53
- package/dist/deprecations.js +162 -2
- package/dist/design.js +141 -13
- package/dist/event-replication.js +112 -1
- package/dist/events-sse.js +43 -7
- package/dist/events.js +238 -6
- package/dist/failure-patterns.d.ts +107 -0
- package/dist/failure-patterns.d.ts.map +1 -1
- package/dist/failure-patterns.js +924 -43
- package/dist/failure-patterns.js.map +1 -1
- package/dist/federation.js +236 -12
- package/dist/goals.js +101 -13
- package/dist/golden.js +355 -3
- package/dist/handlers/agent.js +165 -4
- package/dist/handlers/alias-adapters.js +129 -1
- package/dist/handlers/aliases.js +171 -1
- package/dist/handlers/audit.js +87 -1
- package/dist/handlers/boundary.js +221 -1
- package/dist/handlers/capture.js +1114 -73
- package/dist/handlers/causality.js +119 -9
- package/dist/handlers/cloud.js +382 -85
- package/dist/handlers/companion.js +459 -28
- package/dist/handlers/datalake.js +187 -7
- package/dist/handlers/dispatch-context.js +22 -0
- package/dist/handlers/entity.js +256 -25
- package/dist/handlers/events.js +335 -16
- package/dist/handlers/failure.d.ts.map +1 -1
- package/dist/handlers/failure.js +408 -13
- package/dist/handlers/failure.js.map +1 -1
- package/dist/handlers/goals.js +296 -4
- package/dist/handlers/intelligence.js +681 -126
- package/dist/handlers/invoicing.js +70 -1
- package/dist/handlers/mcpclient.js +137 -6
- package/dist/handlers/orchestration.js +125 -40
- package/dist/handlers/output-schemas.js +24 -1
- package/dist/handlers/presence.js +99 -3
- package/dist/handlers/project.js +182 -28
- package/dist/handlers/prompts.js +157 -6
- package/dist/handlers/quest.js +224 -4
- package/dist/handlers/recall.js +237 -13
- package/dist/handlers/registry.js +167 -1
- package/dist/handlers/resources.js +288 -1
- package/dist/handlers/review.js +74 -11
- package/dist/handlers/run.js +498 -16
- package/dist/handlers/search.js +338 -15
- package/dist/handlers/session.js +643 -31
- package/dist/handlers/share.js +184 -8
- package/dist/handlers/shims.js +464 -1
- package/dist/handlers/skill.js +449 -67
- package/dist/handlers/survivors.js +120 -1
- package/dist/handlers/symbols.js +109 -8
- package/dist/handlers/syncops.js +302 -4
- package/dist/handlers/types.js +27 -1
- package/dist/harvest.js +191 -5
- package/dist/hours.js +156 -7
- package/dist/http-auth.js +321 -3
- package/dist/http-fast.js +1302 -22
- package/dist/icons.js +47 -1
- package/dist/importers.js +268 -1
- package/dist/index.js +840 -2
- package/dist/indexer.js +145 -4
- package/dist/intelligence.js +261 -31
- package/dist/internal-dispatch.js +212 -3
- package/dist/keyset.js +110 -1
- package/dist/knowledge-graph.js +176 -12
- package/dist/license.js +441 -2
- package/dist/logger.js +199 -2
- package/dist/maintenance.js +148 -2
- package/dist/mcp-client.js +262 -6
- package/dist/memory-artifacts.js +596 -32
- package/dist/migrate-prompt.js +124 -2
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +799 -42
- package/dist/migrations.js.map +1 -1
- package/dist/performance.js +228 -1
- package/dist/presence.js +140 -11
- package/dist/priority-embed.js +164 -5
- package/dist/providers/embedding-provider.js +196 -1
- package/dist/readonly-gate.js +29 -1
- package/dist/receipt.js +43 -1
- package/dist/rehydration.js +157 -9
- package/dist/reindex.js +88 -1
- package/dist/render-target.js +544 -21
- package/dist/render.js +280 -4
- package/dist/repl-guard.js +173 -1
- package/dist/replication-daemon-entrypoint.js +31 -1
- package/dist/replication-daemon.js +262 -2
- package/dist/rerank.js +142 -1
- package/dist/resilience.js +591 -1
- package/dist/reverse-bridge.js +360 -5
- package/dist/security.js +244 -1
- package/dist/session-seen.js +51 -3
- package/dist/setup.js +260 -1
- package/dist/skill-author.js +168 -5
- package/dist/spec-kit.js +191 -1
- package/dist/sqlite-busy.js +154 -1
- package/dist/statusline.js +315 -11
- package/dist/sub-agent.js +262 -13
- package/dist/summarizer.js +139 -13
- package/dist/symbols.js +283 -7
- package/dist/sync.js +359 -5
- package/dist/tasks-dispatch.js +84 -1
- package/dist/tasks.js +282 -1
- package/dist/token-budget.js +143 -1
- package/dist/tool-analytics.js +129 -7
- package/dist/tool-annotations.js +365 -1
- package/dist/tool-manifest-v2.json +1 -1
- package/dist/tool-manifest.json +1 -1
- package/dist/tool-profiles.js +75 -1
- package/dist/trace-harvest.js +244 -6
- package/dist/types.js +30 -1
- package/dist/ui-dashboard.js +50 -41
- package/dist/ulid.js +81 -1
- package/dist/usage-tracker.js +66 -1
- package/dist/validate.js +129 -1
- package/dist/vault.js +534 -1
- package/dist/vector-init.js +67 -1
- package/dist/vectors.js +184 -3
- package/dist/version-check.js +136 -4
- package/dist/visibility.js +155 -19
- package/dist/wyrm-cli.js +2845 -101
- package/dist/wyrm-cli.js.map +1 -1
- package/dist/wyrm-guard.d.ts.map +1 -1
- package/dist/wyrm-guard.js +475 -14
- package/dist/wyrm-guard.js.map +1 -1
- package/dist/wyrm-loop.js +150 -3
- package/dist/wyrm-manifest.json +1 -1
- package/dist/wyrm-statusline-daemon.js +11 -1
- package/dist/wyrm-statusline.js +56 -4
- package/dist/wyrm-ui.js +77 -9
- package/package.json +1 -1
package/dist/ulid.js
CHANGED
|
@@ -1 +1,81 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Wyrm ULID — crypto-based monotonic ULID generator (v7 F2, T008).
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd.
|
|
5
|
+
* @license Proprietary — (c) 2026 Ghost Protocol (Pvt) Ltd. All rights reserved. See LICENSE.
|
|
6
|
+
*
|
|
7
|
+
* 26-char Crockford-base32 identifier: 10 chars of millisecond timestamp +
|
|
8
|
+
* 16 chars (80 bits) of crypto randomness. Used as `runs.run_id` (migration 20)
|
|
9
|
+
* so run ids sort lexicographically in creation order — `ORDER BY run_id`
|
|
10
|
+
* is `ORDER BY started-at` for free, with no coordination table.
|
|
11
|
+
*
|
|
12
|
+
* Monotonic within this process: two calls in the same millisecond (or after a
|
|
13
|
+
* backwards clock step) increment the random tail instead of re-rolling, so ids
|
|
14
|
+
* from one generator are ALWAYS strictly increasing. Zero dependencies — built
|
|
15
|
+
* on node:crypto randomBytes (Article I: fully offline, no LLM, no network).
|
|
16
|
+
*/
|
|
17
|
+
import { randomBytes } from 'crypto';
|
|
18
|
+
/** Crockford base32 alphabet (no I, L, O, U — unambiguous when read aloud). */
|
|
19
|
+
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
20
|
+
const TIME_LEN = 10; // 48-bit ms timestamp -> 10 base32 chars (good past year 10889)
|
|
21
|
+
const RANDOM_LEN = 16; // 80 bits of randomness -> 16 base32 chars
|
|
22
|
+
let lastTime = -1;
|
|
23
|
+
let lastRandom = [];
|
|
24
|
+
function encodeTime(time) {
|
|
25
|
+
let out = '';
|
|
26
|
+
for (let i = 0; i < TIME_LEN; i++) {
|
|
27
|
+
out = ENCODING[time % 32] + out;
|
|
28
|
+
time = Math.floor(time / 32);
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
/** 16 uniform base32 digits from crypto randomness (256 % 32 === 0: no modulo bias). */
|
|
33
|
+
function randomDigits() {
|
|
34
|
+
const bytes = randomBytes(RANDOM_LEN);
|
|
35
|
+
const digits = new Array(RANDOM_LEN);
|
|
36
|
+
for (let i = 0; i < RANDOM_LEN; i++)
|
|
37
|
+
digits[i] = bytes[i] % 32;
|
|
38
|
+
return digits;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Generate a monotonic ULID. `now` is injectable for tests; defaults to
|
|
42
|
+
* Date.now(). If the clock reads the same ms as (or earlier than) the previous
|
|
43
|
+
* call, the previous timestamp is reused and the random tail incremented —
|
|
44
|
+
* strict lexicographic monotonicity survives same-ms bursts and clock skew.
|
|
45
|
+
*/
|
|
46
|
+
export function ulid(now = Date.now()) {
|
|
47
|
+
let digits;
|
|
48
|
+
let time = now;
|
|
49
|
+
if (now <= lastTime) {
|
|
50
|
+
// Same millisecond or clock went backwards: stay monotonic by reusing the
|
|
51
|
+
// last timestamp and incrementing the 80-bit random tail by one.
|
|
52
|
+
time = lastTime;
|
|
53
|
+
digits = lastRandom.slice();
|
|
54
|
+
let i = RANDOM_LEN - 1;
|
|
55
|
+
for (; i >= 0; i--) {
|
|
56
|
+
if (digits[i] < 31) {
|
|
57
|
+
digits[i]++;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
digits[i] = 0;
|
|
61
|
+
}
|
|
62
|
+
if (i < 0) {
|
|
63
|
+
// 2^80 increments in one ms — practically unreachable; re-roll.
|
|
64
|
+
digits = randomDigits();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
digits = randomDigits();
|
|
69
|
+
}
|
|
70
|
+
lastTime = time;
|
|
71
|
+
lastRandom = digits;
|
|
72
|
+
let tail = '';
|
|
73
|
+
for (let i = 0; i < RANDOM_LEN; i++)
|
|
74
|
+
tail += ENCODING[digits[i]];
|
|
75
|
+
return encodeTime(time) + tail;
|
|
76
|
+
}
|
|
77
|
+
/** Shape check: 26 Crockford-base32 chars (excludes I, L, O, U). */
|
|
78
|
+
export function isUlid(value) {
|
|
79
|
+
return typeof value === 'string' && /^[0-9A-HJKMNP-TV-Z]{26}$/.test(value);
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ulid.js.map
|
package/dist/usage-tracker.js
CHANGED
|
@@ -1 +1,66 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* In-memory tool-usage buffer + stats for the wyrm_usage handler.
|
|
3
|
+
*
|
|
4
|
+
* Extracted VERBATIM from index.ts (quest #80) so the server entrypoint stays
|
|
5
|
+
* under its ≤999-line surface guard. A factory closes over the ring buffer and
|
|
6
|
+
* the analytics sink; behaviour is byte-for-byte the inline original.
|
|
7
|
+
*
|
|
8
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd.
|
|
9
|
+
* @license Proprietary — (c) 2026 Ghost Protocol (Pvt) Ltd. All rights reserved. See LICENSE.
|
|
10
|
+
*/
|
|
11
|
+
const USAGE_MAX_ENTRIES = 500;
|
|
12
|
+
/** Build a usage tracker over the given analytics sink. */
|
|
13
|
+
export function createUsageTracker(analytics) {
|
|
14
|
+
const usageLog = [];
|
|
15
|
+
function trackUsage(entry) {
|
|
16
|
+
usageLog.push(entry);
|
|
17
|
+
if (usageLog.length > USAGE_MAX_ENTRIES) {
|
|
18
|
+
usageLog.splice(0, usageLog.length - USAGE_MAX_ENTRIES);
|
|
19
|
+
}
|
|
20
|
+
// Persist to analytics (Pro+ feature, but record for all — dashboard gated)
|
|
21
|
+
analytics.record({
|
|
22
|
+
tool: entry.tool,
|
|
23
|
+
tokens_in: entry.tokens_in,
|
|
24
|
+
tokens_out: entry.tokens_out,
|
|
25
|
+
cached: entry.cached,
|
|
26
|
+
ms: entry.ms,
|
|
27
|
+
success: true,
|
|
28
|
+
timestamp: entry.timestamp,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function getUsageStats(last) {
|
|
32
|
+
const entries = last ? usageLog.slice(-last) : usageLog;
|
|
33
|
+
const cachedEntries = entries.filter(e => e.cached);
|
|
34
|
+
const toolMap = new Map();
|
|
35
|
+
let totalTokensIn = 0, totalTokensOut = 0, tokensSaved = 0, totalMs = 0;
|
|
36
|
+
for (const e of entries) {
|
|
37
|
+
totalTokensIn += e.tokens_in;
|
|
38
|
+
totalTokensOut += e.tokens_out;
|
|
39
|
+
totalMs += e.ms;
|
|
40
|
+
if (e.cached)
|
|
41
|
+
tokensSaved += e.tokens_out;
|
|
42
|
+
const existing = toolMap.get(e.tool) || { calls: 0, tokens: 0 };
|
|
43
|
+
existing.calls++;
|
|
44
|
+
existing.tokens += e.tokens_out;
|
|
45
|
+
toolMap.set(e.tool, existing);
|
|
46
|
+
}
|
|
47
|
+
const topTools = [...toolMap.entries()]
|
|
48
|
+
.map(([tool, data]) => ({ tool, ...data }))
|
|
49
|
+
.sort((a, b) => b.tokens - a.tokens)
|
|
50
|
+
.slice(0, 10);
|
|
51
|
+
return {
|
|
52
|
+
totalCalls: entries.length,
|
|
53
|
+
cachedCalls: cachedEntries.length,
|
|
54
|
+
cacheHitRate: entries.length > 0
|
|
55
|
+
? `${((cachedEntries.length / entries.length) * 100).toFixed(1)}%`
|
|
56
|
+
: '0%',
|
|
57
|
+
totalTokensIn,
|
|
58
|
+
totalTokensOut,
|
|
59
|
+
tokensSaved,
|
|
60
|
+
avgResponseMs: entries.length > 0 ? Math.round(totalMs / entries.length) : 0,
|
|
61
|
+
topTools,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return { trackUsage, getUsageStats, usageLog };
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=usage-tracker.js.map
|
package/dist/validate.js
CHANGED
|
@@ -1 +1,129 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Boundary input validation for MCP tool arguments.
|
|
3
|
+
*
|
|
4
|
+
* v7 Pillar 4 (harden by construction) / Constitution Article VII (secure by
|
|
5
|
+
* default). Coerce + validate a tool argument AT THE DISPATCH SEAM so a handler
|
|
6
|
+
* never receives an out-of-contract value — closing the class the v6.14.1
|
|
7
|
+
* `wyrm_prune` SQL-injection belonged to (a string where an integer was assumed).
|
|
8
|
+
*
|
|
9
|
+
* Every validator throws {@link ValidationError} on bad input; the CallTool
|
|
10
|
+
* dispatcher catches it and returns a clean `isError` response — never a crash,
|
|
11
|
+
* never an injection sink. The low-level MCP `Server` treats `inputSchema` as
|
|
12
|
+
* advisory only, so this is the real enforcement layer.
|
|
13
|
+
*
|
|
14
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd.
|
|
15
|
+
* @license Proprietary — (c) 2026 Ghost Protocol (Pvt) Ltd. All rights reserved. See LICENSE.
|
|
16
|
+
*/
|
|
17
|
+
export class ValidationError extends Error {
|
|
18
|
+
field;
|
|
19
|
+
constructor(field, reason) {
|
|
20
|
+
super(`'${field}' ${reason}`);
|
|
21
|
+
this.name = 'ValidationError';
|
|
22
|
+
this.field = field;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const isNil = (v) => v === undefined || v === null;
|
|
26
|
+
/** Integer, coerced from a clean numeric string. Returns `default`/undefined when absent. */
|
|
27
|
+
export function asInt(field, v, opts = {}) {
|
|
28
|
+
if (isNil(v))
|
|
29
|
+
return opts.default;
|
|
30
|
+
let n;
|
|
31
|
+
if (typeof v === 'number')
|
|
32
|
+
n = v;
|
|
33
|
+
else if (typeof v === 'string' && /^[+-]?\d+$/.test(v.trim()))
|
|
34
|
+
n = Number(v.trim());
|
|
35
|
+
else
|
|
36
|
+
throw new ValidationError(field, 'must be an integer');
|
|
37
|
+
if (!Number.isInteger(n))
|
|
38
|
+
throw new ValidationError(field, 'must be an integer');
|
|
39
|
+
if (opts.min !== undefined && n < opts.min)
|
|
40
|
+
throw new ValidationError(field, `must be >= ${opts.min}`);
|
|
41
|
+
if (opts.max !== undefined && n > opts.max)
|
|
42
|
+
throw new ValidationError(field, `must be <= ${opts.max}`);
|
|
43
|
+
return n;
|
|
44
|
+
}
|
|
45
|
+
/** Required integer — throws if absent. */
|
|
46
|
+
export function requireInt(field, v, opts = {}) {
|
|
47
|
+
const n = asInt(field, v, opts);
|
|
48
|
+
if (n === undefined)
|
|
49
|
+
throw new ValidationError(field, 'is required');
|
|
50
|
+
return n;
|
|
51
|
+
}
|
|
52
|
+
/** Finite number (float allowed). */
|
|
53
|
+
export function asNumber(field, v, opts = {}) {
|
|
54
|
+
if (isNil(v))
|
|
55
|
+
return opts.default;
|
|
56
|
+
let n;
|
|
57
|
+
if (typeof v === 'number')
|
|
58
|
+
n = v;
|
|
59
|
+
else if (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v)))
|
|
60
|
+
n = Number(v);
|
|
61
|
+
else
|
|
62
|
+
throw new ValidationError(field, 'must be a number');
|
|
63
|
+
if (!Number.isFinite(n))
|
|
64
|
+
throw new ValidationError(field, 'must be a finite number');
|
|
65
|
+
if (opts.min !== undefined && n < opts.min)
|
|
66
|
+
throw new ValidationError(field, `must be >= ${opts.min}`);
|
|
67
|
+
if (opts.max !== undefined && n > opts.max)
|
|
68
|
+
throw new ValidationError(field, `must be <= ${opts.max}`);
|
|
69
|
+
return n;
|
|
70
|
+
}
|
|
71
|
+
/** String with optional length + pattern bounds. */
|
|
72
|
+
export function asString(field, v, opts = {}) {
|
|
73
|
+
if (isNil(v))
|
|
74
|
+
return opts.default;
|
|
75
|
+
if (typeof v !== 'string')
|
|
76
|
+
throw new ValidationError(field, 'must be a string');
|
|
77
|
+
if (opts.minLen !== undefined && v.length < opts.minLen)
|
|
78
|
+
throw new ValidationError(field, `must be at least ${opts.minLen} characters`);
|
|
79
|
+
if (opts.maxLen !== undefined && v.length > opts.maxLen)
|
|
80
|
+
throw new ValidationError(field, `must be at most ${opts.maxLen} characters`);
|
|
81
|
+
if (opts.pattern && !opts.pattern.test(v))
|
|
82
|
+
throw new ValidationError(field, 'has an invalid format');
|
|
83
|
+
return v;
|
|
84
|
+
}
|
|
85
|
+
/** Required string — throws if absent. */
|
|
86
|
+
export function requireString(field, v, opts = {}) {
|
|
87
|
+
const s = asString(field, v, opts);
|
|
88
|
+
if (s === undefined)
|
|
89
|
+
throw new ValidationError(field, 'is required');
|
|
90
|
+
return s;
|
|
91
|
+
}
|
|
92
|
+
/** Boolean, accepting the common truthy/falsey wire encodings (`'1'`/`'0'`, `'true'`/`'false'`, `1`/`0`). */
|
|
93
|
+
export function asBool(field, v, def) {
|
|
94
|
+
if (isNil(v))
|
|
95
|
+
return def;
|
|
96
|
+
if (typeof v === 'boolean')
|
|
97
|
+
return v;
|
|
98
|
+
if (v === 'true' || v === '1' || v === 1)
|
|
99
|
+
return true;
|
|
100
|
+
if (v === 'false' || v === '0' || v === 0)
|
|
101
|
+
return false;
|
|
102
|
+
throw new ValidationError(field, 'must be a boolean');
|
|
103
|
+
}
|
|
104
|
+
/** One of a fixed allowed set. */
|
|
105
|
+
export function asEnum(field, v, allowed, def) {
|
|
106
|
+
if (isNil(v))
|
|
107
|
+
return def;
|
|
108
|
+
if (typeof v !== 'string' || !allowed.includes(v)) {
|
|
109
|
+
throw new ValidationError(field, `must be one of: ${allowed.join(', ')}`);
|
|
110
|
+
}
|
|
111
|
+
return v;
|
|
112
|
+
}
|
|
113
|
+
/** Array of strings, each optionally length-bounded. */
|
|
114
|
+
export function asStringArray(field, v, opts = {}) {
|
|
115
|
+
if (isNil(v))
|
|
116
|
+
return undefined;
|
|
117
|
+
if (!Array.isArray(v))
|
|
118
|
+
throw new ValidationError(field, 'must be an array');
|
|
119
|
+
if (opts.maxItems !== undefined && v.length > opts.maxItems)
|
|
120
|
+
throw new ValidationError(field, `must have at most ${opts.maxItems} items`);
|
|
121
|
+
return v.map((item, i) => {
|
|
122
|
+
if (typeof item !== 'string')
|
|
123
|
+
throw new ValidationError(`${field}[${i}]`, 'must be a string');
|
|
124
|
+
if (opts.maxLen !== undefined && item.length > opts.maxLen)
|
|
125
|
+
throw new ValidationError(`${field}[${i}]`, `must be at most ${opts.maxLen} characters`);
|
|
126
|
+
return item;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=validate.js.map
|