fromParserSchema
SUPPORT · function · Runtime value
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.
Import
import { fromParserSchema } from '@jangts/agent-core/support';import { fromParserSchema } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Schema | { parse(value: unknown): unknown } | — |
Call
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.
| Type parameter | Constraint | Default type |
|---|---|---|
Schema | { parse(value: unknown): unknown; } | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
schema | Schema | Required | — |
options | { toJsonSchema(schema: Schema): JsonSchema; } | Required | — |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
toJsonSchema | (schema: Schema) => JsonSchema | Required | See the type definition and usage context. |
Returns: SchemaAdapter<ReturnType<Schema["parse"]>>.
Return value 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) => ReturnType<Schema["parse"]> | Required | Validates or coerces untrusted input into Value; throws when the input is invalid. |
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
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' }));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.
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 '));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 '));Use an existing JSON Schema compile port
Extension · Node.js
This demonstrates an explicit compile port, not a general JSON Schema compiler; production can plug in a mature compiler the App already has.
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));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));