@dexwox-labs/a2a-core
Version:
Core types, validation and telemetry for Google's Agent-to-Agent (A2A) protocol - shared foundation for client and server implementations
103 lines • 3.19 kB
JavaScript
;
/**
* @module SchemaUtils
* @description Utilities for working with JSON Schema and Zod validation
*
* This module provides functions for converting JSON Schema to Zod validators
* and validating data against JSON Schema definitions.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateWithSchema = validateWithSchema;
exports.createValidator = createValidator;
const json_schema_to_zod_1 = require("json-schema-to-zod");
const zod_1 = require("zod");
/**
* Validates data against a JSON Schema definition
*
* This function converts a JSON Schema to a Zod validator and uses it to
* validate the provided data. It returns a SafeParseReturnType that contains
* either the validated data or validation errors.
*
* @param schema - JSON Schema definition
* @param data - Data to validate against the schema
* @returns A SafeParseReturnType containing either the validated data or validation errors
*
* @example
* ```typescript
* const userSchema = {
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number', minimum: 0 }
* },
* required: ['name']
* };
*
* const result = validateWithSchema<User>(userSchema, {
* name: 'John Doe',
* age: 30
* });
*
* if (result.success) {
* // Use the validated data
* console.log('Valid user:', result.data);
* } else {
* // Handle validation errors
* console.error('Invalid user:', result.error);
* }
* ```
*/
function validateWithSchema(schema, data) {
const zodSchema = (0, json_schema_to_zod_1.jsonSchemaToZod)(schema, { module: 'cjs' });
const validator = zod_1.z.object(zodSchema);
return validator.safeParse(data);
}
/**
* Creates a reusable validator from a JSON Schema definition
*
* This function converts a JSON Schema to a Zod validator and returns an object
* with a validate function and the schema. The validate function can be used to
* validate data against the schema multiple times without recreating the validator.
*
* @param schema - JSON Schema definition
* @returns An object with a validate function and the Zod schema
*
* @example
* ```typescript
* // Create a reusable validator
* const userValidator = createValidator<User>({
* type: 'object',
* properties: {
* name: { type: 'string' },
* email: { type: 'string', format: 'email' },
* age: { type: 'number', minimum: 18 }
* },
* required: ['name', 'email']
* });
*
* // Validate multiple users
* const user1Result = userValidator.validate({
* name: 'John Doe',
* email: 'john@example.com',
* age: 25
* });
*
* const user2Result = userValidator.validate({
* name: 'Jane Smith',
* email: 'jane@example.com',
* age: 30
* });
*
* // You can also access the Zod schema directly
* type UserType = z.infer<typeof userValidator.schema>;
* ```
*/
function createValidator(schema) {
const zodSchema = (0, json_schema_to_zod_1.jsonSchemaToZod)(schema, { module: 'cjs' });
const validator = zod_1.z.object(zodSchema);
return {
validate: (data) => validator.safeParse(data),
schema: validator
};
}
//# sourceMappingURL=schema-utils.js.map