fromParserSchema
SUPPORT · function · 运行时值
Wrap a library schema that already parses values (for example Zod or Valibot style).
parse is delegated unchanged, so the library keeps ownership of coercion and error reporting; only the JSON Schema projection is produced here.
导入
ts
import { fromParserSchema } from '@jangts/agent-core/support';js
import { fromParserSchema } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Schema | { parse(value: unknown): unknown } | — |
调用
ts
fromParserSchema<Schema extends { parse(value: unknown): unknown; }>(schema: Schema, options: { toJsonSchema(schema: Schema): JsonSchema; }): SchemaAdapter<ReturnType<Schema["parse"]>>Wrap a library schema that already parses values (for example Zod or Valibot style).
parse is delegated unchanged, so the library keeps ownership of coercion and error reporting; only the JSON Schema projection is produced here.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Schema | { parse(value: unknown): unknown; } | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
schema | Schema | 必需 | — |
options | { toJsonSchema(schema: Schema): JsonSchema; } | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
toJsonSchema | (schema: Schema) => JsonSchema | 必需 | 见类型定义及调用场景。 |
返回: SchemaAdapter<ReturnType<Schema["parse"]>>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
jsonSchema | Readonly<Record<string, unknown>> | 必需 | 只读。Schema advertised to the model and used for documentation. |
parse | (input: unknown) => ReturnType<Schema["parse"]> | 必需 | Validates or coerces untrusted input into Value; throws when the input is invalid. |
使用示例
- Schema:保留 JSON 声明与实际解析的同一合同:Support 生成规范的 SchemaAdapter,Core 只要求 jsonSchema 与同步 parse 合同。
一个有实际校验的字符串对象
入门 · Node.js
ts
import { Support } from '@jangts/agent-core';
const nameSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length !== 1 || !('name' in value) || typeof value.name !== 'string') throw new TypeError('需要 name 字符串');
return { name: value.name };
},
});
console.log(nameSchema.parse({ name: 'Ivan' }));js
import { Support } from '@jangts/agent-core';
const nameSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length !== 1 || !('name' in value) || typeof value.name !== 'string')
throw new TypeError('需要 name 字符串');
return { name: value.name };
},
});
console.log(nameSchema.parse({ name: 'Ivan' }));复用已经拥有的解析对象
配置 · Node.js
实际接解析库时,toJsonSchema 必须准确表达该 parser;不是库名称决定可信度。
ts
import { Support } from '@jangts/agent-core';
class NameParser {
parse(value: unknown) { if (typeof value !== 'string' || !value.trim()) throw new TypeError('需要非空姓名'); return value.trim(); }
}
const schema = Support.fromParserSchema(new NameParser(), {
toJsonSchema: () => ({ type: 'string', minLength: 1 }),
});
console.log(schema.parse(' Ivan '));js
import { Support } from '@jangts/agent-core';
class NameParser {
parse(value) { if (typeof value !== 'string' || !value.trim())
throw new TypeError('需要非空姓名'); return value.trim(); }
}
const schema = Support.fromParserSchema(new NameParser(), {
toJsonSchema: () => ({ type: 'string', minLength: 1 }),
});
console.log(schema.parse(' Ivan '));使用已有 JSON Schema 编译端口
扩展 · Node.js
演示显式编译端口,不是一个通用 JSON Schema 编译器;生产可接入 App 已有的成熟编译器。
ts
import { Support } from '@jangts/agent-core';
const schema = Support.fromJsonSchema<number>({ type: 'integer', minimum: 0 }, {
compile(jsonSchema) {
if (jsonSchema.type !== 'integer' || jsonSchema.minimum !== 0) throw new Error('本例编译器只处理该规则');
return (value: unknown): value is number => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
},
});
console.log(schema.parse(3));js
import { Support } from '@jangts/agent-core';
const schema = Support.fromJsonSchema({ type: 'integer', minimum: 0 }, {
compile(jsonSchema) {
if (jsonSchema.type !== 'integer' || jsonSchema.minimum !== 0)
throw new Error('本例编译器只处理该规则');
return (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
},
});
console.log(schema.parse(3));