@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,104 @@
|
|
|
1
|
+
import { createIssue, failure, success, createParseMethods } from '../base';
|
|
2
|
+
import { optional } from '../utils/optional';
|
|
3
|
+
import { nullable } from '../utils/nullable';
|
|
4
|
+
/**
|
|
5
|
+
* Schema for validating objects with typed properties.
|
|
6
|
+
* Validates each property according to its schema and collects all validation errors.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The object shape definition
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const userSchema = s.object({
|
|
13
|
+
* name: s.string(),
|
|
14
|
+
* age: s.number(),
|
|
15
|
+
* email: s.string()
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* const user = userSchema.parse({
|
|
19
|
+
* name: 'John',
|
|
20
|
+
* age: 30,
|
|
21
|
+
* email: 'john@example.com'
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class ObjectSchema {
|
|
26
|
+
shape;
|
|
27
|
+
description;
|
|
28
|
+
parseMethods = createParseMethods();
|
|
29
|
+
constructor(shape) {
|
|
30
|
+
this.shape = shape;
|
|
31
|
+
}
|
|
32
|
+
'~standard' = {
|
|
33
|
+
version: 1,
|
|
34
|
+
vendor: 'agentuity',
|
|
35
|
+
validate: (value) => {
|
|
36
|
+
if (value === null) {
|
|
37
|
+
return failure([createIssue('Expected object, got null')]);
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
return failure([createIssue('Expected object, got array')]);
|
|
41
|
+
}
|
|
42
|
+
if (typeof value !== 'object') {
|
|
43
|
+
return failure([createIssue(`Expected object, got ${typeof value}`)]);
|
|
44
|
+
}
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
const result = {};
|
|
47
|
+
const issues = [];
|
|
48
|
+
for (const [key, schema] of Object.entries(this.shape)) {
|
|
49
|
+
const fieldValue = value[key];
|
|
50
|
+
const validation = schema['~standard'].validate(fieldValue);
|
|
51
|
+
// Only support synchronous validation for now
|
|
52
|
+
if (validation instanceof Promise) {
|
|
53
|
+
throw new Error('Async validation not supported');
|
|
54
|
+
}
|
|
55
|
+
if (validation.issues) {
|
|
56
|
+
for (const issue of validation.issues) {
|
|
57
|
+
issues.push(createIssue(issue.message, issue.path ? [key, ...issue.path] : [key]));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
result[key] = validation.value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (issues.length > 0) {
|
|
65
|
+
return failure(issues);
|
|
66
|
+
}
|
|
67
|
+
return success(result);
|
|
68
|
+
},
|
|
69
|
+
types: undefined,
|
|
70
|
+
};
|
|
71
|
+
describe(description) {
|
|
72
|
+
this.description = description;
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
optional() {
|
|
76
|
+
return optional(this);
|
|
77
|
+
}
|
|
78
|
+
nullable() {
|
|
79
|
+
return nullable(this);
|
|
80
|
+
}
|
|
81
|
+
parse = this.parseMethods.parse;
|
|
82
|
+
safeParse = this.parseMethods.safeParse;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create an object schema with typed properties.
|
|
86
|
+
*
|
|
87
|
+
* @param shape - Object defining the schema for each property
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const userSchema = s.object({
|
|
92
|
+
* name: s.string().describe('Full name'),
|
|
93
|
+
* age: s.number().describe('Age in years'),
|
|
94
|
+
* email: s.optional(s.string())
|
|
95
|
+
* });
|
|
96
|
+
*
|
|
97
|
+
* type User = s.infer<typeof userSchema>;
|
|
98
|
+
* const user = userSchema.parse(data);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function object(shape) {
|
|
102
|
+
return new ObjectSchema(shape);
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/complex/object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,QAAQ,EAAkB,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAyB7C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,YAAY;IAMJ;IAHpB,WAAW,CAAU;IACb,YAAY,GAAG,kBAAkB,EAAuB,CAAC;IAEjE,YAAoB,KAAQ;QAAR,UAAK,GAAL,KAAK,CAAG;IAAG,CAAC;IAEvB,WAAW,GAAG;QACtB,OAAO,EAAE,CAAU;QACnB,MAAM,EAAE,WAAW;QACnB,QAAQ,EAAE,CAAC,KAAc,EAAE,EAAE;YAC5B,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACpB,OAAO,OAAO,CAAC,CAAC,WAAW,CAAC,2BAA2B,CAAC,CAAC,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,KAAK,CAAC,OAAO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C;gBACvB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;YACxB,CAAC;YAED,OAAO,OAAO,CAAC,MAA6B,CAAC,CAAC;QAC/C,CAAC;QACD,KAAK,EAAE,SAAmF;KAC1F,CAAC;IAEF,QAAQ,CAAC,WAAmB;QAC3B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,QAAQ;QACP,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED,QAAQ;QACP,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC;IAChC,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC;CACxC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,MAAM,CAAwB,KAAQ;IACrD,OAAO,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Schema, Infer } from '../base';
|
|
2
|
+
/**
|
|
3
|
+
* Schema for validating records (objects with string keys and typed values).
|
|
4
|
+
* Like TypeScript's Record<string, T> type.
|
|
5
|
+
*
|
|
6
|
+
* @template K - The key schema (must be string)
|
|
7
|
+
* @template V - The value schema
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const schema = s.record(s.string(), s.number());
|
|
12
|
+
* schema.parse({ a: 1, b: 2 }); // { a: 1, b: 2 }
|
|
13
|
+
* schema.parse({ a: 'invalid' }); // throws ValidationError
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class RecordSchema<K extends Schema<string, string>, V extends Schema<any, any>> implements Schema<Record<Infer<K>, Infer<V>>, Record<Infer<K>, Infer<V>>> {
|
|
17
|
+
private keySchema;
|
|
18
|
+
private valueSchema;
|
|
19
|
+
description?: string;
|
|
20
|
+
private recordParseMethods;
|
|
21
|
+
constructor(keySchema: K, valueSchema: V);
|
|
22
|
+
readonly '~standard': {
|
|
23
|
+
version: 1;
|
|
24
|
+
vendor: string;
|
|
25
|
+
validate: (value: unknown) => import("@agentuity/core").StandardSchemaV1.FailureResult | import("@agentuity/core").StandardSchemaV1.SuccessResult<Record<Infer<K>, Infer<V>>>;
|
|
26
|
+
types: {
|
|
27
|
+
input: Record<Infer<K>, Infer<V>>;
|
|
28
|
+
output: Record<Infer<K>, Infer<V>>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
describe(description: string): this;
|
|
32
|
+
optional(): import("..").OptionalSchema<this>;
|
|
33
|
+
nullable(): import("..").NullableSchema<this>;
|
|
34
|
+
parse: (this: Schema<any, Record<Infer<K>, Infer<V>>>, value: unknown) => Record<Infer<K>, Infer<V>>;
|
|
35
|
+
safeParse: (this: Schema<any, Record<Infer<K>, Infer<V>>>, value: unknown) => import("..").SafeParseResult<Record<Infer<K>, Infer<V>>>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a record schema for objects with string keys and typed values.
|
|
39
|
+
*
|
|
40
|
+
* @param keySchema - Schema for keys (typically s.string())
|
|
41
|
+
* @param valueSchema - Schema for values
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const configSchema = s.record(s.string(), s.number());
|
|
46
|
+
* const config = configSchema.parse({ timeout: 30, retries: 3 });
|
|
47
|
+
*
|
|
48
|
+
* const metadataSchema = s.record(s.string(), s.unknown());
|
|
49
|
+
* const metadata = metadataSchema.parse({ any: 'data', here: 123 });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function record<K extends Schema<string, string>, V extends Schema<any, any>>(keySchema: K, valueSchema: V): RecordSchema<K, V>;
|
|
53
|
+
//# sourceMappingURL=record.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.d.ts","sour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CAAC,CAEpB"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { createIssue, failure, success, createParseMethods } from '../base';
|
|
2
|
+
import { optional } from '../utils/optional';
|
|
3
|
+
import { nullable } from '../utils/nullable';
|
|
4
|
+
/**
|
|
5
|
+
* Schema for validating records (objects with string keys and typed values).
|
|
6
|
+
* Like TypeScript's Record<string, T> type.
|
|
7
|
+
*
|
|
8
|
+
* @template K - The key schema (must be string)
|
|
9
|
+
* @template V - The value schema
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const schema = s.record(s.string(), s.number());
|
|
14
|
+
* schema.parse({ a: 1, b: 2 }); // { a: 1, b: 2 }
|
|
15
|
+
* schema.parse({ a: 'invalid' }); // throws ValidationError
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
export class RecordSchema {
|
|
20
|
+
keySchema;
|
|
21
|
+
valueSchema;
|
|
22
|
+
description;
|
|
23
|
+
recordParseMethods = createParseMethods();
|
|
24
|
+
constructor(keySchema, valueSchema) {
|
|
25
|
+
this.keySchema = keySchema;
|
|
26
|
+
this.valueSchema = valueSchema;
|
|
27
|
+
}
|
|
28
|
+
'~standard' = {
|
|
29
|
+
version: 1,
|
|
30
|
+
vendor: 'agentuity',
|
|
31
|
+
validate: (value) => {
|
|
32
|
+
if (value === null) {
|
|
33
|
+
return failure([createIssue('Expected record, got null')]);
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(value)) {
|
|
36
|
+
return failure([createIssue('Expected record, got array')]);
|
|
37
|
+
}
|
|
38
|
+
if (typeof value !== 'object') {
|
|
39
|
+
return failure([createIssue(`Expected record, got ${typeof value}`)]);
|
|
40
|
+
}
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
+
const result = {};
|
|
43
|
+
const issues = [];
|
|
44
|
+
for (const [key, val] of Object.entries(value)) {
|
|
45
|
+
// Validate key
|
|
46
|
+
const keyValidation = this.keySchema['~standard'].validate(key);
|
|
47
|
+
if (keyValidation instanceof Promise) {
|
|
48
|
+
throw new Error('Async validation not supported');
|
|
49
|
+
}
|
|
50
|
+
if (keyValidation.issues) {
|
|
51
|
+
for (const issue of keyValidation.issues) {
|
|
52
|
+
issues.push(createIssue(`Invalid key "${key}": ${issue.message}`, [key]));
|
|
53
|
+
}
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
// Validate value
|
|
57
|
+
const valueValidation = this.valueSchema['~standard'].validate(val);
|
|
58
|
+
if (valueValidation instanceof Promise) {
|
|
59
|
+
throw new Error('Async validation not supported');
|
|
60
|
+
}
|
|
61
|
+
if (valueValidation.issues) {
|
|
62
|
+
for (const issue of valueValidation.issues) {
|
|
63
|
+
issues.push(createIssue(issue.message, issue.path ? [key, ...issue.path] : [key]));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
result[key] = valueValidation.value;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (issues.length > 0) {
|
|
71
|
+
return failure(issues);
|
|
72
|
+
}
|
|
73
|
+
return success(result);
|
|
74
|
+
},
|
|
75
|
+
types: undefined,
|
|
76
|
+
};
|
|
77
|
+
describe(description) {
|
|
78
|
+
this.description = description;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
optional() {
|
|
82
|
+
return optional(this);
|
|
83
|
+
}
|
|
84
|
+
nullable() {
|
|
85
|
+
return nullable(this);
|
|
86
|
+
}
|
|
87
|
+
parse = this.recordParseMethods.parse;
|
|
88
|
+
safeParse = this.recordParseMethods.safeParse;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Create a record schema for objects with string keys and typed values.
|
|
92
|
+
*
|
|
93
|
+
* @param keySchema - Schema for keys (typically s.string())
|
|
94
|
+
* @param valueSchema - Schema for values
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const configSchema = s.record(s.string(), s.number());
|
|
99
|
+
* const config = configSchema.parse({ timeout: 30, retries: 3 });
|
|
100
|
+
*
|
|
101
|
+
* const metadataSchema = s.record(s.string(), s.unknown());
|
|
102
|
+
* const metadata = metadataSchema.parse({ any: 'data', here: 123 });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
export function record(keySchema, valueSchema) {
|
|
107
|
+
return new RecordSchema(keySchema, valueSchema);
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.js","sourceRoot":"","sources":["../../src/complex/record.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C;;;;;;;;;;;;;GAaG;AACH,8DAA8D;AAC9D,MAAM,OAAO,YAAY;IAOf;IACA;IALT,WAAW,CAAU;IACb,kBAAkB,GAAG,kBAAkB,EAA8B,CAAC;IAE9E,YACS,SAAY,EACZ,WAAc;QADd,cAAS,GAAT,SAAS,CAAG;QACZ,gBAAW,GAAX,WAAW,CAAG;IACpB,CAAC;IAEK,WAAW,GAAG;QACtB,OAAO,EAAE,CAAU;QACnB,MAAM,EAAE,WAAW;QACnB,QAAQ,EAAE,CAAC,KAAc,EAAE,EAAE;YAC5B,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACpB,OAAO,OAAO,CAAC,CAAC,WAAW,CAAC,2BAA2B,CAAC,CAAC,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC1B,OAAO,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,EAAE,CAAC;wBAC5C,MAAM,CAAC,IAAI,CACV,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CACrE,CAAC;oBACH,CAAC;gBACF,CAAC;qBAAM,CAAC;oBACP,MAAM,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,KAAK,CAAC;gBACrC,CAAC;YACF,CAAC;YAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACvB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;YACxB,CAAC;YAED,OAAO,OAAO,CAAC,MAAoC,CAAC,CAAC;QACtD,CAAC;QACD,KAAK,EAAE,SAGN;KACD,CAAC;IAEF,QAAQ,CAAC,WAAmB;QAC3B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,QAAQ;QACP,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED,QAAQ;QACP,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED,KAAK,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC;IACtC,SAAS,GAAG,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC;CAC9C;AAED;;;;;;;;;;;;;;GAcG;AACH,8DAA8D;AAC9D,MAAM,UAAU,MAAM,CACrB,SAAY,EACZ,WAAc;IAEd,OAAO,IAAI,YAAY,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;AACjD,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
export type { Schema, Infer, InferInput, InferOutput, ValidationIssue, ValidationResult, SafeParseResult, SafeParseSuccess, SafeParseError, } from './base';
|
|
2
|
+
export { createIssue, success, failure, ValidationError } from './base';
|
|
3
|
+
export { StringSchema, string } from './primitives/string';
|
|
4
|
+
export { NumberSchema, number } from './primitives/number';
|
|
5
|
+
export { BooleanSchema, boolean } from './primitives/boolean';
|
|
6
|
+
export { NullSchema, null_ } from './primitives/null';
|
|
7
|
+
export { UndefinedSchema, undefined_ } from './primitives/undefined';
|
|
8
|
+
export { UnknownSchema, unknown } from './primitives/unknown';
|
|
9
|
+
export { AnySchema, any } from './primitives/any';
|
|
10
|
+
export { ObjectSchema, object } from './complex/object';
|
|
11
|
+
export { ArraySchema, array } from './complex/array';
|
|
12
|
+
export { RecordSchema, record } from './complex/record';
|
|
13
|
+
export { LiteralSchema, literal } from './utils/literal';
|
|
14
|
+
export { OptionalSchema, optional } from './utils/optional';
|
|
15
|
+
export { NullableSchema, nullable } from './utils/nullable';
|
|
16
|
+
export { UnionSchema, union } from './utils/union';
|
|
17
|
+
export { toJSONSchema, fromJSONSchema, type JSONSchema } from './json-schema';
|
|
18
|
+
export { CoerceStringSchema, coerceString } from './coerce/string';
|
|
19
|
+
export { CoerceNumberSchema, coerceNumber } from './coerce/number';
|
|
20
|
+
export { CoerceBooleanSchema, coerceBoolean } from './coerce/boolean';
|
|
21
|
+
export { CoerceDateSchema, coerceDate } from './coerce/date';
|
|
22
|
+
import { string } from './primitives/string';
|
|
23
|
+
import { number } from './primitives/number';
|
|
24
|
+
import { boolean } from './primitives/boolean';
|
|
25
|
+
import { null_ } from './primitives/null';
|
|
26
|
+
import { undefined_ } from './primitives/undefined';
|
|
27
|
+
import { unknown } from './primitives/unknown';
|
|
28
|
+
import { any } from './primitives/any';
|
|
29
|
+
import { object } from './complex/object';
|
|
30
|
+
import { array } from './complex/array';
|
|
31
|
+
import { record } from './complex/record';
|
|
32
|
+
import { literal } from './utils/literal';
|
|
33
|
+
import { optional } from './utils/optional';
|
|
34
|
+
import { nullable } from './utils/nullable';
|
|
35
|
+
import { union } from './utils/union';
|
|
36
|
+
import { toJSONSchema, fromJSONSchema } from './json-schema';
|
|
37
|
+
import { coerceString } from './coerce/string';
|
|
38
|
+
import { coerceNumber } from './coerce/number';
|
|
39
|
+
import { coerceBoolean } from './coerce/boolean';
|
|
40
|
+
import { coerceDate } from './coerce/date';
|
|
41
|
+
import type { Infer as InferType, Schema } from './base';
|
|
42
|
+
/**
|
|
43
|
+
* Create an enum schema (union of literal values).
|
|
44
|
+
* Shorthand for creating a union of literals.
|
|
45
|
+
*
|
|
46
|
+
* @param values - Array of literal values
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const roleSchema = s.enum(['admin', 'user', 'guest']);
|
|
51
|
+
* const role = roleSchema.parse('admin'); // 'admin'
|
|
52
|
+
*
|
|
53
|
+
* // Equivalent to:
|
|
54
|
+
* s.union(s.literal('admin'), s.literal('user'), s.literal('guest'))
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function enumSchema<T extends readonly [string | number | boolean, ...(string | number | boolean)[]]>(values: T): import(".").UnionSchema<import(".").LiteralSchema<any>[]>;
|
|
58
|
+
/**
|
|
59
|
+
* Main schema builder object.
|
|
60
|
+
* Provides access to all schema types and utilities.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { s } from '@agentuity/schema';
|
|
65
|
+
*
|
|
66
|
+
* // Define a schema
|
|
67
|
+
* const User = s.object({
|
|
68
|
+
* name: s.string(),
|
|
69
|
+
* age: s.number(),
|
|
70
|
+
* role: s.enum(['admin', 'user'])
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* // Extract type
|
|
74
|
+
* type User = s.infer<typeof User>;
|
|
75
|
+
*
|
|
76
|
+
* // Parse data
|
|
77
|
+
* const user = User.parse(data);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare const s: {
|
|
81
|
+
/** Create a string schema */
|
|
82
|
+
string: typeof string;
|
|
83
|
+
/** Create a number schema */
|
|
84
|
+
number: typeof number;
|
|
85
|
+
/** Create a boolean schema */
|
|
86
|
+
boolean: typeof boolean;
|
|
87
|
+
/** Create a null schema */
|
|
88
|
+
null: typeof null_;
|
|
89
|
+
/** Create an undefined schema */
|
|
90
|
+
undefined: typeof undefined_;
|
|
91
|
+
/** Create an unknown schema (accepts any value) */
|
|
92
|
+
unknown: typeof unknown;
|
|
93
|
+
/** Create an any schema (accepts any value) */
|
|
94
|
+
any: typeof any;
|
|
95
|
+
/** Create an object schema with typed properties */
|
|
96
|
+
object: typeof object;
|
|
97
|
+
/** Create an array schema with typed elements */
|
|
98
|
+
array: typeof array;
|
|
99
|
+
/** Create a record schema (object with string keys and typed values) */
|
|
100
|
+
record: typeof record;
|
|
101
|
+
/** Create a literal value schema */
|
|
102
|
+
literal: typeof literal;
|
|
103
|
+
/** Make a schema optional (T | undefined) */
|
|
104
|
+
optional: typeof optional;
|
|
105
|
+
/** Make a schema nullable (T | null) */
|
|
106
|
+
nullable: typeof nullable;
|
|
107
|
+
/** Create a union of schemas */
|
|
108
|
+
union: typeof union;
|
|
109
|
+
/** Create an enum schema (union of literals) */
|
|
110
|
+
enum: typeof enumSchema;
|
|
111
|
+
/** Convert schema to JSON Schema format */
|
|
112
|
+
toJSONSchema: typeof toJSONSchema;
|
|
113
|
+
/** Convert JSON Schema to schema */
|
|
114
|
+
fromJSONSchema: typeof fromJSONSchema;
|
|
115
|
+
/** Coercion schemas for type conversion */
|
|
116
|
+
coerce: {
|
|
117
|
+
/** Coerce to string using String(value) */
|
|
118
|
+
string: typeof coerceString;
|
|
119
|
+
/** Coerce to number using Number(value) */
|
|
120
|
+
number: typeof coerceNumber;
|
|
121
|
+
/** Coerce to boolean using Boolean(value) */
|
|
122
|
+
boolean: typeof coerceBoolean;
|
|
123
|
+
/** Coerce to Date using new Date(value) */
|
|
124
|
+
date: typeof coerceDate;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Namespace for s.infer type extraction (like zod's z.infer).
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const User = s.object({ name: s.string(), age: s.number() });
|
|
133
|
+
* type User = s.infer<typeof User>;
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export declare namespace s {
|
|
137
|
+
/**
|
|
138
|
+
* Extract the TypeScript type from a schema (like zod's z.infer).
|
|
139
|
+
*
|
|
140
|
+
* @template T - The schema type
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const Player = s.object({ username: s.string(), xp: s.number() });
|
|
145
|
+
* type Player = s.infer<typeof Player>;
|
|
146
|
+
* // { username: string; xp: number }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
type infer<T extends Schema<any, any>> = InferType<T>;
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACX,MAAM,EACN,KAAK,EACL,UAAU,EACV,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,cAAc,GACd,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAExE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,M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CAAoC;;IAEpC,2CAA2C;;QAE1C,2CAA2C;;QAE3C,2CAA2C;;QAE3C,6CAA6C;;QAE7C,2CAA2C;;;CAG5C,CAAC;AAEF;;;;;;;;GAQG;AAEH,yBAAiB,CAAC,CAAC;IAClB;;;;;;;;;;;OAWG;IAEH,KAAY,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC;CAC7D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
export { createIssue, success, failure, ValidationError } from './base';
|
|
2
|
+
export { StringSchema, string } from './primitives/string';
|
|
3
|
+
export { NumberSchema, number } from './primitives/number';
|
|
4
|
+
export { BooleanSchema, boolean } from './primitives/boolean';
|
|
5
|
+
export { NullSchema, null_ } from './primitives/null';
|
|
6
|
+
export { UndefinedSchema, undefined_ } from './primitives/undefined';
|
|
7
|
+
export { UnknownSchema, unknown } from './primitives/unknown';
|
|
8
|
+
export { AnySchema, any } from './primitives/any';
|
|
9
|
+
export { ObjectSchema, object } from './complex/object';
|
|
10
|
+
export { ArraySchema, array } from './complex/array';
|
|
11
|
+
export { RecordSchema, record } from './complex/record';
|
|
12
|
+
export { LiteralSchema, literal } from './utils/literal';
|
|
13
|
+
export { OptionalSchema, optional } from './utils/optional';
|
|
14
|
+
export { NullableSchema, nullable } from './utils/nullable';
|
|
15
|
+
export { UnionSchema, union } from './utils/union';
|
|
16
|
+
export { toJSONSchema, fromJSONSchema } from './json-schema';
|
|
17
|
+
export { CoerceStringSchema, coerceString } from './coerce/string';
|
|
18
|
+
export { CoerceNumberSchema, coerceNumber } from './coerce/number';
|
|
19
|
+
export { CoerceBooleanSchema, coerceBoolean } from './coerce/boolean';
|
|
20
|
+
export { CoerceDateSchema, coerceDate } from './coerce/date';
|
|
21
|
+
import { string } from './primitives/string';
|
|
22
|
+
import { number } from './primitives/number';
|
|
23
|
+
import { boolean } from './primitives/boolean';
|
|
24
|
+
import { null_ } from './primitives/null';
|
|
25
|
+
import { undefined_ } from './primitives/undefined';
|
|
26
|
+
import { unknown } from './primitives/unknown';
|
|
27
|
+
import { any } from './primitives/any';
|
|
28
|
+
import { object } from './complex/object';
|
|
29
|
+
import { array } from './complex/array';
|
|
30
|
+
import { record } from './complex/record';
|
|
31
|
+
import { literal } from './utils/literal';
|
|
32
|
+
import { optional } from './utils/optional';
|
|
33
|
+
import { nullable } from './utils/nullable';
|
|
34
|
+
import { union } from './utils/union';
|
|
35
|
+
import { toJSONSchema, fromJSONSchema } from './json-schema';
|
|
36
|
+
import { coerceString } from './coerce/string';
|
|
37
|
+
import { coerceNumber } from './coerce/number';
|
|
38
|
+
import { coerceBoolean } from './coerce/boolean';
|
|
39
|
+
import { coerceDate } from './coerce/date';
|
|
40
|
+
/**
|
|
41
|
+
* Create an enum schema (union of literal values).
|
|
42
|
+
* Shorthand for creating a union of literals.
|
|
43
|
+
*
|
|
44
|
+
* @param values - Array of literal values
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const roleSchema = s.enum(['admin', 'user', 'guest']);
|
|
49
|
+
* const role = roleSchema.parse('admin'); // 'admin'
|
|
50
|
+
*
|
|
51
|
+
* // Equivalent to:
|
|
52
|
+
* s.union(s.literal('admin'), s.literal('user'), s.literal('guest'))
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
function enumSchema(values) {
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
|
+
return union(...values.map((v) => literal(v)));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Main schema builder object.
|
|
61
|
+
* Provides access to all schema types and utilities.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { s } from '@agentuity/schema';
|
|
66
|
+
*
|
|
67
|
+
* // Define a schema
|
|
68
|
+
* const User = s.object({
|
|
69
|
+
* name: s.string(),
|
|
70
|
+
* age: s.number(),
|
|
71
|
+
* role: s.enum(['admin', 'user'])
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* // Extract type
|
|
75
|
+
* type User = s.infer<typeof User>;
|
|
76
|
+
*
|
|
77
|
+
* // Parse data
|
|
78
|
+
* const user = User.parse(data);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export const s = {
|
|
82
|
+
/** Create a string schema */
|
|
83
|
+
string,
|
|
84
|
+
/** Create a number schema */
|
|
85
|
+
number,
|
|
86
|
+
/** Create a boolean schema */
|
|
87
|
+
boolean,
|
|
88
|
+
/** Create a null schema */
|
|
89
|
+
null: null_,
|
|
90
|
+
/** Create an undefined schema */
|
|
91
|
+
undefined: undefined_,
|
|
92
|
+
/** Create an unknown schema (accepts any value) */
|
|
93
|
+
unknown,
|
|
94
|
+
/** Create an any schema (accepts any value) */
|
|
95
|
+
any,
|
|
96
|
+
/** Create an object schema with typed properties */
|
|
97
|
+
object,
|
|
98
|
+
/** Create an array schema with typed elements */
|
|
99
|
+
array,
|
|
100
|
+
/** Create a record schema (object with string keys and typed values) */
|
|
101
|
+
record,
|
|
102
|
+
/** Create a literal value schema */
|
|
103
|
+
literal,
|
|
104
|
+
/** Make a schema optional (T | undefined) */
|
|
105
|
+
optional,
|
|
106
|
+
/** Make a schema nullable (T | null) */
|
|
107
|
+
nullable,
|
|
108
|
+
/** Create a union of schemas */
|
|
109
|
+
union,
|
|
110
|
+
/** Create an enum schema (union of literals) */
|
|
111
|
+
enum: enumSchema,
|
|
112
|
+
/** Convert schema to JSON Schema format */
|
|
113
|
+
toJSONSchema,
|
|
114
|
+
/** Convert JSON Schema to schema */
|
|
115
|
+
fromJSONSchema,
|
|
116
|
+
/** Coercion schemas for type conversion */
|
|
117
|
+
coerce: {
|
|
118
|
+
/** Coerce to string using String(value) */
|
|
119
|
+
string: coerceString,
|
|
120
|
+
/** Coerce to number using Number(value) */
|
|
121
|
+
number: coerceNumber,
|
|
122
|
+
/** Coerce to boolean using Boolean(value) */
|
|
123
|
+
boolean: coerceBoolean,
|
|
124
|
+
/** Coerce to Date using new Date(value) */
|
|
125
|
+
date: coerceDate,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAExE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAExD,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,eAAe,CA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a,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAI3C;;;;;;;;;;;;;;GAcG;AACH,SAAS,UAAU,CAEjB,MAAS;IACV,8DAA8D;IAC9D,OAAO,KAAK,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAQ,CAAC,CAAC,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,MAAM,CAAC,GAAG;IAChB,6BAA6B;IAC7B,MAAM;IACN,6BAA6B;IAC7B,MAAM;IACN,8BAA8B;IAC9B,OAAO;IACP,2BAA2B;IAC3B,IAAI,EAAE,KAAK;IACX,iCAAiC;IACjC,SAAS,EAAE,UAAU;IACrB,mDAAmD;IACnD,OAAO;IACP,+CAA+C;IAC/C,GAAG;IACH,oDAAoD;IACpD,MAAM;IACN,iDAAiD;IACjD,KAAK;IACL,wEAAwE;IACxE,MAAM;IACN,oCAAoC;IACpC,OAAO;IACP,6CAA6C;IAC7C,QAAQ;IACR,wCAAwC;IACxC,QAAQ;IACR,gCAAgC;IAChC,KAAK;IACL,gDAAgD;IAChD,IAAI,EAAE,UAAU;IAChB,2CAA2C;IAC3C,YAAY;IACZ,oCAAoC;IACpC,cAAc;IACd,2CAA2C;IAC3C,MAAM,EAAE;QACP,2CAA2C;QAC3C,MAAM,EAAE,YAAY;QACpB,2CAA2C;QAC3C,MAAM,EAAE,YAAY;QACpB,6CAA6C;QAC7C,OAAO,EAAE,aAAa;QACtB,2CAA2C;QAC3C,IAAI,EAAE,UAAU;KAChB;CACD,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Schema } from './base';
|
|
2
|
+
/**
|
|
3
|
+
* JSON Schema object representation.
|
|
4
|
+
* Subset of JSON Schema Draft 7 specification.
|
|
5
|
+
*/
|
|
6
|
+
export interface JSONSchema {
|
|
7
|
+
type?: 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
8
|
+
description?: string;
|
|
9
|
+
const?: string | number | boolean;
|
|
10
|
+
enum?: Array<string | number | boolean>;
|
|
11
|
+
properties?: Record<string, JSONSchema>;
|
|
12
|
+
required?: string[];
|
|
13
|
+
items?: JSONSchema;
|
|
14
|
+
anyOf?: JSONSchema[];
|
|
15
|
+
oneOf?: JSONSchema[];
|
|
16
|
+
allOf?: JSONSchema[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert a schema to JSON Schema format.
|
|
20
|
+
* Supports primitives, objects, arrays, unions, literals, optional, and nullable types.
|
|
21
|
+
*
|
|
22
|
+
* @param schema - The schema to convert
|
|
23
|
+
* @returns JSON Schema object
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const userSchema = s.object({
|
|
28
|
+
* name: s.string().describe('User name'),
|
|
29
|
+
* age: s.number().describe('User age')
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const jsonSchema = s.toJSONSchema(userSchema);
|
|
33
|
+
* // { type: 'object', properties: {...}, required: [...] }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function toJSONSchema(schema: Schema<any, any>): JSONSchema;
|
|
37
|
+
/**
|
|
38
|
+
* Convert a JSON Schema object to a schema.
|
|
39
|
+
* Supports round-trip conversion with toJSONSchema.
|
|
40
|
+
*
|
|
41
|
+
* @param jsonSchema - The JSON Schema object to convert
|
|
42
|
+
* @returns Schema instance
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const jsonSchema = {
|
|
47
|
+
* type: 'object',
|
|
48
|
+
* properties: {
|
|
49
|
+
* name: { type: 'string' },
|
|
50
|
+
* age: { type: 'number' }
|
|
51
|
+
* },
|
|
52
|
+
* required: ['name', 'age']
|
|
53
|
+
* };
|
|
54
|
+
*
|
|
55
|
+
* const schema = s.fromJSONSchema(jsonSchema);
|
|
56
|
+
* const user = schema.parse({ name: 'John', age: 30 });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function fromJSONSchema(jsonSchema: JSONSchema): Schema<any, any>;
|
|
60
|
+
//# sourceMappingURL=json-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.d.ts","sourceRoot":"","sources":["../src/json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAarC;;;GAGG;AACH,MAAM,WAAW,UAAU;IAC1B,IAAI,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,CAAC;IACjF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;IAClC,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,UAAU,CA6GjE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAoJvE"}
|