@ontrails/core 1.0.0-beta.0
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/.turbo/turbo-build.log +1 -0
- package/.turbo/turbo-lint.log +3 -0
- package/.turbo/turbo-typecheck.log +1 -0
- package/CHANGELOG.md +15 -0
- package/README.md +179 -0
- package/dist/adapters.d.ts +39 -0
- package/dist/adapters.d.ts.map +1 -0
- package/dist/adapters.js +2 -0
- package/dist/adapters.js.map +1 -0
- package/dist/blob-ref.d.ts +20 -0
- package/dist/blob-ref.d.ts.map +1 -0
- package/dist/blob-ref.js +22 -0
- package/dist/blob-ref.js.map +1 -0
- package/dist/branded.d.ts +36 -0
- package/dist/branded.d.ts.map +1 -0
- package/dist/branded.js +89 -0
- package/dist/branded.js.map +1 -0
- package/dist/collections.d.ts +31 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +60 -0
- package/dist/collections.js.map +1 -0
- package/dist/context.d.ts +10 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +15 -0
- package/dist/context.js.map +1 -0
- package/dist/derive.d.ts +33 -0
- package/dist/derive.d.ts.map +1 -0
- package/dist/derive.js +122 -0
- package/dist/derive.js.map +1 -0
- package/dist/errors.d.ts +83 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +142 -0
- package/dist/errors.js.map +1 -0
- package/dist/event.d.ts +45 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +17 -0
- package/dist/event.js.map +1 -0
- package/dist/fetch.d.ts +15 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +102 -0
- package/dist/fetch.js.map +1 -0
- package/dist/guards.d.ts +17 -0
- package/dist/guards.d.ts.map +1 -0
- package/dist/guards.js +25 -0
- package/dist/guards.js.map +1 -0
- package/dist/health.d.ts +18 -0
- package/dist/health.d.ts.map +1 -0
- package/dist/health.js +5 -0
- package/dist/health.js.map +1 -0
- package/dist/hike.d.ts +36 -0
- package/dist/hike.d.ts.map +1 -0
- package/dist/hike.js +20 -0
- package/dist/hike.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/job.d.ts +24 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +17 -0
- package/dist/job.js.map +1 -0
- package/dist/layer.d.ts +17 -0
- package/dist/layer.d.ts.map +1 -0
- package/dist/layer.js +21 -0
- package/dist/layer.js.map +1 -0
- package/dist/path-security.d.ts +28 -0
- package/dist/path-security.d.ts.map +1 -0
- package/dist/path-security.js +63 -0
- package/dist/path-security.js.map +1 -0
- package/dist/patterns/bulk.d.ts +15 -0
- package/dist/patterns/bulk.d.ts.map +1 -0
- package/dist/patterns/bulk.js +14 -0
- package/dist/patterns/bulk.js.map +1 -0
- package/dist/patterns/change.d.ts +10 -0
- package/dist/patterns/change.d.ts.map +1 -0
- package/dist/patterns/change.js +10 -0
- package/dist/patterns/change.js.map +1 -0
- package/dist/patterns/date-range.d.ts +10 -0
- package/dist/patterns/date-range.d.ts.map +1 -0
- package/dist/patterns/date-range.js +10 -0
- package/dist/patterns/date-range.js.map +1 -0
- package/dist/patterns/index.d.ts +9 -0
- package/dist/patterns/index.d.ts.map +1 -0
- package/dist/patterns/index.js +9 -0
- package/dist/patterns/index.js.map +1 -0
- package/dist/patterns/pagination.d.ts +18 -0
- package/dist/patterns/pagination.d.ts.map +1 -0
- package/dist/patterns/pagination.js +18 -0
- package/dist/patterns/pagination.js.map +1 -0
- package/dist/patterns/progress.d.ts +11 -0
- package/dist/patterns/progress.d.ts.map +1 -0
- package/dist/patterns/progress.js +11 -0
- package/dist/patterns/progress.js.map +1 -0
- package/dist/patterns/sorting.d.ts +13 -0
- package/dist/patterns/sorting.d.ts.map +1 -0
- package/dist/patterns/sorting.js +10 -0
- package/dist/patterns/sorting.js.map +1 -0
- package/dist/patterns/status.d.ts +15 -0
- package/dist/patterns/status.d.ts.map +1 -0
- package/dist/patterns/status.js +9 -0
- package/dist/patterns/status.js.map +1 -0
- package/dist/patterns/timestamps.d.ts +10 -0
- package/dist/patterns/timestamps.d.ts.map +1 -0
- package/dist/patterns/timestamps.js +10 -0
- package/dist/patterns/timestamps.js.map +1 -0
- package/dist/redaction/index.d.ts +4 -0
- package/dist/redaction/index.d.ts.map +1 -0
- package/dist/redaction/index.js +3 -0
- package/dist/redaction/index.js.map +1 -0
- package/dist/redaction/patterns.d.ts +9 -0
- package/dist/redaction/patterns.d.ts.map +1 -0
- package/dist/redaction/patterns.js +39 -0
- package/dist/redaction/patterns.js.map +1 -0
- package/dist/redaction/redactor.d.ts +27 -0
- package/dist/redaction/redactor.d.ts.map +1 -0
- package/dist/redaction/redactor.js +89 -0
- package/dist/redaction/redactor.js.map +1 -0
- package/dist/resilience.d.ts +34 -0
- package/dist/resilience.d.ts.map +1 -0
- package/dist/resilience.js +164 -0
- package/dist/resilience.js.map +1 -0
- package/dist/result.d.ts +57 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +145 -0
- package/dist/result.js.map +1 -0
- package/dist/serialization.d.ts +27 -0
- package/dist/serialization.d.ts.map +1 -0
- package/dist/serialization.js +115 -0
- package/dist/serialization.js.map +1 -0
- package/dist/topo.d.ts +18 -0
- package/dist/topo.d.ts.map +1 -0
- package/dist/topo.js +74 -0
- package/dist/topo.js.map +1 -0
- package/dist/trail.d.ts +83 -0
- package/dist/trail.d.ts.map +1 -0
- package/dist/trail.js +16 -0
- package/dist/trail.js.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validate-topo.d.ts +24 -0
- package/dist/validate-topo.d.ts.map +1 -0
- package/dist/validate-topo.js +108 -0
- package/dist/validate-topo.js.map +1 -0
- package/dist/validation.d.ts +27 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +134 -0
- package/dist/validation.js.map +1 -0
- package/dist/workspace.d.ts +25 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +57 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +21 -0
- package/src/__tests__/blob-ref.test.ts +103 -0
- package/src/__tests__/branded.test.ts +148 -0
- package/src/__tests__/collections.test.ts +126 -0
- package/src/__tests__/context.test.ts +66 -0
- package/src/__tests__/derive.test.ts +159 -0
- package/src/__tests__/errors.test.ts +309 -0
- package/src/__tests__/event.test.ts +82 -0
- package/src/__tests__/fetch.test.ts +217 -0
- package/src/__tests__/guards.test.ts +102 -0
- package/src/__tests__/hike.test.ts +117 -0
- package/src/__tests__/job.test.ts +98 -0
- package/src/__tests__/layer.test.ts +224 -0
- package/src/__tests__/path-security.test.ts +114 -0
- package/src/__tests__/patterns.test.ts +273 -0
- package/src/__tests__/redaction.test.ts +244 -0
- package/src/__tests__/resilience.test.ts +246 -0
- package/src/__tests__/result.test.ts +155 -0
- package/src/__tests__/serialization.test.ts +236 -0
- package/src/__tests__/topo.test.ts +184 -0
- package/src/__tests__/trail.test.ts +179 -0
- package/src/__tests__/validate-topo.test.ts +201 -0
- package/src/__tests__/validation.test.ts +283 -0
- package/src/__tests__/workspace.test.ts +183 -0
- package/src/adapters.ts +68 -0
- package/src/blob-ref.ts +39 -0
- package/src/branded.ts +135 -0
- package/src/collections.ts +99 -0
- package/src/context.ts +18 -0
- package/src/derive.ts +223 -0
- package/src/errors.ts +196 -0
- package/src/event.ts +77 -0
- package/src/fetch.ts +138 -0
- package/src/guards.ts +37 -0
- package/src/health.ts +23 -0
- package/src/hike.ts +77 -0
- package/src/index.ts +158 -0
- package/src/job.ts +20 -0
- package/src/layer.ts +44 -0
- package/src/path-security.ts +90 -0
- package/src/patterns/bulk.ts +16 -0
- package/src/patterns/change.ts +12 -0
- package/src/patterns/date-range.ts +12 -0
- package/src/patterns/index.ts +8 -0
- package/src/patterns/pagination.ts +22 -0
- package/src/patterns/progress.ts +13 -0
- package/src/patterns/sorting.ts +14 -0
- package/src/patterns/status.ts +11 -0
- package/src/patterns/timestamps.ts +12 -0
- package/src/redaction/index.ts +3 -0
- package/src/redaction/patterns.ts +47 -0
- package/src/redaction/redactor.ts +178 -0
- package/src/resilience.ts +234 -0
- package/src/result.ts +180 -0
- package/src/serialization.ts +183 -0
- package/src/topo.ts +123 -0
- package/src/trail.ts +130 -0
- package/src/types.ts +58 -0
- package/src/validate-topo.ts +151 -0
- package/src/validation.ts +182 -0
- package/src/workspace.ts +77 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural validation for a Topo graph.
|
|
3
|
+
*
|
|
4
|
+
* Checks hike follows references, example input validity, event origin
|
|
5
|
+
* references, and output schema completeness. Returns a Result with all
|
|
6
|
+
* issues collected into a single ValidationError.
|
|
7
|
+
*/
|
|
8
|
+
import { ValidationError } from './errors.js';
|
|
9
|
+
import { Result } from './result.js';
|
|
10
|
+
import { validateInput } from './validation.js';
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Validators
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
const checkFollows = (hikes, topo) => {
|
|
15
|
+
const issues = [];
|
|
16
|
+
for (const [id, hike] of hikes) {
|
|
17
|
+
for (const followId of hike.follows) {
|
|
18
|
+
if (followId === id) {
|
|
19
|
+
issues.push({
|
|
20
|
+
message: `Hike follows itself`,
|
|
21
|
+
rule: 'no-self-follow',
|
|
22
|
+
trailId: id,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
else if (!topo.has(followId)) {
|
|
26
|
+
issues.push({
|
|
27
|
+
message: `Follows "${followId}" which is not in the topo`,
|
|
28
|
+
rule: 'follows-exist',
|
|
29
|
+
trailId: id,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return issues;
|
|
35
|
+
};
|
|
36
|
+
const checkOneExample = (id, example, inputSchema, hasOutput) => {
|
|
37
|
+
const issues = [];
|
|
38
|
+
const result = validateInput(inputSchema, example.input);
|
|
39
|
+
if (result.isErr() && example.error === undefined) {
|
|
40
|
+
issues.push({
|
|
41
|
+
message: `Example "${example.name}" input does not parse against schema`,
|
|
42
|
+
rule: 'example-input-valid',
|
|
43
|
+
trailId: id,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (example.expected !== undefined && !hasOutput) {
|
|
47
|
+
issues.push({
|
|
48
|
+
message: `Example "${example.name}" has expected output but trail has no output schema`,
|
|
49
|
+
rule: 'output-schema-present',
|
|
50
|
+
trailId: id,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return issues;
|
|
54
|
+
};
|
|
55
|
+
const checkExamples = (trails) => {
|
|
56
|
+
const issues = [];
|
|
57
|
+
for (const [id, trail] of trails) {
|
|
58
|
+
if (!trail.examples) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const example of trail.examples) {
|
|
62
|
+
issues.push(...checkOneExample(id, example, trail.input, !!trail.output));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return issues;
|
|
66
|
+
};
|
|
67
|
+
const checkEventOrigins = (events, topo) => {
|
|
68
|
+
const issues = [];
|
|
69
|
+
for (const [id, evt] of events) {
|
|
70
|
+
if (!evt.from) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
for (const originId of evt.from) {
|
|
74
|
+
if (!topo.has(originId)) {
|
|
75
|
+
issues.push({
|
|
76
|
+
message: `Event origin "${originId}" is not in the topo`,
|
|
77
|
+
rule: 'event-origin-exists',
|
|
78
|
+
trailId: id,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return issues;
|
|
84
|
+
};
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// Public API
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
/**
|
|
89
|
+
* Validate the structural integrity of a Topo graph.
|
|
90
|
+
*
|
|
91
|
+
* Checks follows references, example inputs, event origins, and output
|
|
92
|
+
* schema presence. Returns `Result.ok()` when no issues are found, or
|
|
93
|
+
* `Result.err(ValidationError)` with all issues in the error context.
|
|
94
|
+
*/
|
|
95
|
+
export const validateTopo = (topo) => {
|
|
96
|
+
const issues = [
|
|
97
|
+
...checkFollows(topo.hikes, topo),
|
|
98
|
+
...checkExamples(topo.trails),
|
|
99
|
+
...checkEventOrigins(topo.events, topo),
|
|
100
|
+
];
|
|
101
|
+
if (issues.length === 0) {
|
|
102
|
+
return Result.ok();
|
|
103
|
+
}
|
|
104
|
+
return Result.err(new ValidationError(`Topo validation failed with ${issues.length} issue(s)`, {
|
|
105
|
+
context: { issues },
|
|
106
|
+
}));
|
|
107
|
+
};
|
|
108
|
+
//# sourceMappingURL=validate-topo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-topo.js","sourceRoot":"","sources":["../src/validate-topo.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAYhD,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E,MAAM,YAAY,GAAG,CACnB,KAAmC,EACnC,IAAU,EACG,EAAE;IACf,MAAM,MAAM,GAAgB,EAAE,CAAC;IAC/B,KAAK,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;QAC/B,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACpC,IAAI,QAAQ,KAAK,EAAE,EAAE,CAAC;gBACpB,MAAM,CAAC,IAAI,CAAC;oBACV,OAAO,EAAE,qBAAqB;oBAC9B,IAAI,EAAE,gBAAgB;oBACtB,OAAO,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;IAEF,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,MAAM,CAAC,EAAE,EAAE,CAAC;IACrB,CAAC;IAED,OAAO,MAAM,CAAC,GAAG,CACf,IAAI,eAAe,CACjB,+BAA+B,MAAM,CAAC,MAAM,WAAW,EACvD;QACE,OAAO,EAAE,EAAE,MAAM,EAAE;KACpB,CACF,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utilities for @ontrails/core
|
|
3
|
+
*
|
|
4
|
+
* Wraps Zod parsing into Result types and provides JSON Schema conversion
|
|
5
|
+
* for trail input schemas.
|
|
6
|
+
*/
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
import { ValidationError } from './errors.js';
|
|
9
|
+
import { Result } from './result.js';
|
|
10
|
+
type JsonSchema = Record<string, unknown>;
|
|
11
|
+
type JsonSchemaConverter = (schema: z.ZodType) => JsonSchema;
|
|
12
|
+
/** Format each ZodIssue as "path: message" (or just "message" for root). */
|
|
13
|
+
export declare const formatZodIssues: (issues: z.ZodIssue[]) => string[];
|
|
14
|
+
/** Parse unknown data against a Zod schema, returning a Result. */
|
|
15
|
+
export declare const validateInput: <T>(schema: z.ZodType<T>, data: unknown) => Result<T, ValidationError>;
|
|
16
|
+
/** Parse unknown data against a Zod schema, returning a Result suitable for output validation. */
|
|
17
|
+
export declare const validateOutput: <T>(schema: z.ZodType<T>, data: unknown) => Result<T, ValidationError>;
|
|
18
|
+
/**
|
|
19
|
+
* Convert common Zod types to a JSON Schema object.
|
|
20
|
+
*
|
|
21
|
+
* Uses Zod v4's `_zod.def` and `_zod.traits` for introspection.
|
|
22
|
+
* Covers: string, number, boolean, object, array, enum, optional,
|
|
23
|
+
* default, union, literal, nullable, and describe.
|
|
24
|
+
*/
|
|
25
|
+
export declare const zodToJsonSchema: JsonSchemaConverter;
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAerC,KAAK,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC1C,KAAK,mBAAmB,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,KAAK,UAAU,CAAC;AAe7D,4EAA4E;AAC5E,eAAO,MAAM,eAAe,GAAI,QAAQ,CAAC,CAAC,QAAQ,EAAE,KAAG,MAAM,EAIzD,CAAC;AAML,mEAAmE;AACnE,eAAO,MAAM,aAAa,GAAI,CAAC,EAC7B,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,OAAO,KACZ,MAAM,CAAC,CAAC,EAAE,eAAe,CAY3B,CAAC;AAMF,kGAAkG;AAClG,eAAO,MAAM,cAAc,GAAI,CAAC,EAC9B,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,OAAO,KACZ,MAAM,CAAC,CAAC,EAAE,eAAe,CAY3B,CAAC;AAMF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,EAAE,mBA8E7B,CAAC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utilities for @ontrails/core
|
|
3
|
+
*
|
|
4
|
+
* Wraps Zod parsing into Result types and provides JSON Schema conversion
|
|
5
|
+
* for trail input schemas.
|
|
6
|
+
*/
|
|
7
|
+
import { ValidationError } from './errors.js';
|
|
8
|
+
import { Result } from './result.js';
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Internal helpers (defined before usage)
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
const isOptionalLike = (s) => {
|
|
13
|
+
const defType = s._zod.def['type'];
|
|
14
|
+
return defType === 'optional' || defType === 'default';
|
|
15
|
+
};
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Issue formatting
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
/** Format each ZodIssue as "path: message" (or just "message" for root). */
|
|
20
|
+
export const formatZodIssues = (issues) => issues.map((issue) => {
|
|
21
|
+
const path = issue.path.join('.');
|
|
22
|
+
return path ? `${path}: ${issue.message}` : issue.message;
|
|
23
|
+
});
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Input validation
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
/** Parse unknown data against a Zod schema, returning a Result. */
|
|
28
|
+
export const validateInput = (schema, data) => {
|
|
29
|
+
const parsed = schema.safeParse(data);
|
|
30
|
+
if (parsed.success) {
|
|
31
|
+
return Result.ok(parsed.data);
|
|
32
|
+
}
|
|
33
|
+
const messages = formatZodIssues(parsed.error.issues);
|
|
34
|
+
return Result.err(new ValidationError(messages.join('; '), {
|
|
35
|
+
cause: parsed.error,
|
|
36
|
+
context: { issues: parsed.error.issues },
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Output validation
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
/** Parse unknown data against a Zod schema, returning a Result suitable for output validation. */
|
|
43
|
+
export const validateOutput = (schema, data) => {
|
|
44
|
+
const parsed = schema.safeParse(data);
|
|
45
|
+
if (parsed.success) {
|
|
46
|
+
return Result.ok(parsed.data);
|
|
47
|
+
}
|
|
48
|
+
const messages = formatZodIssues(parsed.error.issues);
|
|
49
|
+
return Result.err(new ValidationError(`Output validation failed: ${messages.join('; ')}`, {
|
|
50
|
+
cause: parsed.error,
|
|
51
|
+
context: { issues: parsed.error.issues },
|
|
52
|
+
}));
|
|
53
|
+
};
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Zod → JSON Schema (public API)
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
/**
|
|
58
|
+
* Convert common Zod types to a JSON Schema object.
|
|
59
|
+
*
|
|
60
|
+
* Uses Zod v4's `_zod.def` and `_zod.traits` for introspection.
|
|
61
|
+
* Covers: string, number, boolean, object, array, enum, optional,
|
|
62
|
+
* default, union, literal, nullable, and describe.
|
|
63
|
+
*/
|
|
64
|
+
export const zodToJsonSchema = (schema) => {
|
|
65
|
+
const s = schema;
|
|
66
|
+
const collectObjectFields = (shape) => {
|
|
67
|
+
const properties = {};
|
|
68
|
+
const required = [];
|
|
69
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
70
|
+
properties[key] = zodToJsonSchema(value);
|
|
71
|
+
if (!isOptionalLike(value)) {
|
|
72
|
+
required.push(key);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return { properties, required };
|
|
76
|
+
};
|
|
77
|
+
const convertObject = (value) => {
|
|
78
|
+
const shape = value._zod.def['shape'];
|
|
79
|
+
if (!shape) {
|
|
80
|
+
return { type: 'object' };
|
|
81
|
+
}
|
|
82
|
+
const { properties, required } = collectObjectFields(shape);
|
|
83
|
+
const result = { properties, type: 'object' };
|
|
84
|
+
if (required.length > 0) {
|
|
85
|
+
result['required'] = required;
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
const zodConverters = {
|
|
90
|
+
array: (value) => {
|
|
91
|
+
const element = value._zod.def['element'];
|
|
92
|
+
return { items: zodToJsonSchema(element), type: 'array' };
|
|
93
|
+
},
|
|
94
|
+
boolean: () => ({ type: 'boolean' }),
|
|
95
|
+
default: (value) => {
|
|
96
|
+
const inner = value._zod.def['innerType'];
|
|
97
|
+
const innerSchema = zodToJsonSchema(inner);
|
|
98
|
+
const rawDefault = value._zod.def['defaultValue'];
|
|
99
|
+
innerSchema['default'] =
|
|
100
|
+
typeof rawDefault === 'function' ? rawDefault() : rawDefault;
|
|
101
|
+
return innerSchema;
|
|
102
|
+
},
|
|
103
|
+
enum: (value) => {
|
|
104
|
+
const entries = value._zod.def['entries'];
|
|
105
|
+
return { enum: Object.values(entries), type: 'string' };
|
|
106
|
+
},
|
|
107
|
+
literal: (value) => {
|
|
108
|
+
const values = value._zod.def['values'];
|
|
109
|
+
return { const: values[0] };
|
|
110
|
+
},
|
|
111
|
+
nullable: (value) => {
|
|
112
|
+
const inner = value._zod.def['innerType'];
|
|
113
|
+
return { anyOf: [zodToJsonSchema(inner), { type: 'null' }] };
|
|
114
|
+
},
|
|
115
|
+
number: () => ({ type: 'number' }),
|
|
116
|
+
object: convertObject,
|
|
117
|
+
optional: (value) => {
|
|
118
|
+
const inner = value._zod.def['innerType'];
|
|
119
|
+
return zodToJsonSchema(inner);
|
|
120
|
+
},
|
|
121
|
+
string: () => ({ type: 'string' }),
|
|
122
|
+
union: (value) => {
|
|
123
|
+
const options = value._zod.def['options'];
|
|
124
|
+
return { anyOf: options.map((option) => zodToJsonSchema(option)) };
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
const converter = zodConverters[s._zod.def['type']];
|
|
128
|
+
const base = converter ? converter(s) : {};
|
|
129
|
+
if (s.description) {
|
|
130
|
+
base['description'] = s.description;
|
|
131
|
+
}
|
|
132
|
+
return base;
|
|
133
|
+
};
|
|
134
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAkBrC,8EAA8E;AAC9E,0CAA0C;AAC1C,8EAA8E;AAE9E,MAAM,cAAc,GAAG,CAAC,CAAe,EAAW,EAAE;IAClD,MAAM,OAAO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A2B,CAAC;YACpE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;QACrE,CAAC;KACF,CAAC;IAEF,MAAM,SAAS,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAW,CAAC,CAAC;IAC9D,MAAM,IAAI,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAE3C,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;QAClB,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC;IACtC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace detection utilities.
|
|
3
|
+
*
|
|
4
|
+
* Walks the filesystem to find monorepo workspace roots and provides
|
|
5
|
+
* helpers for working with paths relative to a workspace.
|
|
6
|
+
*/
|
|
7
|
+
import { NotFoundError } from './errors.js';
|
|
8
|
+
import { Result } from './result.js';
|
|
9
|
+
/**
|
|
10
|
+
* Walks up from `startDir` (defaults to `process.cwd()`) looking for a
|
|
11
|
+
* `package.json` that contains a `"workspaces"` field.
|
|
12
|
+
*
|
|
13
|
+
* Returns the directory path of the workspace root on success, or a
|
|
14
|
+
* `NotFoundError` if no workspace root is found.
|
|
15
|
+
*/
|
|
16
|
+
export declare const findWorkspaceRoot: (startDir?: string) => Promise<Result<string, NotFoundError>>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns `true` if `filePath` is inside `workspaceRoot`.
|
|
19
|
+
*/
|
|
20
|
+
export declare const isInsideWorkspace: (filePath: string, workspaceRoot: string) => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the relative path from `workspaceRoot` to `filePath`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const getRelativePath: (filePath: string, workspaceRoot: string) => string;
|
|
25
|
+
//# sourceMappingURL=workspace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace.d.ts","sourceRoot":"","sources":["../src/workspace.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAiBrC;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAC5B,WAAW,MAAM,KAChB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAqBvC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAC5B,UAAU,MAAM,EAChB,eAAe,MAAM,KACpB,OAGF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,MAAM,EAChB,eAAe,MAAM,KACpB,MAA6D,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace detection utilities.
|
|
3
|
+
*
|
|
4
|
+
* Walks the filesystem to find monorepo workspace roots and provides
|
|
5
|
+
* helpers for working with paths relative to a workspace.
|
|
6
|
+
*/
|
|
7
|
+
import { resolve, relative, dirname, join, isAbsolute } from 'node:path';
|
|
8
|
+
import { NotFoundError } from './errors.js';
|
|
9
|
+
import { Result } from './result.js';
|
|
10
|
+
/** Check if a directory has a package.json with a `workspaces` field. */
|
|
11
|
+
const hasWorkspacesField = async (dir) => {
|
|
12
|
+
const pkgPath = join(dir, 'package.json');
|
|
13
|
+
const file = Bun.file(pkgPath);
|
|
14
|
+
if (!(await file.exists())) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const pkg = await file.json();
|
|
19
|
+
return typeof pkg === 'object' && pkg !== null && 'workspaces' in pkg;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Walks up from `startDir` (defaults to `process.cwd()`) looking for a
|
|
27
|
+
* `package.json` that contains a `"workspaces"` field.
|
|
28
|
+
*
|
|
29
|
+
* Returns the directory path of the workspace root on success, or a
|
|
30
|
+
* `NotFoundError` if no workspace root is found.
|
|
31
|
+
*/
|
|
32
|
+
export const findWorkspaceRoot = async (startDir) => {
|
|
33
|
+
let current = resolve(startDir ?? process.cwd());
|
|
34
|
+
// eslint-disable-next-line no-constant-condition
|
|
35
|
+
while (true) {
|
|
36
|
+
if (await hasWorkspacesField(current)) {
|
|
37
|
+
return Result.ok(current);
|
|
38
|
+
}
|
|
39
|
+
const parent = dirname(current);
|
|
40
|
+
if (parent === current) {
|
|
41
|
+
return Result.err(new NotFoundError(`No workspace root found from "${startDir ?? process.cwd()}"`));
|
|
42
|
+
}
|
|
43
|
+
current = parent;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` if `filePath` is inside `workspaceRoot`.
|
|
48
|
+
*/
|
|
49
|
+
export const isInsideWorkspace = (filePath, workspaceRoot) => {
|
|
50
|
+
const rel = relative(resolve(workspaceRoot), resolve(filePath));
|
|
51
|
+
return rel !== '' && !rel.startsWith('..') && !isAbsolute(rel);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Returns the relative path from `workspaceRoot` to `filePath`.
|
|
55
|
+
*/
|
|
56
|
+
export const getRelativePath = (filePath, workspaceRoot) => relative(resolve(workspaceRoot), resolve(filePath));
|
|
57
|
+
//# sourceMappingURL=workspace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace.js","sourceRoot":"","sources":["../src/workspace.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,WAAW,CAA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AAC;QAED,OAAO,GAAG,MAAM,CAAC;IACnB,CAAC;AACH,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,QAAgB,EAChB,aAAqB,EACZ,EAAE;IACX,MAAM,GAAG,GAAG,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAChE,OAAO,GAAG,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;AACjE,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,QAAgB,EAChB,aAAqB,EACb,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ontrails/core",
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./src/index.ts",
|
|
7
|
+
"./patterns": "./src/patterns/index.ts",
|
|
8
|
+
"./redaction": "./src/redaction/index.ts",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc -b",
|
|
13
|
+
"test": "bun test",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"lint": "oxlint ./src",
|
|
16
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"zod": "catalog:"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { createBlobRef, isBlobRef } from '../blob-ref';
|
|
4
|
+
|
|
5
|
+
describe('BlobRef', () => {
|
|
6
|
+
const sampleData = new Uint8Array([137, 80, 78, 71]);
|
|
7
|
+
|
|
8
|
+
describe('createBlobRef', () => {
|
|
9
|
+
test('returns a frozen object with the provided fields', () => {
|
|
10
|
+
const ref = createBlobRef({
|
|
11
|
+
data: sampleData,
|
|
12
|
+
mimeType: 'image/png',
|
|
13
|
+
name: 'image.png',
|
|
14
|
+
size: 4,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(ref.name).toBe('image.png');
|
|
18
|
+
expect(ref.mimeType).toBe('image/png');
|
|
19
|
+
expect(ref.size).toBe(4);
|
|
20
|
+
expect(ref.data).toBe(sampleData);
|
|
21
|
+
expect(Object.isFrozen(ref)).toBe(true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('works with ReadableStream data', () => {
|
|
25
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
26
|
+
start(controller) {
|
|
27
|
+
controller.enqueue(sampleData);
|
|
28
|
+
controller.close();
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const ref = createBlobRef({
|
|
33
|
+
data: stream,
|
|
34
|
+
mimeType: 'video/mp4',
|
|
35
|
+
name: 'video.mp4',
|
|
36
|
+
size: 1024,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(ref.data).toBeInstanceOf(ReadableStream);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('isBlobRef', () => {
|
|
44
|
+
test('returns true for a valid BlobRef with Uint8Array data', () => {
|
|
45
|
+
const ref = createBlobRef({
|
|
46
|
+
data: sampleData,
|
|
47
|
+
mimeType: 'application/octet-stream',
|
|
48
|
+
name: 'file.bin',
|
|
49
|
+
size: 4,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(isBlobRef(ref)).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('returns true for a valid BlobRef with ReadableStream data', () => {
|
|
56
|
+
const stream = new ReadableStream<Uint8Array>();
|
|
57
|
+
const ref = createBlobRef({
|
|
58
|
+
data: stream,
|
|
59
|
+
mimeType: 'application/octet-stream',
|
|
60
|
+
name: 'file.bin',
|
|
61
|
+
size: 0,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(isBlobRef(ref)).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('returns true for a plain object matching the shape', () => {
|
|
68
|
+
expect(
|
|
69
|
+
isBlobRef({
|
|
70
|
+
data: new Uint8Array(),
|
|
71
|
+
mimeType: 'application/pdf',
|
|
72
|
+
name: 'doc.pdf',
|
|
73
|
+
size: 999,
|
|
74
|
+
})
|
|
75
|
+
).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('returns false for null', () => {
|
|
79
|
+
expect(isBlobRef(null)).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('returns false for a string', () => {
|
|
83
|
+
expect(isBlobRef('not a blob')).toBe(false);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('returns false when name is missing', () => {
|
|
87
|
+
expect(
|
|
88
|
+
isBlobRef({ data: new Uint8Array(), mimeType: 'text/plain', size: 0 })
|
|
89
|
+
).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('returns false when data is a plain object', () => {
|
|
93
|
+
expect(
|
|
94
|
+
isBlobRef({
|
|
95
|
+
data: {},
|
|
96
|
+
mimeType: 'text/plain',
|
|
97
|
+
name: 'x',
|
|
98
|
+
size: 0,
|
|
99
|
+
})
|
|
100
|
+
).toBe(false);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
brand,
|
|
5
|
+
unbrand,
|
|
6
|
+
uuid,
|
|
7
|
+
email,
|
|
8
|
+
nonEmptyString,
|
|
9
|
+
positiveInt,
|
|
10
|
+
shortId,
|
|
11
|
+
hashId,
|
|
12
|
+
} from '../branded';
|
|
13
|
+
|
|
14
|
+
describe('branded', () => {
|
|
15
|
+
describe('brand / unbrand', () => {
|
|
16
|
+
test('brand wraps and unbrand unwraps', () => {
|
|
17
|
+
const branded = brand('Tag', 'hello');
|
|
18
|
+
expect(unbrand(branded)).toBe('hello');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('branded value is still usable as base type at runtime', () => {
|
|
22
|
+
const branded = brand('PositiveInt', 42);
|
|
23
|
+
expect(branded + 1).toBe(43);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('uuid()', () => {
|
|
28
|
+
test('accepts a valid v4 UUID', () => {
|
|
29
|
+
const result = uuid('550e8400-e29b-41d4-a716-446655440000');
|
|
30
|
+
expect(result.isOk()).toBe(true);
|
|
31
|
+
const value: string = unbrand(result.unwrap());
|
|
32
|
+
expect(value).toBe('550e8400-e29b-41d4-a716-446655440000');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('rejects an empty string', () => {
|
|
36
|
+
const result = uuid('');
|
|
37
|
+
expect(result.isErr()).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('rejects a malformed UUID', () => {
|
|
41
|
+
const result = uuid('not-a-uuid');
|
|
42
|
+
expect(result.isErr()).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('rejects UUID missing hyphens', () => {
|
|
46
|
+
const result = uuid('550e8400e29b41d4a716446655440000');
|
|
47
|
+
expect(result.isErr()).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('email()', () => {
|
|
52
|
+
test('accepts a simple email', () => {
|
|
53
|
+
const result = email('test@example.com');
|
|
54
|
+
expect(result.isOk()).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('rejects missing @', () => {
|
|
58
|
+
const result = email('testexample.com');
|
|
59
|
+
expect(result.isErr()).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('rejects missing domain', () => {
|
|
63
|
+
const result = email('test@');
|
|
64
|
+
expect(result.isErr()).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('rejects empty string', () => {
|
|
68
|
+
const result = email('');
|
|
69
|
+
expect(result.isErr()).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('nonEmptyString()', () => {
|
|
74
|
+
test('accepts a non-empty string', () => {
|
|
75
|
+
const result = nonEmptyString('hello');
|
|
76
|
+
expect(result.isOk()).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('rejects an empty string', () => {
|
|
80
|
+
const result = nonEmptyString('');
|
|
81
|
+
expect(result.isErr()).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('positiveInt()', () => {
|
|
86
|
+
test('accepts 1', () => {
|
|
87
|
+
const result = positiveInt(1);
|
|
88
|
+
expect(result.isOk()).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('accepts a large integer', () => {
|
|
92
|
+
const result = positiveInt(1_000_000);
|
|
93
|
+
expect(result.isOk()).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('rejects 0', () => {
|
|
97
|
+
const result = positiveInt(0);
|
|
98
|
+
expect(result.isErr()).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('rejects negative numbers', () => {
|
|
102
|
+
const result = positiveInt(-5);
|
|
103
|
+
expect(result.isErr()).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('rejects non-integers', () => {
|
|
107
|
+
const result = positiveInt(1.5);
|
|
108
|
+
expect(result.isErr()).toBe(true);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('shortId()', () => {
|
|
113
|
+
test('returns a string of default length 8', () => {
|
|
114
|
+
const id = shortId();
|
|
115
|
+
expect(id).toHaveLength(8);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('respects custom length', () => {
|
|
119
|
+
const id = shortId(16);
|
|
120
|
+
expect(id).toHaveLength(16);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('contains only alphanumeric characters', () => {
|
|
124
|
+
const id = shortId(100);
|
|
125
|
+
expect(id).toMatch(/^[A-Za-z0-9]+$/);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('produces unique values', () => {
|
|
129
|
+
const ids = new Set(Array.from({ length: 50 }, () => shortId()));
|
|
130
|
+
expect(ids.size).toBe(50);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('hashId()', () => {
|
|
135
|
+
test('returns a hex string', () => {
|
|
136
|
+
const id = hashId('test');
|
|
137
|
+
expect(id).toMatch(/^[0-9a-f]{8}$/);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('is deterministic', () => {
|
|
141
|
+
expect(hashId('hello')).toBe(hashId('hello'));
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('different inputs produce different outputs', () => {
|
|
145
|
+
expect(hashId('a')).not.toBe(hashId('b'));
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|