Key Validation Proposal for Effect.Service in Next Major Release

Is there a chance, we can do key validation in the next major with Effect.Service? This would help a lot in terms of standardising context keys. I do acknowledge, it smells overly paternalistic to enforce this though. Example:

export const createKey = <T extends string>(
  key: IsValid<T> extends true ? T : 'Error: use @Namespace/Name format'
) => key;

export type IsValid<S extends string> =
  Split<S, '/'> extends [
    infer First extends string,
    ...infer Rest extends string[],
  ]
    ? FirstIsValid<First> extends true
      ? Rest extends []
        ? false
        : ValidateRest<Rest>
      : false
    : false;

type FirstIsValid<S extends string> = S extends `@${infer Rest}`
  ? Rest extends ''
    ? false
    : ValidSegment<Rest>
  : false;

type ValidateRest<T extends string[]> = T extends [
  infer Segment extends string,
  ...infer Rest extends string[],
]
  ? Segment extends ''
    ? false
    : ValidSegment<Segment> extends true
      ? ValidateRest<Rest>
      : false
  : true;

type ValidSegment<
  S extends string,
  FirstChar extends boolean = true,
> = S extends `${infer First}${infer Rest}`
  ? FirstChar extends true
    ? First extends Digit
      ? false // first char cannot be digit
      : First extends Alpha
        ? ValidSegment<Rest, false>
        : false
    : First extends AllowedChar
      ? ValidSegment<Rest, false>
      : false
  : true;
Was this page helpful?