mastra-ai/mastra · error · SchemaValidationError
SchemaValidationError(field, this.formatErrors(result.error)
Error message
SchemaValidationError(field, this.formatErrors(result.error))
What it means
The dataset validator converts a JSON Schema (JSONSchema7) to a Zod schema and safeParses the data. When parsing fails, it throws SchemaValidationError carrying the field name ('input' or 'groundTruth') and the formatted Zod error list. This means a dataset item does not conform to the declared schema for the given field.
Source
Thrown at packages/core/src/datasets/validation/validator.ts:41
if (!zodSchema) {
const zodString = jsonSchemaToZod(schema);
zodSchema = resolveZodSchema(zodString);
this.cache.set(cacheKey, zodSchema);
}
return zodSchema;
}
/** Clear cached validator (call when schema changes) */
clearCache(cacheKey: string): void {
this.cache.delete(cacheKey);
}
/** Validate data against schema */
validate(data: unknown, schema: JSONSchema7, field: 'input' | 'groundTruth', cacheKey: string): void {
const zodSchema = this.getValidator(schema, cacheKey);
const result = zodSchema.safeParse(data);
if (!result.success) {
throw new SchemaValidationError(field, this.formatErrors(result.error));
}
}
/** Validate multiple items, returning valid/invalid split */
validateBatch(
items: Array<{ input: unknown; groundTruth?: unknown }>,
inputSchema: JSONSchema7 | null | undefined,
outputSchema: JSONSchema7 | null | undefined,
cacheKeyPrefix: string,
maxErrors = 10,
): BatchValidationResult {
const result: BatchValidationResult = { valid: [], invalid: [] };
// Pre-compile schemas for performance
const inputValidator = inputSchema ? this.getValidator(inputSchema, `${cacheKeyPrefix}:input`) : null;
const outputValidator = outputSchema ? this.getValidator(outputSchema, `${cacheKeyPrefix}:output`) : null;
for (const [i, item] of items.entries()) {View on GitHub (pinned to 75dd419e61)
Solutions
- Read the formatted error list in the SchemaValidationError and correct the offending item(s) to match the schema.
- Run the validation locally first (e.g. with ajv) before submitting records so failures are caught client-side.
- If the schema changed intentionally, migrate existing records to the new shape instead of editing the schema to fit old data.
- Confirm the correct JSONSchema7 draft is used; unsupported or malformed schemas can compile to an unexpectedly strict Zod schema.
Example fix
// before
await dataset.addRecord({ input: { q: 'hello' } }); // schema requires 'question'
// after
await dataset.addRecord({ input: { question: 'hello' } }); Defensive patterns
Strategy: validation
Validate before calling
import Ajv from 'ajv';
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(item.input)) throw new Error(`Invalid input: ${ajv.errorsText(validate.errors)}`); Type guard
function conformsToSchema(data: unknown, schema: object): boolean {
try {
const ajv = new Ajv();
return Boolean(ajv.compile(schema as object)(data));
} catch { return false; }
} Try / catch
try {
dataset.validate(data, schema, 'input', cacheKey);
} catch (e) {
if (e.name === 'SchemaValidationError') {
console.error(`Field '${e.field}' failed:`, e.message);
return; // or fix/reject the record
}
throw e;
} Prevention
- Validate records with ajv (or a Zod equivalent) before submitting to the library.
- Keep schemas in one place and version them; migrate old records when schemas change.
- Log the full SchemaValidationError message — it lists every offending path and reason.
When it happens
Trigger: Calling validator.validate(data, schema, field, cacheKey) directly, or any library path that validates dataset items (saving/adding records, creating experiments, running evaluation) with data that violates the JSON schema for input or groundTruth.
Common situations: Missing required properties, wrong types (e.g. string instead of number), extra strictness from the schema, or a dataset row pasted in from another source with a different shape; also happens when the schema was changed after records were created with an older shape.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- We could not convert the schema to a JSONSchema
- SchemaUpdateValidationError
- Schema validation failed due to an invalid schema definition
- WORKFLOW_SCHEMA_VALIDATION_FAILED
- ${label} contains an unsupported field.
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/b31853c540eec874.
Report an issue: GitHub.