defineSchema
SUPPORT · function · 运行时值
Re-snapshot an existing adapter without changing its behavior.
Used to detach a caller-owned adapter from later mutation before it is exposed or reused.
导入
ts
import { defineSchema } from '@jangts/agent-core/support';js
import { defineSchema } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
调用
ts
defineSchema<Value>(definition: SchemaAdapter<Value>): SchemaAdapter<Value>Re-snapshot an existing adapter without changing its behavior.
Used to detach a caller-owned adapter from later mutation before it is exposed or reused.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definition | SchemaAdapter<Value> | 必需 | — |
definition 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
jsonSchema | Readonly<Record<string, unknown>> | 必需 | 只读。Schema advertised to the model and used for documentation. |
parse | (input: unknown) => Value | 必需 | Validates or coerces untrusted input into Value; throws when the input is invalid. |
返回: SchemaAdapter<Value>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
jsonSchema | Readonly<Record<string, unknown>> | 必需 | 只读。Schema advertised to the model and used for documentation. |
parse | (input: unknown) => Value | 必需 | 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));