JsonSchemaOptions
SUPPORT · interface · Type contract
Options for building an adapter from a JSON Schema compiler.
Import
ts
import type { JsonSchemaOptions } from '@jangts/agent-core/support';1
js
/** @typedef {import('@jangts/agent-core/support').JsonSchemaOptions} JsonSchemaOptions */1
Other public entry points:@jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/support.
Definition
ts
interface JsonSchemaOptions<Output>1
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Output | — | — |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
compile | (schema: Record<string, unknown>) => JsonSchemaValidator<Output> | Required | Compiles a frozen schema copy into a validator; must return synchronously. |
error | ((value: unknown, errors: unknown) => Error) | undefined | Optional | Builds the thrown error for an invalid value; defaults to a TypeError. |
Member index
| Member | Purpose |
|---|---|
compile | Compiles a frozen schema copy into a validator; must return synchronously. |
Member details
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.
| Parameter | Type | Passing | Description |
|---|---|---|---|
schema | Record<string, unknown> | Required | — |
Returns: JsonSchemaValidator<Output>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
errors | unknown | Optional | Read-only. Compiler-reported details for the last failed validation, when the library exposes them. |
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' }));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
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 '));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