David AlonsoD
Convex Community2y ago
6 replies
David Alonso

Validator operation not behaving as expected

I have this table:
const vTableViewResourceAccess = v.object({
  ...vCommonAccessProps.fields,
  resourceType: v.literal("tableView"),
  resourceId: v.id("tableViews"),
  permissions: v.array(vTableViewPermissions),
});

export const accessTable = defineTable(
  v.union(
    vWorkspaceAccess,
    vFirestoreProjectResourceAccess,
    vDataSourceResourceAccess,
...
}


And I'm trying to create a custom type like so:
export const vResourcePermission = v.union(
  ...accessTable.validator.members.map((member) => {
    return v.object({
      resourceType: member.fields.resourceType,
      resourceId: v.optional(member.fields.resourceId),
      requiredPermissions: member.fields.permissions,
    });
  })
);


However when I try to use this validator as an argument of a function, requiredPermissions lets me choose from all permissions and not the scoped ones by the resourceType, and I don't understand why.

In case you want to repro: vTableViewPermissions is simply a union of string literals
Was this page helpful?