[EDR Workflows] Explicitly export type in open api autogenerated ZOD types (#191830)

This commit is contained in:
Tomasz Ciecierski 2024-09-03 09:53:57 +02:00 committed by GitHub
parent 3a352bcf7f
commit bac95ead5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 64 additions and 22 deletions

View file

@ -87,4 +87,12 @@ export function registerHelpers(handlebarsInstance: typeof Handlebars) {
return circularRefs.has(`#/components/schemas/${schemaName}`);
}
);
/**
* Large Zod schemas might lead to TypeScript type serialization limit. The hotfix applies type hinting for all schemas using `anyOf` or `oneOf`.
*/
handlebarsInstance.registerHelper(
'shouldCastExplicitly',
({ anyOf, oneOf }) => anyOf?.length > 2 || oneOf?.length > 2
);
}

View file

@ -31,8 +31,17 @@ export type {{@key}} = {{> ts_type}};
export type {{@key}}Input = {{> ts_input_type }};
export const {{@key}}: z.ZodType<{{@key}}, ZodTypeDef, {{@key}}Input> = {{> zod_schema_item }};
{{else}}
{{#if (shouldCastExplicitly this)}}
{{!-- We need this temporary type to infer from it below, but in the end we want to export as a casted {{@key}} type --}}
{{!-- error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. --}}
const {{@key}}Internal = {{> zod_schema_item}};
export type {{@key}} = z.infer<typeof {{@key}}Internal>;
export const {{@key}} = {{@key}}Internal as z.ZodType<{{@key}}>;
{{else}}
export type {{@key}} = z.infer<typeof {{@key}}>;
export const {{@key}} = {{> zod_schema_item}};
{{/if }}
{{/if}}
{{#if enum}}
{{#unless (isSingle enum)}}

View file

@ -59,15 +59,18 @@ export const ExceptionListItemEntryExists = z.object({
operator: ExceptionListItemEntryOperator,
});
export type ExceptionListItemEntryNestedEntryItem = z.infer<
typeof ExceptionListItemEntryNestedEntryItem
>;
export const ExceptionListItemEntryNestedEntryItem = z.union([
const ExceptionListItemEntryNestedEntryItemInternal = z.union([
ExceptionListItemEntryMatch,
ExceptionListItemEntryMatchAny,
ExceptionListItemEntryExists,
]);
export type ExceptionListItemEntryNestedEntryItem = z.infer<
typeof ExceptionListItemEntryNestedEntryItemInternal
>;
export const ExceptionListItemEntryNestedEntryItem =
ExceptionListItemEntryNestedEntryItemInternal as z.ZodType<ExceptionListItemEntryNestedEntryItem>;
export type ExceptionListItemEntryNested = z.infer<typeof ExceptionListItemEntryNested>;
export const ExceptionListItemEntryNested = z.object({
type: z.literal('nested'),
@ -85,8 +88,7 @@ export const ExceptionListItemEntryMatchWildcard = z.object({
operator: ExceptionListItemEntryOperator,
});
export type ExceptionListItemEntry = z.infer<typeof ExceptionListItemEntry>;
export const ExceptionListItemEntry = z.discriminatedUnion('type', [
const ExceptionListItemEntryInternal = z.discriminatedUnion('type', [
ExceptionListItemEntryMatch,
ExceptionListItemEntryMatchAny,
ExceptionListItemEntryList,
@ -95,5 +97,9 @@ export const ExceptionListItemEntry = z.discriminatedUnion('type', [
ExceptionListItemEntryMatchWildcard,
]);
export type ExceptionListItemEntry = z.infer<typeof ExceptionListItemEntryInternal>;
export const ExceptionListItemEntry =
ExceptionListItemEntryInternal as z.ZodType<ExceptionListItemEntry>;
export type ExceptionListItemEntryArray = z.infer<typeof ExceptionListItemEntryArray>;
export const ExceptionListItemEntryArray = z.array(ExceptionListItemEntry);

View file

@ -597,8 +597,7 @@ export const EsqlRuleUpdateProps = SharedUpdateProps.merge(EsqlRuleCreateFields)
export type EsqlRulePatchProps = z.infer<typeof EsqlRulePatchProps>;
export const EsqlRulePatchProps = SharedPatchProps.merge(EsqlRulePatchFields.partial());
export type TypeSpecificCreateProps = z.infer<typeof TypeSpecificCreateProps>;
export const TypeSpecificCreateProps = z.discriminatedUnion('type', [
const TypeSpecificCreatePropsInternal = z.discriminatedUnion('type', [
EqlRuleCreateFields,
QueryRuleCreateFields,
SavedQueryRuleCreateFields,
@ -609,8 +608,11 @@ export const TypeSpecificCreateProps = z.discriminatedUnion('type', [
EsqlRuleCreateFields,
]);
export type TypeSpecificPatchProps = z.infer<typeof TypeSpecificPatchProps>;
export const TypeSpecificPatchProps = z.union([
export type TypeSpecificCreateProps = z.infer<typeof TypeSpecificCreatePropsInternal>;
export const TypeSpecificCreateProps =
TypeSpecificCreatePropsInternal as z.ZodType<TypeSpecificCreateProps>;
const TypeSpecificPatchPropsInternal = z.union([
EqlRulePatchFields,
QueryRulePatchFields,
SavedQueryRulePatchFields,
@ -621,8 +623,11 @@ export const TypeSpecificPatchProps = z.union([
EsqlRulePatchFields,
]);
export type TypeSpecificResponse = z.infer<typeof TypeSpecificResponse>;
export const TypeSpecificResponse = z.discriminatedUnion('type', [
export type TypeSpecificPatchProps = z.infer<typeof TypeSpecificPatchPropsInternal>;
export const TypeSpecificPatchProps =
TypeSpecificPatchPropsInternal as z.ZodType<TypeSpecificPatchProps>;
const TypeSpecificResponseInternal = z.discriminatedUnion('type', [
EqlRuleResponseFields,
QueryRuleResponseFields,
SavedQueryRuleResponseFields,
@ -633,8 +638,10 @@ export const TypeSpecificResponse = z.discriminatedUnion('type', [
EsqlRuleResponseFields,
]);
export type RuleCreateProps = z.infer<typeof RuleCreateProps>;
export const RuleCreateProps = z.discriminatedUnion('type', [
export type TypeSpecificResponse = z.infer<typeof TypeSpecificResponseInternal>;
export const TypeSpecificResponse = TypeSpecificResponseInternal as z.ZodType<TypeSpecificResponse>;
const RuleCreatePropsInternal = z.discriminatedUnion('type', [
EqlRuleCreateProps,
QueryRuleCreateProps,
SavedQueryRuleCreateProps,
@ -645,8 +652,10 @@ export const RuleCreateProps = z.discriminatedUnion('type', [
EsqlRuleCreateProps,
]);
export type RuleUpdateProps = z.infer<typeof RuleUpdateProps>;
export const RuleUpdateProps = z.discriminatedUnion('type', [
export type RuleCreateProps = z.infer<typeof RuleCreatePropsInternal>;
export const RuleCreateProps = RuleCreatePropsInternal as z.ZodType<RuleCreateProps>;
const RuleUpdatePropsInternal = z.discriminatedUnion('type', [
EqlRuleUpdateProps,
QueryRuleUpdateProps,
SavedQueryRuleUpdateProps,
@ -657,8 +666,10 @@ export const RuleUpdateProps = z.discriminatedUnion('type', [
EsqlRuleUpdateProps,
]);
export type RulePatchProps = z.infer<typeof RulePatchProps>;
export const RulePatchProps = z.union([
export type RuleUpdateProps = z.infer<typeof RuleUpdatePropsInternal>;
export const RuleUpdateProps = RuleUpdatePropsInternal as z.ZodType<RuleUpdateProps>;
const RulePatchPropsInternal = z.union([
EqlRulePatchProps,
QueryRulePatchProps,
SavedQueryRulePatchProps,
@ -669,8 +680,10 @@ export const RulePatchProps = z.union([
EsqlRulePatchProps,
]);
export type RuleResponse = z.infer<typeof RuleResponse>;
export const RuleResponse = z.discriminatedUnion('type', [
export type RulePatchProps = z.infer<typeof RulePatchPropsInternal>;
export const RulePatchProps = RulePatchPropsInternal as z.ZodType<RulePatchProps>;
const RuleResponseInternal = z.discriminatedUnion('type', [
EqlRule,
QueryRule,
SavedQueryRule,
@ -680,3 +693,6 @@ export const RuleResponse = z.discriminatedUnion('type', [
NewTermsRule,
EsqlRule,
]);
export type RuleResponse = z.infer<typeof RuleResponseInternal>;
export const RuleResponse = RuleResponseInternal as z.ZodType<RuleResponse>;

View file

@ -284,8 +284,7 @@ export const BulkActionEditPayloadTimeline = z.object({
}),
});
export type BulkActionEditPayload = z.infer<typeof BulkActionEditPayload>;
export const BulkActionEditPayload = z.union([
const BulkActionEditPayloadInternal = z.union([
BulkActionEditPayloadTags,
BulkActionEditPayloadIndexPatterns,
BulkActionEditPayloadInvestigationFields,
@ -294,6 +293,10 @@ export const BulkActionEditPayload = z.union([
BulkActionEditPayloadSchedule,
]);
export type BulkActionEditPayload = z.infer<typeof BulkActionEditPayloadInternal>;
export const BulkActionEditPayload =
BulkActionEditPayloadInternal as z.ZodType<BulkActionEditPayload>;
export type BulkEditRules = z.infer<typeof BulkEditRules>;
export const BulkEditRules = BulkActionBase.merge(
z.object({