Skip to content

Feature request: enum type #2560

@zd4y

Description

@zd4y

Hello, it would be really nice to have an enum type. Currently this can be done with i.string() but it doesn't provide type safety. For getting the type safety I have to override AppSchema and also use as MyEnum in the data returned by useQuery. I also need to add perms to make sure invalid data never gets inserted.

For example let's say I have a status: i.string().indexed() field in a groupInvitations entity that only accepts 'pending' | 'accepted' | 'declined'

I need to override the AppSchema like this:

export type InvitationStatus = 'pending' | 'accepted' | 'declined'

export type AppSchema = Omit<_AppSchema, 'entities'> & {
  entities: Omit<_AppSchema['entities'], 'groupInvitations'> & {
    groupInvitations: Omit<_AppSchema['entities']['groupInvitations'], 'status'> & {
      status: InvitationStatus
    }
  }
}

And when I use the data:

const { data } = db.useQuery(...)
const status = data.groupInvitations[0].status as InvitationStatus

And in the instant.perms.ts file:

  groupInvitations: {
    allow: {
      create: 'validStatus',
      update: 'validStatus',
    },
    bind: {
      validStatus: "data.status in ['pending', 'accepted', 'declined']",
    },
  },

With support for an enum type I would hope to be able to get all of this automatically just by specifying the valid enum values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions