Skip to content
中文

JsonSchemaValidator ​

SUPPORT · interface · Type contract

App-owned JSON Schema compilers provide synchronous validation.

Import ​

ts
import type { JsonSchemaValidator } from '@jangts/agent-core/support';
js
/** @typedef {import('@jangts/agent-core/support').JsonSchemaValidator} JsonSchemaValidator */

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

Definition ​

ts
interface JsonSchemaValidator<Output>

Type parameters ​

Type parameterConstraintDefault type
Output—unknown

Call ​

ts
JsonSchemaValidator(value: unknown): value is Output

Returns whether the value is valid; must be synchronous.

ParameterTypePassingDescription
valueunknownRequired—

Returns: boolean.

Field structure ​

FieldTypeRequiredDescription
errorsunknownOptionalRead-only. Compiler-reported details for the last failed validation, when the library exposes them.

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

API reference, usage examples and developer guides.