SchemaAdapter
CORE · interface · Type contract
Compiled schema pair: the JSON form shown to the model and the runtime parser.
Import
ts
import type { SchemaAdapter } from '@jangts/agent-core';js
/** @typedef {import('@jangts/agent-core').SchemaAdapter} SchemaAdapter */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs.
Definition
ts
interface SchemaAdapter<Value>Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | unknown |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
jsonSchema | Readonly<Record<string, unknown>> | Required | Read-only. Schema advertised to the model and used for documentation. |
parse | (input: unknown) => Value | Required | Validates or coerces untrusted input into Value; throws when the input is invalid. |
Member index
| Member | Purpose |
|---|---|
parse | Validates or coerces untrusted input into Value; throws when the input is invalid. |
Member details
parse
Validates or coerces untrusted input into Value; throws when the input is invalid.
ts
SchemaAdapter.parse(input: unknown): ValueValidates or coerces untrusted input into Value; throws when the input is invalid.
| Parameter | Type | Passing | Description |
|---|---|---|---|
input | unknown | Required | — |
Returns: Value.
Awaited value: Awaited<Value>.
Examples
- Schema: keep the JSON declaration and actual parsing on the same contract: Support generates a canonical SchemaAdapter; Core only requires the jsonSchema and synchronous parse contract.
A string object with real validation
Beginner · 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' }));Reuse a parsing object you already own
Configuration · Node.js
When integrating a real parsing library, toJsonSchema must accurately express that parser; the library name does not determine trustworthiness.
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 '));