Skip to main content

An elegant and automatic solution for generating/outputting Typescript interfaces and enums from your Marshmallow Schemas and enums classes.Extension of Aden Harold works on typemallow.

Project description

typemallow 2

An elegant and automatic solution for generating/outputting Typescript interfaces from your Marshmallow Schemas

I updated the typemallow package so it could Handle enums in typescript and more recent versions of marshmallow. Original work is from adenh93.

Usage:

Using typemallow2 is simple.

First, install the package pip install typemallow2

Next, for your Marshmallow schemas that you wish to generate Typescript interfaces for, simply import ts_interface and generate_ts from the typemallow module, and prepend the @ts_interface() class decorator to your Marshmallow schema class.

All that is required to generate your Typescript interfaces is to call the generate_ts() function, and provide a filepath as a parameter to output the result.

main.py

from typemallow2 import ts_interface, generate_ts


@ts_interface()
class Foo(Schema):
    some_field = fields.Str()
    another_field = fields.Date()


generate_ts('./output.ts')

output.ts

export interface Foo {
    some_field: string;
    another_field: date;
}

typemallow even supports Nested Schema fields and enums.

main.py

from typemallow2 import ts_interface, generate_ts, ts_enum
from marshmallow import Schema, fields
from marshmallow_enum import EnumField
from enum import Enum, auto


@ts_enum(value_is_auto=True)
class MyAutoEnum(Enum):
    enum1 = auto()
    enum2 = auto()

@ts_enum(value_is_auto=False)
class MyNonAutoEnum(Enum):
    enum1 = 100
    enum2 = 200
    enum3 = "Baguette"

@ts_interface()
class Boo(Schema):
    some_field = fields.Str()
    my_enum_field = EnumField(MyAutoEnum)


@ts_interface()
class Foo(Schema):
    some_field = fields.Str()
    another_field = fields.Str()
    my_field = fields.Bool()
    my_interface_field = fields.Nested(Boo, many=False)

generate_ts('./output.ts')

output.ts

export enum MyAutoEnum {
	enum1,
	enum2,
}

export interface Bar {
	some_field: string;
	my_enum_field: MyAutoEnum;
}

export interface Foo {
	some_field: string;
	another_field: string;
	my_field: boolean;
	my_interface_field: Bar;
	my_interfaces_fields: Bar[];
}

Extended Usage:

The @ts_interface() decorator function accepts an optional parameter, context, which defaults to... well... 'default'.

"Why is this the case?"

When a Marshmallow Schema is identified with with @ts_interface decorator, it is added to a list in a dictionary of schemas, with the dictionary key being the value provided to the context parameter. If you were to provide different contexts for each schema, additional keys will be created if they do not exist, or the schema will simply be appended to the list at the existing key.

This comes in handy, as the generate_ts() function also accepts an optional context parameter, which will filter only schemas in the dictionary at the specific key.

This is useful if you wish to output different contexts to different files, e.g.

main.py

...
from typemallow2 import ts_interface, generate_ts

@ts_interface(context='internal')
class Foo(Schema):
    foo = fields.Str()


@ts_interface(context='internal')
class Bar(Schema):
    bar = fields.Str()


@ts_interface(context='external')
class FooBar(Schema):
    foo_bar = fields.Str()


''' 
we're telling typemallow2 that we only want to generate interfaces from Schemas with 
an 'internal' context to './internal.ts' 
'''
generate_ts('./internal.ts', context='internal')

''' 
only generate interfaces from Schemas with an 'external' context to './external.ts' 
'''
generate_ts('./external.ts', context='external')

internal.ts

export interface Foo {
    foo: string;
}

export interface Bar {
    bar: string;
}

external.ts

export interface FooBar {
    foo_bar: string;
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typemallow2-0.0.3.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

typemallow2-0.0.3-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file typemallow2-0.0.3.tar.gz.

File metadata

  • Download URL: typemallow2-0.0.3.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for typemallow2-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c24f70cd6bd271c1ccb40b4afa3b019f161b19771ddb2c7f6b8c6a26f6abaeea
MD5 31fdd758feab0f458e94504ad519565a
BLAKE2b-256 86a2ebccf6cc570d4b91c08130c9b07412720ac8f2df77de1ec4ee2a597c2e32

See more details on using hashes here.

File details

Details for the file typemallow2-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: typemallow2-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for typemallow2-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b2595d75e13e0cb4654713b067dbe330838099aa167d6745becd8d7ea431741e
MD5 1444f276b35bb3703c692937bf60fea7
BLAKE2b-256 733c6eb30388116728397913c0a9be2e0a9c17f6e3eb5497a07f1487b04e09c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page