0.0.5 • Published 3 years ago

magic-schema v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

magic-schema

npm version npm downloads Github Actions Codecov bundle

▶️ Check online playground

Usage

Install package

Install magic-schema npm package:

yarn add magic-schema
# or
npm i magic-schema

Define reference object

First we have to define a reference object that describes types, defaults and normalizer

const defaultPlanet = {
  name: 'earth',
  specs: {
    gravity: {
      $resolve: val => parseFloat(val),
      $default: '9.8'
    },
    moons: {
      $resolve: (val = ['moon']) => [].concat(val),
      $schema: {
        title: 'planet moons'
      }
    }
  }
}

Resolving Schema

import { resolveSchema } from 'magic-schema'

const schema = resolveSchema(defaultPlanet)

Output:

{
  "properties": {
    "name": {
      "type": "string",
      "default": "earth"
    },
    "specs": {
      "properties": {
        "gravity": {
          "default": 9.8,
          "type": "number"
        },
        "moons": {
          "title": "planet moons",
          "default": [
            "moon"
          ],
          "type": "array",
          "items": [
            {
              "type": "string"
            }
          ]
        }
      },
      "type": "object"
    }
  },
  "type": "object"
}

Generating types

import { resolveSchema } from 'magic-schema'

const types = generateTypes(resolveSchema(defaultPlanet))

Output:

interface MyObject {
   /** @default "earth" */
  name: string,

  specs: {
    /** @default 9.8 */
    gravity: number,

    /**
     * planet moons
     * @default ["moon"]
    */
    moons: string[],
  },
}

Contribution

  • Clone repository
  • Install dependencies with yarn install
  • Use yarn dev to start playground (work with src/)
  • Use yarn test before push to ensure all tests and lint checks passing

License

MIT

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago