Skip to content
中文

fromJsonSchema ​

SUPPORT · function · Runtime value

Build an adapter from a caller-owned typed JSON Schema compiler.

The schema is cloned and frozen before it reaches the compiler, so later mutation of the input cannot change validation. Compilation and validation must both be synchronous.

Import ​

ts
import { fromJsonSchema } from '@jangts/agent-core/support';
js
import { fromJsonSchema } from '@jangts/agent-core/support';

Other public entry points:@jangts/agent-core/support.

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Value—unknown

Call ​

ts
fromJsonSchema<Value = unknown>(schema: JsonSchema, options: JsonSchemaOptions<Value>): SchemaAdapter<Value>

Build an adapter from a caller-owned typed JSON Schema compiler.

The schema is cloned and frozen before it reaches the compiler, so later mutation of the input cannot change validation. Compilation and validation must both be synchronous.

Type parameterConstraintDefault type
Value—unknown
ParameterTypePassingDescription
schemaReadonly<Record<string, unknown>>Required—
optionsJsonSchemaOptions<Value>Required—
Fields of options
FieldTypeRequiredDescription
compile(schema: Record<string, unknown>) => JsonSchemaValidator<Value>RequiredCompiles a frozen schema copy into a validator; must return synchronously.
error((value: unknown, errors: unknown) => Error) | undefinedOptionalBuilds the thrown error for an invalid value; defaults to a TypeError.

Returns: SchemaAdapter<Value>.

Return value structure
FieldTypeRequiredDescription
jsonSchemaReadonly<Record<string, unknown>>RequiredRead-only. Schema advertised to the model and used for documentation.
parse(input: unknown) => ValueRequiredValidates or coerces untrusted input into Value; throws when the input is invalid.

Examples ​

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 '));

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.

ts
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));
js
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));

API reference, usage examples and developer guides.