@tpmjs/tools-json-schema-validate
Validates JSON data against a JSON Schema specification using the ajv validator. Returns validation status, detailed errors, and error count. Supports JSON Schema Draft 7 and common formats (email, uri, date-time, etc.).
Test @tpmjs/tools-json-schema-validate (jsonSchemaValidateTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-json-schema-validatepnpm add @tpmjs/tools-json-schema-validateyarn add @tpmjs/tools-json-schema-validatebun add @tpmjs/tools-json-schema-validatedeno add npm:@tpmjs/tools-json-schema-validateimport { jsonSchemaValidateTool } from '@tpmjs/tools-json-schema-validate';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { jsonSchemaValidateTool } from '@tpmjs/tools-json-schema-validate';
const result = await generateText({
model: openai('gpt-4o'),
tools: { jsonSchemaValidateTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
dataobjectThe data to validate (can be any JSON value: object, array, string, etc.)
schemaobjectJSON Schema object defining the expected structure and validation rules (Draft 7 compatible)
Schema extracted: 1/1/2026, 8:18:32 AM
Validates JSON data against JSON Schema using the ajv validator.
npm install @tpmjs/tools-json-schema-validate
import { jsonSchemaValidateTool } from '@tpmjs/tools-json-schema-validate'; const result = await jsonSchemaValidateTool.execute({ data: { name: 'John Doe', email: 'john@example.com', age: 30, }, schema: { type: 'object', properties: { name: { type: 'string', minLength: 1 }, email: { type: 'string', format: 'email' }, age: { type: 'number', minimum: 0, maximum: 120 }, }, required: ['name', 'email'], additionalProperties: false, }, }); console.log(result); // { // valid: true, // errors: [], // errorCount: 0 // }
const result = await jsonSchemaValidateTool.execute({ data: { name: '', email: 'invalid-email', age: 150, }, schema: { type: 'object', properties: { name: { type: 'string', minLength: 1 }, email: { type: 'string', format: 'email' }, age: { type: 'number', minimum: 0, maximum: 120 }, }, required: ['name', 'email'], }, }); console.log(result); // { // valid: false, // errors: [ // { // path: '/name', // message: 'String length must be >= 1', // keyword: 'minLength', // params: { limit: 1 } // }, // { // path: '/email', // message: 'Invalid email format', // keyword: 'format', // params: { format: 'email' } // }, // { // path: '/age', // message: 'Value must be <= 120', // keyword: 'maximum', // params: { limit: 120 } // } // ], // errorCount: 3 // }
Returns a ValidationResult object with:
true if data passes validation, false otherwiseEach error includes:
/user/email)required, type)const result = await jsonSchemaValidateTool.execute({ data: [1, 2, 3, 4, 5], schema: { type: 'array', items: { type: 'number' }, minItems: 1, maxItems: 10, }, });
const result = await jsonSchemaValidateTool.execute({ data: { user: { name: 'Alice', address: { city: 'New York', zip: '10001', }, }, }, schema: { type: 'object', properties: { user: { type: 'object', properties: { name: { type: 'string' }, address: { type: 'object', properties: { city: { type: 'string' }, zip: { type: 'string', pattern: '^[0-9]{5}$' }, }, required: ['city', 'zip'], }, }, required: ['name', 'address'], }, }, required: ['user'], }, });
const result = await jsonSchemaValidateTool.execute({ data: { status: 'active' }, schema: { type: 'object', properties: { status: { type: 'string', enum: ['active', 'inactive', 'pending'], }, }, }, });
If the schema itself is invalid, the tool throws an error:
try { const result = await jsonSchemaValidateTool.execute({ data: {}, schema: { type: 'invalid-type' }, // Invalid schema }); } catch (error) { console.error(error.message); // "Invalid JSON Schema: ..." }
MIT
Downloads/month
0
Quality Score