Skip to content
English

SchemaAdapter ​

CORE · interface · 类型合同

Compiled schema pair: the JSON form shown to the model and the runtime parser.

导入 ​

ts
import type { SchemaAdapter } from '@jangts/agent-core';
js
/** @typedef {import('@jangts/agent-core').SchemaAdapter} SchemaAdapter */

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs。

定义 ​

ts
interface SchemaAdapter<Value>

泛型参数 ​

类型参数约束默认类型
Value—unknown

字段结构 ​

字段类型必需说明
jsonSchemaReadonly<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.

成员目录 ​

成员用途
parseValidates or coerces untrusted input into Value; throws when the input is invalid.

成员详情 ​

parse ​

Validates or coerces untrusted input into Value; throws when the input is invalid.

ts
SchemaAdapter.parse(input: unknown): Value

Validates or coerces untrusted input into Value; throws when the input is invalid.

参数类型传递说明
inputunknown必需—

返回: Value。

等待后的值: Awaited<Value>。

使用示例 ​

一个有实际校验的字符串对象 ​

入门 · 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 '));

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。