Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve @kbn/config-schema string type with generics #129184

Closed
dimaanj opened this issue Apr 1, 2022 · 3 comments
Closed

Improve @kbn/config-schema string type with generics #129184

dimaanj opened this issue Apr 1, 2022 · 3 comments
Labels
Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc

Comments

@dimaanj
Copy link
Contributor

dimaanj commented Apr 1, 2022

Working with @kbn/config-schema were found that it would be nice to have generic typing for string schema type. Since in some cases it might be enum like here is Comparator. In that case it will be possible to get rid of type casting (as). Suggestion:

function string<T extends string>(options?: StringOptions<T>): Type<T>;

type StringOptions<T extends string> = TypeOptions<T> & {
    minLength?: number;
    maxLength?: number;
    hostname?: boolean;
};
@dimaanj dimaanj added the Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc label Apr 1, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

@afharo
Copy link
Member

afharo commented Apr 1, 2022

@dmitriynj have you considered using schema.oneOf for this type of validation?

  thresholdComparator: schema.oneOf([
    schema.literal(Comparator.GT),
    schema.literal(Comparator.LT),
    schema.literal(Comparator.GT_OR_EQ),
    schema.literal(Comparator.LT_OR_EQ),
    schema.literal(Comparator.BETWEEN),
    schema.literal(Comparator.NOT_BETWEEN),
  ]),

@dimaanj
Copy link
Contributor Author

dimaanj commented Apr 1, 2022

@dmitriynj have you considered using schema.oneOf for this type of validation?

schema.oneOf solves the problem with type casting for enums, thank you. Therefore closing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc
Projects
None yet
Development

No branches or pull requests

3 participants