benbenbenB
Convex Community11mo ago
2 replies
benbenben

Query Pre-processing for JSON Schema Storage

I'm working on an app in which I need to store JSON Schemas:

interface JsonSchema {
  title?: string;
  type?: string;
  description?: string;
  properties?: Record<string, any>;
  required?: string[];
  additionalProperties?: boolean;
  definitions?: Record<string, any>;
  $defs?: Record<string, any>;
  $schema?: string;
  $id?: string;
  comment?: string;
  $ref?: string;
  allOf?: JsonSchema[];
  anyOf?: JsonSchema[];
  oneOf?: JsonSchema[];
  not?: JsonSchema;
  items?: JsonSchema | JsonSchema[];
}


My first instinct was to use an Object data-type, but because Convex disallows keys starting with $ that idea was out.

So I figured I could just use some serialization in the handler to store the JSON Schemas as plain text:

handler: async (ctx, args) => {
    const now = Date.now();

    // Serialize the schemas
    const inputSchemaJson = JSON.stringify(args.inputSchema || {});
    const outputSchemaJson = JSON.stringify(args.outputSchema || {});

    // Create the model document with required fields
    const modelData: ModelDocument = {
      modelId: args.modelId,
      name: args.name,
      isPlaceholder: args.isPlaceholder,
      isSchemaPlaceholder: args.isSchemaPlaceholder,
      task: args.task,
      inputSchemaJson,
      outputSchemaJson,
      createdAt: now,
      updatedAt: now,
    };


But it seems like the handler is pre-validating the args and rejecting the JSON Schemas because of the keys that start with $.

I'd love for this serialization to happen auto-magically when creating / retrieving records—is there a preprocessing hook or something I can tap into to coerce the args into an acceptable format?
Was this page helpful?