#custom validator

3 messages · Page 1 of 1 (latest)

coral thistle
#

is this optimal?

import { mutation } from './_generated/server';
import schema from './schema';
import { v } from 'convex/values';

const propertyAssetsValidator = v.array(v.object({ 
        property_id: schema.tables.property_assets.validator.fields.property_id,
        asset_url: schema.tables.property_assets.validator.fields.asset_url,
        asset_type: v.union(v.literal('document'), v.literal('image'), v.literal('video')),
        is_primary: schema.tables.property_assets.validator.fields.is_primary,
        display_order: schema.tables.property_assets.validator.fields.display_order,
        image_category: v.optional(v.union(v.literal('property_image'), v.literal('floor_plan'))),
        description: schema.tables.property_assets.validator.fields.description
 }));
unborn cobaltBOT
#

Hi @coral thistle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, your custom validator seems to be correctly defined and is optimal for your use case. You are correctly using the v.array and v.object methods to define the structure of your data, and you are correctly using the v.union and v.literal methods to define the possible values for asset_type and image_category. You are also correctly referencing the validators for the fields of the property_assets table from your schema.

However, please note that the v.optional method is used to mark a field as optional. If image_category is not always present in your data, using v.optional is the correct approach.

Here are some references from the knowledge sources that might be helpful:

Remember to always keep your schema and validators in sync to avoid any inconsistencies in your data.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: