Skip to content

Support yup enums with any naming conventions (EnumName.ENUM_VALUE) #560

@milan-wisdom

Description

@milan-wisdom

I modified the casing of the GraphQL-generated enums to follow EnumName.ENUM_VALUE. When I run typescript-validation-schema to generate the schemas, it always defaults to the original EnumName.EnumValue casing.

My codegen

generates:
  types.ts:
    plugins:
      - 'typescript'
    config:
      namingConvention:
        enumValues: 'change-case-all#upperCase'
  validation.ts:
    plugins:
      - 'typescript-validation-schema'
    config:
      importFrom: ./types
      schema: yup

The outcome of this is the following generated files:

// types.ts
export enum SortDirection {
  ASC = 'ASC',
  DESC = 'DESC'
}
// validation.ts
import * as yup from 'yup'
import { SortDirectionSchema } from './types'

export const SortDirectionSchema = yup.string<SortDirection>().oneOf([SortDirection.Asc, SortDirection.Desc]).defined();

However, if the generator would create the following valid yup schema, the casing of the enum values would be irrelevant.

// validation.ts
import * as yup from 'yup'
import { SortDirectionSchema } from './types'

export const SortDirectionSchema = yup.string<SortDirection>().oneOf(Object.values(SortDirection)).defined();

Reference discussion: jquense/yup#1013

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions