benkeen / generatedata

A powerful, feature-rich, random test data generator.
https://generatedata.com
2.21k stars 611 forks source link

Define central `GenerationDataSchema` type #783

Closed benkeen closed 1 year ago

benkeen commented 1 year ago

This is an early step in offering an npm package version of generate data.

See notes: https://github.com/benkeen/generatedata/blob/master/scripts/NPM-PACKAGE.md#schema-definition

benkeen commented 1 year ago

From Stack Overflow:

enum Plugins {
    A = "A",
    B = "B",
    //
};

interface OptionsMap {
    [Plugins.A]: { x: string, y: number };
    [Plugins.B]: { s: boolean, t: Date };
}

type Options = { [K in Plugins]: { plugin: K, options: OptionsMap[K] } }[Plugins]

const allOptions: Options[] = [
    {
        plugin: Plugins.A,
        options: {
            x: "",
            y: 1
        }
    },
    {
        plugin: Plugins.B,
        options: {
            s: true,
            t: new Date()
        }
    },
    {
        plugin: Plugins.A,
        options: {
            s: false, // error!
            t: new Date()
        } 
    }
]
benkeen commented 1 year ago

Bit fussy & requires user to use enums instead of strings. Also it requires user to provide options: null for certain DTs. Will deal with that later.