Skip to content

Commit 2c080ae

Browse files
committed
refactor: deduplicate oneOf definition code
1 parent 29f2f1e commit 2c080ae

1 file changed

Lines changed: 3 additions & 25 deletions

File tree

templates/macros.njk

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@
335335
{% endmacro -%}
336336

337337
{% macro defineOneOfProperty(definitionName, prop, strictInterfaces, camelCasedPropNames) -%}
338+
{{- 'import * as t from "io-ts";' | addImport -}}
339+
338340
{% for schema in prop -%}
339341
{% if schema.type == "object" %}
340342
{% set name %}{{ definitionName }}{{ loop.index }}{% endset %}
@@ -462,31 +464,7 @@
462464
{% elif definition.oneOf %}
463465
{% set oneOfProps = definition.oneOf if definition.oneOf else definition.allOf %}
464466

465-
{{- 'import * as t from "io-ts";' | addImport -}}
466-
467-
{% for schema in oneOfProps -%}
468-
{% if schema.type == "object" %}
469-
{{ defineObject(definitionName + loop.index, schema, strictInterfaces, camelCasedPropNames) }}
470-
{% elif schema.$ref %}
471-
{%- set realPropName = schema.$ref | splitBy("/") | last -%}
472-
{{ importLocalProp(realPropName) }}
473-
{% endif %}
474-
{% endfor %}
475-
476-
export const {{ definitionName }} =
477-
t.union([
478-
{% for schema in oneOfProps -%}
479-
{% if schema.type == "object" %}
480-
{{ definitionName + loop.index }},
481-
{% elif schema.$ref %}
482-
{{ schema.$ref | splitBy("/") | last }},
483-
{% endif %}
484-
{% endfor %}
485-
],
486-
"{{ definitionName }}"
487-
);
488-
489-
export type {{ definitionName }} = t.TypeOf<typeof {{ definitionName }}>;
467+
{{ defineOneOfProperty(definitionName, oneOfProps, strictInterfaces, camelCasedPropNames) }}
490468

491469
{% elif definition.type == "number" %}
492470

0 commit comments

Comments
 (0)