JsonSchemaOptions
SUPPORT · interface · 类型合同
Options for building an adapter from a JSON Schema compiler.
导入
ts
import type { JsonSchemaOptions } from '@jangts/agent-core/support';1
js
/** @typedef {import('@jangts/agent-core/support').JsonSchemaOptions} JsonSchemaOptions */1
其他公开入口:@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support。
定义
ts
interface JsonSchemaOptions<Output>1
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Output | — | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
compile | (schema: Record<string, unknown>) => JsonSchemaValidator<Output> | 必需 | Compiles a frozen schema copy into a validator; must return synchronously. |
error | ((value: unknown, errors: unknown) => Error) | undefined | 可省略 | Builds the thrown error for an invalid value; defaults to a TypeError. |
成员目录
| 成员 | 用途 |
|---|---|
compile | Compiles a frozen schema copy into a validator; must return synchronously. |
成员详情
compile
Compiles a frozen schema copy into a validator; must return synchronously.
ts
JsonSchemaOptions.compile(schema: Record<string, unknown>): JsonSchemaValidator<Output>1
Compiles a frozen schema copy into a validator; must return synchronously.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
schema | Record<string, unknown> | 必需 | — |
返回: JsonSchemaValidator<Output>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
errors | unknown | 可省略 | 只读。Compiler-reported details for the last failed validation, when the library exposes them. |
使用示例
- 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' }));1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
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' }));1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
复用已经拥有的解析对象
配置 · 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 '));1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
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 '));1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9