Replies: 1 comment
-
|
You could create a Zod schema that validates JSON Schema structure :) import { z } from 'zod';
// Simplified JSON Schema validator (not complete)
const jsonSchemaValidator: z.ZodType<any> = z.lazy(() =>
z.union([
z.boolean(),
z.object({
type: z.enum(['null', 'boolean', 'object', 'array', 'number', 'string', 'integer']).optional(),
properties: z.record(jsonSchemaValidator).optional(),
items: jsonSchemaValidator.optional(),
required: z.array(z.string()).optional(),
additionalProperties: z.union([z.boolean(), jsonSchemaValidator]).optional(),
minimum: z.number().optional(),
maximum: z.number().optional(),
// ... many more properties
}).passthrough()
])
);
// Usage
try {
const result = jsonSchemaValidator.parse(mySchema);
console.log("Valid JSON Schema!");
} catch (e) {
console.log("Invalid:", e);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Zod already has support for converting a Zod Schema to JSON Schema, but what if I want to validate whether an object is a valid JSON Schema?
Beta Was this translation helpful? Give feedback.
All reactions