Skip to content

Commit

Permalink
[EDR Workflows] Explicitly export type in open api autogenerated ZOD …
Browse files Browse the repository at this point in the history
…types (elastic#191830)
  • Loading branch information
tomsonpl authored Sep 3, 2024
1 parent 3a352bc commit bac95ea
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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,
Expand All @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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>;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand Down

0 comments on commit bac95ea

Please sign in to comment.