@tsed/schema-formio

Transform Ts.ED Schema & JsonSchema to a valid Formio schema

Usage no npm install needed!

<script type="module">
  import tsedSchemaFormio from 'https://cdn.skypack.dev/@tsed/schema-formio';
</script>

README

Ts.ED logo

@tsed/schema-formio

Build & Release PR Welcome Coverage Status npm version semantic-release code style: prettier backers

Website   •   Getting started   •   Slack   •   Twitter

A package of Ts.ED framework. See website: https://tsed.io/

Package to transform a Schema declared with @tsed/schema to a valid Formio schema.

Documentation

Documentation is available on https://tsed.io

Installation

You can get the latest release and the type definitions using npm:

npm install --save @tsed/schema-formio

Basic example

Given the following model

import {getFormioSchema, Form} from "@tsed/schema-formio";

@Form()
export class Model {
  @Property()
  id: string;
}

console.log(await getFormioSchema(Model));

Generates the following formio schema:

{
  "components": [
    {
      "disabled": false,
      "input": true,
      "key": "test",
      "label": "Test",
      "type": "textfield",
      "validate": {
        "required": false
      }
    }
  ],
  "display": "form",
  "machineName": "model",
  "name": "model",
  "title": "Model",
  "type": "form"
}

Decorators

This package support a large part of the JsonSchema decorators provided by @tsed/schema. So you can use Property, Required, CollectionOf, etc... decorator to generate a valid Formio schema.

Some extra decorators have been added to customize the component generated for a class property. Here the list:

  • Component: Create a custom component,
  • Currency: Change the property to a Currency component,
  • DataSourceJson: Add custom data source for a Select component,
  • DataSourceUrl: Fetch data source from an endpoint,
  • Hidden: Change the property to a Hidden component,
  • InputTags: Change the property to an InputTags component,
  • Multiple: Set the multiple flag on a property,
  • Password: Change the property to an input Password component,
  • Select: Change the property to a Select component,
  • TableView: Display or not the property in a Table,
  • Tabs: Group property using the Formio tab component,
  • Textarea: Change a component to a Textarea component.

Component

Component decorator let you to define any extra formio metadata on a decorated property:

import {Form, Component} from "@tsed/schema-formio";

@Form()
export class Model {
  @Component({
    tooltip: "MyTooltip"
  })
  tags: string;
}

So with this decorator, you can define any metadata and define your own decorator to wrap a complete component schema.

InputTags

import {getFormioSchema} from "@tsed/schema-formio";

export class Model {
  @InputTags()
  @Title("Tags for my model")
  @CollectionOf(String)
  tags: string[];
}

console.log(await getFormioSchema(Model));

Generates the following formio schema:

{
  "components": [
    {
      "key": "test",
      "type": "tags",
      "input": true,
      "label": "Test",
      "storeas": "array",
      "tableView": false,
      "disabled": false,
      "validate": {
        "required": false
      }
    }
  ],
  "display": "form",
  "machineName": "model",
  "name": "model",
  "title": "Model",
  "type": "form"
}

Contributors

Please read contributing guidelines here

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2018 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.