@agentuity/schema 0.0.69
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/AGENTS.md +86 -0
- package/README.md +323 -0
- package/dist/base.d.ts +111 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +93 -0
- package/dist/base.js.map +1 -0
- package/dist/coerce/boolean.d.ts +37 -0
- package/dist/coerce/boolean.d.ts.map +1 -0
- package/dist/coerce/boolean.js +49 -0
- package/dist/coerce/boolean.js.map +1 -0
- package/dist/coerce/date.d.ts +36 -0
- package/dist/coerce/date.d.ts.map +1 -0
- package/dist/coerce/date.js +60 -0
- package/dist/coerce/date.js.map +1 -0
- package/dist/coerce/number.d.ts +36 -0
- package/dist/coerce/number.d.ts.map +1 -0
- package/dist/coerce/number.js +59 -0
- package/dist/coerce/number.js.map +1 -0
- package/dist/coerce/string.d.ts +35 -0
- package/dist/coerce/string.d.ts.map +1 -0
- package/dist/coerce/string.js +47 -0
- package/dist/coerce/string.js.map +1 -0
- package/dist/complex/array.d.ts +56 -0
- package/dist/complex/array.d.ts.map +1 -0
- package/dist/complex/array.js +96 -0
- package/dist/complex/array.js.map +1 -0
- package/dist/complex/object.d.ts +76 -0
- package/dist/complex/object.d.ts.map +1 -0
- package/dist/complex/object.js +104 -0
- package/dist/complex/object.js.map +1 -0
- package/dist/complex/record.d.ts +53 -0
- package/dist/complex/record.d.ts.map +1 -0
- package/dist/complex/record.js +109 -0
- package/dist/complex/record.js.map +1 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +128 -0
- package/dist/index.js.map +1 -0
- package/dist/json-schema.d.ts +60 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/json-schema.js +280 -0
- package/dist/json-schema.js.map +1 -0
- package/dist/primitives/any.d.ts +44 -0
- package/dist/primitives/any.d.ts.map +1 -0
- package/dist/primitives/any.js +57 -0
- package/dist/primitives/any.js.map +1 -0
- package/dist/primitives/boolean.d.ts +39 -0
- package/dist/primitives/boolean.d.ts.map +1 -0
- package/dist/primitives/boolean.js +53 -0
- package/dist/primitives/boolean.js.map +1 -0
- package/dist/primitives/null.d.ts +26 -0
- package/dist/primitives/null.d.ts.map +1 -0
- package/dist/primitives/null.js +40 -0
- package/dist/primitives/null.js.map +1 -0
- package/dist/primitives/number.d.ts +87 -0
- package/dist/primitives/number.d.ts.map +1 -0
- package/dist/primitives/number.js +129 -0
- package/dist/primitives/number.js.map +1 -0
- package/dist/primitives/string.d.ts +64 -0
- package/dist/primitives/string.d.ts.map +1 -0
- package/dist/primitives/string.js +102 -0
- package/dist/primitives/string.js.map +1 -0
- package/dist/primitives/undefined.d.ts +26 -0
- package/dist/primitives/undefined.d.ts.map +1 -0
- package/dist/primitives/undefined.js +40 -0
- package/dist/primitives/undefined.js.map +1 -0
- package/dist/primitives/unknown.d.ts +47 -0
- package/dist/primitives/unknown.d.ts.map +1 -0
- package/dist/primitives/unknown.js +56 -0
- package/dist/primitives/unknown.js.map +1 -0
- package/dist/utils/literal.d.ts +47 -0
- package/dist/utils/literal.d.ts.map +1 -0
- package/dist/utils/literal.js +64 -0
- package/dist/utils/literal.js.map +1 -0
- package/dist/utils/nullable.d.ts +50 -0
- package/dist/utils/nullable.d.ts.map +1 -0
- package/dist/utils/nullable.js +69 -0
- package/dist/utils/nullable.js.map +1 -0
- package/dist/utils/optional.d.ts +50 -0
- package/dist/utils/optional.d.ts.map +1 -0
- package/dist/utils/optional.js +69 -0
- package/dist/utils/optional.js.map +1 -0
- package/dist/utils/union.d.ts +60 -0
- package/dist/utils/union.d.ts.map +1 -0
- package/dist/utils/union.js +87 -0
- package/dist/utils/union.js.map +1 -0
- package/package.json +39 -0
- package/src/__tests__/coerce.test.ts +88 -0
- package/src/__tests__/complex.test.ts +124 -0
- package/src/__tests__/errors.test.ts +129 -0
- package/src/__tests__/json-schema.test.ts +138 -0
- package/src/__tests__/primitives.test.ts +184 -0
- package/src/__tests__/type-inference.test.ts +68 -0
- package/src/__tests__/utils.test.ts +100 -0
- package/src/base.ts +185 -0
- package/src/coerce/boolean.ts +56 -0
- package/src/coerce/date.ts +68 -0
- package/src/coerce/number.ts +67 -0
- package/src/coerce/string.ts +54 -0
- package/src/complex/array.ts +108 -0
- package/src/complex/object.ts +141 -0
- package/src/complex/record.ts +129 -0
- package/src/index.ts +177 -0
- package/src/json-schema.ts +331 -0
- package/src/primitives/any.ts +64 -0
- package/src/primitives/boolean.ts +60 -0
- package/src/primitives/null.ts +47 -0
- package/src/primitives/number.ts +141 -0
- package/src/primitives/string.ts +113 -0
- package/src/primitives/undefined.ts +47 -0
- package/src/primitives/unknown.ts +63 -0
- package/src/utils/literal.ts +71 -0
- package/src/utils/nullable.ts +80 -0
- package/src/utils/optional.ts +80 -0
- package/src/utils/union.ts +103 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Schema, Infer } from '../base';
|
|
2
|
+
import { createIssue, failure, createParseMethods } from '../base';
|
|
3
|
+
import { optional } from '../utils/optional';
|
|
4
|
+
import { nullable } from '../utils/nullable';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
type InferUnion<T extends Schema<any, any>[]> = Infer<T[number]>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Schema for union types (one of multiple possible schemas).
|
|
11
|
+
* Validates against each schema until one succeeds.
|
|
12
|
+
*
|
|
13
|
+
* @template T - Array of schema types
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const idSchema = s.union(s.string(), s.number());
|
|
18
|
+
* idSchema.parse('abc123'); // 'abc123'
|
|
19
|
+
* idSchema.parse(123); // 123
|
|
20
|
+
*
|
|
21
|
+
* const roleSchema = s.union(
|
|
22
|
+
* s.literal('admin'),
|
|
23
|
+
* s.literal('user'),
|
|
24
|
+
* s.literal('guest')
|
|
25
|
+
* );
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
export class UnionSchema<T extends Schema<any, any>[]>
|
|
30
|
+
implements Schema<InferUnion<T>, InferUnion<T>>
|
|
31
|
+
{
|
|
32
|
+
description?: string;
|
|
33
|
+
private parseMethods = createParseMethods<InferUnion<T>>();
|
|
34
|
+
|
|
35
|
+
constructor(private schemas: T) {}
|
|
36
|
+
|
|
37
|
+
readonly '~standard' = {
|
|
38
|
+
version: 1 as const,
|
|
39
|
+
vendor: 'agentuity',
|
|
40
|
+
validate: (value: unknown) => {
|
|
41
|
+
const allIssues: ReturnType<typeof createIssue>[] = [];
|
|
42
|
+
|
|
43
|
+
for (const schema of this.schemas) {
|
|
44
|
+
const result = schema['~standard'].validate(value);
|
|
45
|
+
|
|
46
|
+
// Only support synchronous validation for now
|
|
47
|
+
if (result instanceof Promise) {
|
|
48
|
+
throw new Error('Async validation not supported');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!result.issues) {
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
+
return result as any;
|
|
54
|
+
}
|
|
55
|
+
allIssues.push(...result.issues);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return failure([
|
|
59
|
+
createIssue(
|
|
60
|
+
`Value did not match any of the union types (${allIssues.length} validation errors)`
|
|
61
|
+
),
|
|
62
|
+
]);
|
|
63
|
+
},
|
|
64
|
+
types: undefined as unknown as { input: InferUnion<T>; output: InferUnion<T> },
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
describe(description: string): this {
|
|
68
|
+
this.description = description;
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
optional() {
|
|
73
|
+
return optional(this);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
nullable() {
|
|
77
|
+
return nullable(this);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
parse = this.parseMethods.parse;
|
|
81
|
+
safeParse = this.parseMethods.safeParse;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Create a union schema (one of multiple possible types).
|
|
86
|
+
*
|
|
87
|
+
* @param schemas - Variable number of schemas to union
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const idSchema = s.union(s.string(), s.number());
|
|
92
|
+
*
|
|
93
|
+
* const roleSchema = s.union(
|
|
94
|
+
* s.literal('admin'),
|
|
95
|
+
* s.literal('user'),
|
|
96
|
+
* s.literal('guest')
|
|
97
|
+
* );
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
export function union<T extends Schema<any, any>[]>(...schemas: T): UnionSchema<T> {
|
|
102
|
+
return new UnionSchema(schemas);
|
|
103
|
+
}
|